Optimize database, implement automatic command deployment, and improve project-wide type safety
Some checks failed
Auto Build and Push Docker Image / build (push) Failing after 14s

This commit is contained in:
2026-04-29 19:50:50 +02:00
parent b2c3f5731b
commit d546035bb7
14 changed files with 258 additions and 177 deletions

View File

@@ -1,4 +1,5 @@
import { EmbedBuilder, TextChannel } from 'discord.js';
import { ExtendedClient } from './ExtendedClient.js';
export class TwitchManager {
private static accessToken: string | null = null;
@@ -62,8 +63,8 @@ export class TwitchManager {
return streams[0] || null;
}
static async checkStreams(client: any) {
const monitors: any[] = (client as any).DB?.all('SELECT * FROM twitch_monitors') || [];
static async checkStreams(client: ExtendedClient) {
const monitors: any[] = client.DB?.all('SELECT * FROM twitch_monitors') || [];
if (monitors.length === 0) return;
const uniqueChannels = [...new Set(monitors.map(m => m.channel_name.toLowerCase()))];
@@ -78,34 +79,47 @@ export class TwitchManager {
}
}
const updates: { id: number, status: 'online' | 'offline', streamId?: string }[] = [];
for (const monitor of monitors) {
try {
const stream = onlineStreamsMap.get(monitor.channel_name.toLowerCase());
const stream = onlineStreamsMap.get(monitor.channel_name.toLowerCase());
if (stream) {
const isNewStream = monitor.last_stream_id !== stream.id;
const wasOffline = monitor.last_status === 'offline';
if (stream) {
const isNewStream = monitor.last_stream_id !== stream.id;
const wasOffline = monitor.last_status === 'offline';
if (wasOffline || (monitor.last_status === 'online' && isNewStream)) {
await this.sendNotification(client, monitor, stream);
(client as any).DB?.run(
if (wasOffline || (monitor.last_status === 'online' && isNewStream)) {
await this.sendNotification(client, monitor, stream);
updates.push({ id: monitor.id, status: 'online', streamId: stream.id });
}
} else {
if (monitor.last_status === 'online') {
updates.push({ id: monitor.id, status: 'offline' });
}
}
}
if (updates.length > 0) {
client.DB?.transaction(() => {
for (const update of updates) {
if (update.status === 'online') {
client.DB?.run(
"UPDATE twitch_monitors SET last_status = 'online', last_stream_id = ? WHERE id = ?",
stream.id,
monitor.id
update.streamId,
update.id
);
} else {
client.DB?.run(
"UPDATE twitch_monitors SET last_status = 'offline', last_stream_id = NULL WHERE id = ?",
update.id
);
}
} else {
if (monitor.last_status === 'online') {
(client as any).DB?.run("UPDATE twitch_monitors SET last_status = 'offline', last_stream_id = NULL WHERE id = ?", monitor.id);
}
}
} catch (error) {
console.error(`[TWITCH] Error processing monitor ${monitor.channel_name}:`, error);
}
});
}
}
private static async sendNotification(client: any, monitor: any, stream: any) {
private static async sendNotification(client: ExtendedClient, monitor: any, stream: any) {
try {
const guild = client.guilds.cache.get(monitor.guild_id);
if (!guild || !monitor.discord_channel_id) return;