Team Ai
Apppublic

Stryker-SC/Discord-Bot-VCMaster-JavaScript-Revamped

sourceHugging Faceupdated 2mo agoView on Hugging Face
0likes
ready.js75 linesDownload Raw Back to events
1const { Events } = require('discord.js');2const logger = require('../utils/logger');3 4module.exports = {5    name: Events.ClientReady,6    once: true,7    execute(client) {8        logger.info(`[Ready] Logged in as ${client.user.tag}`);9 10        // Cleanup: Check all registered temp channels in the background (non-blocking)11        runBackgroundCleanup(client);12    },13};14 15async function runBackgroundCleanup(client) {16    // 10s delay to let boot stabilize and prevent immediate rate limits17    await new Promise(resolve => setTimeout(resolve, 10000));18    19    try {20        const tempChannels = await client.db.getAllTempChannels(); 21        22        // Group temp channels by guild ID to avoid redundant processing23        const channelsByGuild = new Map();24        for (const record of tempChannels) {25            if (!channelsByGuild.has(record.guild_id)) {26                channelsByGuild.set(record.guild_id, []);27            }28            channelsByGuild.get(record.guild_id).push(record);29        }30 31        for (const [guildId, records] of channelsByGuild) {32            // Read guild from memory cache synchronously (no network call)33            const guild = client.guilds.cache.get(guildId);34            if (!guild) continue; // Bot is no longer in this guild, or it's not cached35 36            try {37                // Bulk fetch all channels in the guild once (avoids N+1 rate limits)38                const allChannels = await guild.channels.fetch();39 40                for (const record of records) {41                    const channel = allChannels.get(record.channel_id);42 43                    if (!channel) {44                        // Channel does not exist on Discord (ghost channel)45                        logger.info(`[Cleanup] Removing ghost channel ${record.channel_id} from DB.`);46                        47                        const textChannel = allChannels.get(record.text_channel_id);48                        if (textChannel) {49                            await textChannel.delete().catch(e => logger.error("[Cleanup] Failed to delete orphaned text channel:", e));50                        }51                        52                        await client.db.removeTempChannel(record.channel_id);53                    } else if (channel.members && channel.members.size === 0) {54                        // Channel exists but is empty, delete it55                        logger.info(`[Cleanup] Deleting empty channel ${channel.name} (${channel.id}).`);56                        await channel.delete().catch(e => logger.error("[Cleanup] Failed to delete voice channel:", e));57 58                        // Delete Paired Text Channel59                        const textChannel = allChannels.get(record.text_channel_id);60                        if (textChannel) {61                            await textChannel.delete().catch(e => logger.error("[Cleanup] Failed to delete text channel:", e));62                        }63 64                        await client.db.removeTempChannel(record.channel_id);65                    }66                }67            } catch (error) {68                logger.error(`[Cleanup] Failed to process guild ${guildId}:`, error);69            }70        }71    } catch (error) {72        logger.error(`[Cleanup] Fatal error during cleanup routine:`, error);73    }74}75