From aa634b91f697c68487b1b4395a87401603adceeb Mon Sep 17 00:00:00 2001 From: Fuwn Date: Wed, 1 Oct 2025 20:14:15 -0700 Subject: refactor(listeners): Move clientReady listeners to clientReady module --- packages/gateway/src/listeners/clientReady.ts | 88 ---------------------- .../src/listeners/clientReady/clientReady.ts | 88 ++++++++++++++++++++++ .../src/listeners/clientReady/voiceConnection.ts | 79 +++++++++++++++++++ packages/gateway/src/listeners/index.ts | 4 +- .../messageCreate/moderationAgent/index.ts | 2 +- .../src/listeners/messageCreate/roleplayUmagram.ts | 2 +- packages/gateway/src/listeners/voiceConnection.ts | 79 ------------------- 7 files changed, 171 insertions(+), 171 deletions(-) delete mode 100644 packages/gateway/src/listeners/clientReady.ts create mode 100644 packages/gateway/src/listeners/clientReady/clientReady.ts create mode 100644 packages/gateway/src/listeners/clientReady/voiceConnection.ts delete mode 100644 packages/gateway/src/listeners/voiceConnection.ts diff --git a/packages/gateway/src/listeners/clientReady.ts b/packages/gateway/src/listeners/clientReady.ts deleted file mode 100644 index 1b790ab..0000000 --- a/packages/gateway/src/listeners/clientReady.ts +++ /dev/null @@ -1,88 +0,0 @@ -import { Client, Events } from "discord.js"; -import { ROLEPLAY_UMAGRAM_CHANNEL_ID } from "./constants"; - -export const handleClientReady = (client: Client) => { - client.once(Events.ClientReady, async (readyClient) => { - await readyClient.user.setActivity("r/okbuddyumamusume", { type: 3 }); - - try { - const channel = client.channels.cache.get(ROLEPLAY_UMAGRAM_CHANNEL_ID); - - if (channel && channel.isTextBased()) { - const processChannelMessages = async ( - targetChannel: any, - channelName: string, - ) => { - let lastMessageId: string | undefined; - let messageCount = 0; - let heartCount = 0; - - while (true) { - const messages = await targetChannel.messages.fetch({ - limit: 100, - before: lastMessageId, - }); - - if (messages.size === 0) break; - - for (const message of messages.values()) { - messageCount += 1; - - const existingReaction = message.reactions.cache.get("❤️"); - - if ( - existingReaction && - existingReaction.users.cache.has(client.user!.id) - ) - continue; - - try { - await message.react("❤️"); - - heartCount += 1; - - await new Promise((resolve) => setTimeout(resolve, 100)); - } catch (error) { - console.error( - `Failed to heart message ${message.id} in ${channelName}:`, - error, - ); - } - } - - lastMessageId = messages.last()?.id; - - await new Promise((resolve) => setTimeout(resolve, 500)); - } - - return { messageCount, heartCount }; - }; - - await processChannelMessages(channel, "Main Channel"); - - try { - const activeThreads = await (channel as any).threads?.fetchActive(); - const archivedThreads = await ( - channel as any - ).threads?.fetchArchived(); - const allThreads = [ - ...activeThreads.threads.values(), - ...archivedThreads.threads.values(), - ]; - - for (const thread of allThreads) { - try { - await processChannelMessages(thread, `Thread: ${thread.name}`); - } catch (error) { - console.error(`Error processing thread ${thread.name}:`, error); - } - } - } catch (error) { - console.error("Error fetching threads:", error); - } - } - } catch (error) { - console.error("Error adding hearts to existing messages:", error); - } - }); -}; diff --git a/packages/gateway/src/listeners/clientReady/clientReady.ts b/packages/gateway/src/listeners/clientReady/clientReady.ts new file mode 100644 index 0000000..0a6ad26 --- /dev/null +++ b/packages/gateway/src/listeners/clientReady/clientReady.ts @@ -0,0 +1,88 @@ +import { Client, Events } from "discord.js"; +import { ROLEPLAY_UMAGRAM_CHANNEL_ID } from "../constants"; + +export const handleClientReady = (client: Client) => { + client.once(Events.ClientReady, async (readyClient) => { + await readyClient.user.setActivity("r/okbuddyumamusume", { type: 3 }); + + try { + const channel = client.channels.cache.get(ROLEPLAY_UMAGRAM_CHANNEL_ID); + + if (channel && channel.isTextBased()) { + const processChannelMessages = async ( + targetChannel: any, + channelName: string, + ) => { + let lastMessageId: string | undefined; + let messageCount = 0; + let heartCount = 0; + + while (true) { + const messages = await targetChannel.messages.fetch({ + limit: 100, + before: lastMessageId, + }); + + if (messages.size === 0) break; + + for (const message of messages.values()) { + messageCount += 1; + + const existingReaction = message.reactions.cache.get("❤️"); + + if ( + existingReaction && + existingReaction.users.cache.has(client.user!.id) + ) + continue; + + try { + await message.react("❤️"); + + heartCount += 1; + + await new Promise((resolve) => setTimeout(resolve, 100)); + } catch (error) { + console.error( + `Failed to heart message ${message.id} in ${channelName}:`, + error, + ); + } + } + + lastMessageId = messages.last()?.id; + + await new Promise((resolve) => setTimeout(resolve, 500)); + } + + return { messageCount, heartCount }; + }; + + await processChannelMessages(channel, "Main Channel"); + + try { + const activeThreads = await (channel as any).threads?.fetchActive(); + const archivedThreads = await ( + channel as any + ).threads?.fetchArchived(); + const allThreads = [ + ...activeThreads.threads.values(), + ...archivedThreads.threads.values(), + ]; + + for (const thread of allThreads) { + try { + await processChannelMessages(thread, `Thread: ${thread.name}`); + } catch (error) { + console.error(`Error processing thread ${thread.name}:`, error); + } + } + } catch (error) { + console.error("Error fetching threads:", error); + } + } + } catch (error) { + console.error("Error adding hearts to existing messages:", error); + } + }); +}; diff --git a/packages/gateway/src/listeners/clientReady/voiceConnection.ts b/packages/gateway/src/listeners/clientReady/voiceConnection.ts new file mode 100644 index 0000000..2de1f90 --- /dev/null +++ b/packages/gateway/src/listeners/clientReady/voiceConnection.ts @@ -0,0 +1,79 @@ +import { Client, Events } from "discord.js"; +import { + joinVoiceChannel, + VoiceConnectionStatus, + createAudioPlayer, +} from "@discordjs/voice"; + +const VOICE_CHANNEL_ID = "1422049438863589487"; + +export const handleVoiceConnection = (client: Client) => { + client.once(Events.ClientReady, async () => { + const voiceChannel = client.channels.cache.get(VOICE_CHANNEL_ID); + + if (!voiceChannel || !voiceChannel.isVoiceBased()) { + console.error( + `Voice channel ${VOICE_CHANNEL_ID} not found or is not a voice channel`, + ); + + return; + } + + try { + const connection = joinVoiceChannel({ + channelId: voiceChannel.id, + guildId: voiceChannel.guildId!, + adapterCreator: voiceChannel.guild.voiceAdapterCreator, + }); + const player = createAudioPlayer(); + + connection.subscribe(player); + connection.on("error", (error) => { + console.error("Voice connection error:", error); + setTimeout(() => { + handleVoiceConnection(client); + }, 15000); + }); + connection.on(VoiceConnectionStatus.Disconnected, () => { + setTimeout(() => { + try { + const newConnection = joinVoiceChannel({ + channelId: voiceChannel.id, + guildId: voiceChannel.guildId!, + adapterCreator: voiceChannel.guild.voiceAdapterCreator, + }); + const newPlayer = createAudioPlayer(); + + newConnection.subscribe(newPlayer); + newConnection.on("error", (error) => { + console.error("Voice reconnection error:", error); + setTimeout(() => { + handleVoiceConnection(client); + }, 15000); + }); + newConnection.on(VoiceConnectionStatus.Disconnected, () => { + setTimeout(() => { + handleVoiceConnection(client); + }, 5000); + }); + } catch (error) { + console.error("Failed to reconnect to voice channel:", error); + setTimeout(() => { + handleVoiceConnection(client); + }, 10000); + } + }, 5000); + }); + connection.on(VoiceConnectionStatus.Destroyed, () => { + setTimeout(() => { + handleVoiceConnection(client); + }, 5000); + }); + } catch (error) { + console.error("Failed to connect to voice channel:", error); + setTimeout(() => { + handleVoiceConnection(client); + }, 10000); + } + }); +}; diff --git a/packages/gateway/src/listeners/index.ts b/packages/gateway/src/listeners/index.ts index 492f9b0..b0ca723 100644 --- a/packages/gateway/src/listeners/index.ts +++ b/packages/gateway/src/listeners/index.ts @@ -4,8 +4,8 @@ import { handleRoleProtection } from "./roleProtection"; import { handleChannelDeletion } from "./channelDeletion"; import { handleMessageDeletion } from "./messageDeletion"; import { handleMessageEdit } from "./messageEdit"; -import { handleClientReady } from "./clientReady"; -import { handleVoiceConnection } from "./voiceConnection"; +import { handleClientReady } from "./clientReady/clientReady"; +import { handleVoiceConnection } from "./clientReady/voiceConnection"; import { handleMediaModeration } from "./mediaModeration"; export const handleListeners = (client: Client) => { diff --git a/packages/gateway/src/listeners/messageCreate/moderationAgent/index.ts b/packages/gateway/src/listeners/messageCreate/moderationAgent/index.ts index 66aa52c..40f4423 100644 --- a/packages/gateway/src/listeners/messageCreate/moderationAgent/index.ts +++ b/packages/gateway/src/listeners/messageCreate/moderationAgent/index.ts @@ -1,5 +1,5 @@ import { Message, TextChannel, ThreadChannel } from "discord.js"; -import { sendAuditLog } from "../../commands/utilities"; +import { sendAuditLog } from "../../../commands/utilities"; import { EXCLUDED_CATEGORIES, LOW_RISK_PATTERNS, diff --git a/packages/gateway/src/listeners/messageCreate/roleplayUmagram.ts b/packages/gateway/src/listeners/messageCreate/roleplayUmagram.ts index a60a1b6..31ab60b 100644 --- a/packages/gateway/src/listeners/messageCreate/roleplayUmagram.ts +++ b/packages/gateway/src/listeners/messageCreate/roleplayUmagram.ts @@ -1,5 +1,5 @@ import { Message } from "discord.js"; -import { ROLEPLAY_UMAGRAM_CHANNEL_ID } from "./constants"; +import { ROLEPLAY_UMAGRAM_CHANNEL_ID } from "../constants"; export const handleRoleplayUmagram = async (message: Message) => { const isMainChannel = message.channelId === ROLEPLAY_UMAGRAM_CHANNEL_ID; diff --git a/packages/gateway/src/listeners/voiceConnection.ts b/packages/gateway/src/listeners/voiceConnection.ts deleted file mode 100644 index 2de1f90..0000000 --- a/packages/gateway/src/listeners/voiceConnection.ts +++ /dev/null @@ -1,79 +0,0 @@ -import { Client, Events } from "discord.js"; -import { - joinVoiceChannel, - VoiceConnectionStatus, - createAudioPlayer, -} from "@discordjs/voice"; - -const VOICE_CHANNEL_ID = "1422049438863589487"; - -export const handleVoiceConnection = (client: Client) => { - client.once(Events.ClientReady, async () => { - const voiceChannel = client.channels.cache.get(VOICE_CHANNEL_ID); - - if (!voiceChannel || !voiceChannel.isVoiceBased()) { - console.error( - `Voice channel ${VOICE_CHANNEL_ID} not found or is not a voice channel`, - ); - - return; - } - - try { - const connection = joinVoiceChannel({ - channelId: voiceChannel.id, - guildId: voiceChannel.guildId!, - adapterCreator: voiceChannel.guild.voiceAdapterCreator, - }); - const player = createAudioPlayer(); - - connection.subscribe(player); - connection.on("error", (error) => { - console.error("Voice connection error:", error); - setTimeout(() => { - handleVoiceConnection(client); - }, 15000); - }); - connection.on(VoiceConnectionStatus.Disconnected, () => { - setTimeout(() => { - try { - const newConnection = joinVoiceChannel({ - channelId: voiceChannel.id, - guildId: voiceChannel.guildId!, - adapterCreator: voiceChannel.guild.voiceAdapterCreator, - }); - const newPlayer = createAudioPlayer(); - - newConnection.subscribe(newPlayer); - newConnection.on("error", (error) => { - console.error("Voice reconnection error:", error); - setTimeout(() => { - handleVoiceConnection(client); - }, 15000); - }); - newConnection.on(VoiceConnectionStatus.Disconnected, () => { - setTimeout(() => { - handleVoiceConnection(client); - }, 5000); - }); - } catch (error) { - console.error("Failed to reconnect to voice channel:", error); - setTimeout(() => { - handleVoiceConnection(client); - }, 10000); - } - }, 5000); - }); - connection.on(VoiceConnectionStatus.Destroyed, () => { - setTimeout(() => { - handleVoiceConnection(client); - }, 5000); - }); - } catch (error) { - console.error("Failed to connect to voice channel:", error); - setTimeout(() => { - handleVoiceConnection(client); - }, 10000); - } - }); -}; -- cgit v1.2.3