diff options
| author | Fuwn <[email protected]> | 2025-10-20 22:37:26 -0700 |
|---|---|---|
| committer | Fuwn <[email protected]> | 2025-10-20 22:37:26 -0700 |
| commit | 5d359e815a253087fd4dbdec02b9c65d1dac9fe4 (patch) | |
| tree | 13f87094d6f02610145703c05b19fb0e79c2060c | |
| parent | feat(shared:log): Improve unit logging (diff) | |
| download | umabotdiscord-5d359e815a253087fd4dbdec02b9c65d1dac9fe4.tar.xz umabotdiscord-5d359e815a253087fd4dbdec02b9c65d1dac9fe4.zip | |
feat(gateway): Begin migrating console logs to shared logging framework
23 files changed, 108 insertions, 117 deletions
diff --git a/packages/gateway/src/commands/crp.ts b/packages/gateway/src/commands/crp.ts index 4758574..70e611c 100644 --- a/packages/gateway/src/commands/crp.ts +++ b/packages/gateway/src/commands/crp.ts @@ -1,4 +1,5 @@ import { Message, Role } from "discord.js"; +import { logUnexpectedDiscordAPIError } from "../utilities"; export const handleCrpCommand = async (message: Message) => { if (message.author.bot) return; @@ -88,7 +89,7 @@ export const handleCrpCommand = async (message: Message) => { `✅ Successfully copied permissions from **${sourceRole.name}** to: **${targetRoleNames}**`, ); } catch (error) { - console.error("Error copying role permissions:", error); + logUnexpectedDiscordAPIError(error); await message.reply( "❌ Failed to copy role permissions. Check bot permissions and try again.", ); diff --git a/packages/gateway/src/commands/delete.ts b/packages/gateway/src/commands/delete.ts index bdbe585..32d6a85 100644 --- a/packages/gateway/src/commands/delete.ts +++ b/packages/gateway/src/commands/delete.ts @@ -1,5 +1,5 @@ import { Message } from "discord.js"; -import { replyWithCleanup } from "../utilities"; +import { logUnexpectedDiscordAPIError, replyWithCleanup } from "../utilities"; export const handleDeleteCommand = async (message: Message) => { if (message.author.bot) return; @@ -97,10 +97,7 @@ export const handleDeleteCommand = async (message: Message) => { `Failed to fetch ${failedFetchCount} out of ${messageIds.length} messages for bulk delete`, ); } catch (bulkError) { - console.warn( - "Bulk delete failed, falling back to individual deletion:", - bulkError, - ); + logUnexpectedDiscordAPIError(bulkError); let failedDeleteCount = 0; @@ -136,7 +133,7 @@ export const handleDeleteCommand = async (message: Message) => { ); } } catch (error) { - console.error("Error in bulk delete process:", error); + logUnexpectedDiscordAPIError(error); await message.reply( "❌ Failed to delete messages. Check bot permissions and try again.", ); @@ -156,7 +153,7 @@ export const handleDeleteCommand = async (message: Message) => { } } } catch (error) { - console.error("Error deleting messages:", error); + logUnexpectedDiscordAPIError(error); await message.reply( "❌ Failed to delete messages. Check bot permissions and try again.", ); diff --git a/packages/gateway/src/commands/pin.ts b/packages/gateway/src/commands/pin.ts index 86f24be..884e41b 100644 --- a/packages/gateway/src/commands/pin.ts +++ b/packages/gateway/src/commands/pin.ts @@ -1,5 +1,5 @@ import { Message } from "discord.js"; -import { replyWithCleanup } from "../utilities"; +import { logUnexpectedDiscordAPIError, replyWithCleanup } from "../utilities"; export const handlePinCommand = async (message: Message) => { if (message.author.bot) return; @@ -97,7 +97,7 @@ export const handlePinCommand = async (message: Message) => { }); } } catch (error) { - console.error(`Error processing message ${messageId}:`, error); + logUnexpectedDiscordAPIError(error); results.push({ success: false, messageId, @@ -129,12 +129,12 @@ export const handlePinCommand = async (message: Message) => { try { await replyMessage.delete(); } catch (error) { - console.error("Failed to delete result message:", error); + logUnexpectedDiscordAPIError(error); } }, 10000); } } catch (error) { - console.error("Error pinning message:", error); + logUnexpectedDiscordAPIError(error); if ( error instanceof Error && diff --git a/packages/gateway/src/commands/react.ts b/packages/gateway/src/commands/react.ts index 08e5822..bbe1fe9 100644 --- a/packages/gateway/src/commands/react.ts +++ b/packages/gateway/src/commands/react.ts @@ -1,5 +1,5 @@ import { Message } from "discord.js"; -import { replyWithCleanup } from "../utilities"; +import { logUnexpectedDiscordAPIError, replyWithCleanup } from "../utilities"; export const handleReactCommand = async (message: Message) => { if (message.author.bot) return; @@ -94,7 +94,7 @@ export const handleReactCommand = async (message: Message) => { results.push(`Added ${emoji}`); } } catch (error) { - console.error(`Error processing emoji ${emoji}:`, error); + logUnexpectedDiscordAPIError(error); errors.push(emoji); } } @@ -108,7 +108,7 @@ export const handleReactCommand = async (message: Message) => { await replyWithCleanup(message, resultMessage); } catch (error) { - console.error("Error toggling reactions:", error); + logUnexpectedDiscordAPIError(error); await replyWithCleanup( message, "❌ Failed to toggle reactions on the message.", diff --git a/packages/gateway/src/commands/role.ts b/packages/gateway/src/commands/role.ts index f0cbb29..3144aae 100644 --- a/packages/gateway/src/commands/role.ts +++ b/packages/gateway/src/commands/role.ts @@ -1,5 +1,5 @@ import { Message } from "discord.js"; -import { replyWithCleanup } from "../utilities"; +import { logUnexpectedDiscordAPIError, replyWithCleanup } from "../utilities"; export const handleRoleCommand = async (message: Message) => { if (message.author.bot) return; @@ -134,7 +134,7 @@ export const handleRoleCommand = async (message: Message) => { `✅ ${actionText} for **${targetUser.username}** in **${guild.name}**.`, ); } catch (error) { - console.error("Error in role command:", error); + logUnexpectedDiscordAPIError(error); await replyWithCleanup( message, "❌ Failed to manage role. Check bot permissions and try again.", diff --git a/packages/gateway/src/commands/say.ts b/packages/gateway/src/commands/say.ts index 2783fb0..9df7abe 100644 --- a/packages/gateway/src/commands/say.ts +++ b/packages/gateway/src/commands/say.ts @@ -1,5 +1,5 @@ import { Message } from "discord.js"; -import { replyWithCleanup } from "../utilities"; +import { logUnexpectedDiscordAPIError, replyWithCleanup } from "../utilities"; export const handleSayCommand = async (message: Message) => { if (message.author.bot) return; @@ -107,7 +107,7 @@ export const handleSayCommand = async (message: Message) => { await (targetChannel as any).send(messageContent); } } catch (error) { - console.error("Error executing say command:", error); + logUnexpectedDiscordAPIError(error); try { await replyWithCleanup( @@ -115,7 +115,7 @@ export const handleSayCommand = async (message: Message) => { "❌ Failed to execute the say command. Please check permissions.", ); } catch (replyError) { - console.error("Failed to send error reply:", replyError); + logUnexpectedDiscordAPIError(replyError); } } } diff --git a/packages/gateway/src/commands/sayc.ts b/packages/gateway/src/commands/sayc.ts index eafb2f1..2c8a28c 100644 --- a/packages/gateway/src/commands/sayc.ts +++ b/packages/gateway/src/commands/sayc.ts @@ -1,5 +1,5 @@ import { Message } from "discord.js"; -import { replyWithCleanup } from "../utilities"; +import { logUnexpectedDiscordAPIError, replyWithCleanup } from "../utilities"; export const handleSaycCommand = async (message: Message): Promise<boolean> => { if (message.author.bot) return false; @@ -41,7 +41,7 @@ export const handleSaycCommand = async (message: Message): Promise<boolean> => { return true; } catch (error) { - console.error("Error in sayc command:", error); + logUnexpectedDiscordAPIError(error); await replyWithCleanup( message, "❌ Failed to send message to the specified channel.", diff --git a/packages/gateway/src/commands/start.ts b/packages/gateway/src/commands/start.ts index 9b8beb5..d93df5f 100644 --- a/packages/gateway/src/commands/start.ts +++ b/packages/gateway/src/commands/start.ts @@ -1,5 +1,6 @@ import { Message } from "discord.js"; import { sendProgressUpdate, executeBulkRoleAssignment } from "./utilities"; +import { logUnexpectedDiscordAPIError } from "../utilities"; export const handleStartCommand = async (message: Message) => { if (message.author.bot) return; @@ -68,7 +69,7 @@ export const handleStartCommand = async (message: Message) => { channelId, categoryId, ).catch((error) => { - console.error("Bulk role assignment failed:", error); + logUnexpectedDiscordAPIError(error); sendProgressUpdate( message.client, "❌ Bulk role assignment failed due to an error", diff --git a/packages/gateway/src/commands/utilities.ts b/packages/gateway/src/commands/utilities.ts index 7ed18b4..8891074 100644 --- a/packages/gateway/src/commands/utilities.ts +++ b/packages/gateway/src/commands/utilities.ts @@ -1,5 +1,6 @@ import { Client, ChannelType } from "discord.js"; import { CENTRAL_GUILD_ID } from "../constants"; +import { logUnexpectedDiscordAPIError } from "../utilities"; export const AUDIT_LOG_GUILD_ID = "1419211292396224575"; export const AUDIT_LOG_CHANNEL_ID = "1419211778793144411"; @@ -14,7 +15,7 @@ export const sendProgressUpdate = async ( if (channel && "send" in channel) await (channel as any).send(message); } catch (error) { - console.error("Failed to send progress update:", error); + logUnexpectedDiscordAPIError(error); } }; @@ -72,7 +73,7 @@ export const sendAuditLog = async ( } } } catch (error) { - console.error("Failed to send audit log:", error); + logUnexpectedDiscordAPIError(error); } }; @@ -121,10 +122,7 @@ export const fetchMessagesFromChannel = async ( await new Promise((resolve) => setTimeout(resolve, 100)); } catch (error) { - console.error( - `Error fetching messages from channel ${channel.id}:`, - error, - ); + logUnexpectedDiscordAPIError(error); hasMoreMessages = false; } @@ -175,11 +173,11 @@ export const fetchForumPosts = async ( updateChannelId, ); } catch (error) { - console.error(`Error processing forum post ${thread.id}:`, error); + logUnexpectedDiscordAPIError(error); } } } catch (error) { - console.error(`Error fetching forum posts from ${forumChannel.id}:`, error); + logUnexpectedDiscordAPIError(error); } return { messageCount: totalMessageCount, batchCount: totalBatchCount }; @@ -227,12 +225,12 @@ export const fetchChannelThreads = async ( updateChannelId, ); } catch (error) { - console.error(`Error processing thread ${thread.id}:`, error); + logUnexpectedDiscordAPIError(error); } } } } catch (error) { - console.error(`Error fetching threads from ${textChannel.id}:`, error); + logUnexpectedDiscordAPIError(error); } return { messageCount: totalMessageCount, batchCount: totalBatchCount }; @@ -442,10 +440,7 @@ export const getAllChannelsInCategory = async ( channelNames[thread.id] = `🧵 ${getChannelName(thread)}`; } } catch (error) { - console.error( - `Error fetching threads for channel ${channel.id}:`, - error, - ); + logUnexpectedDiscordAPIError(error); } } @@ -464,10 +459,7 @@ export const getAllChannelsInCategory = async ( channelNames[thread.id] = `📝 ${getChannelName(thread)}`; } } catch (error) { - console.error( - `Error fetching forum posts for channel ${channel.id}:`, - error, - ); + logUnexpectedDiscordAPIError(error); } } } @@ -546,7 +538,7 @@ export const sendPersonaLog = async ( await (channel as any).send({ embeds: [embed] }); } catch (error) { - console.error("Failed to send persona log:", error); + logUnexpectedDiscordAPIError(error); } }; @@ -653,7 +645,7 @@ export const executeBulkRoleAssignment = async ( await new Promise((resolve) => setTimeout(resolve, 200)); } catch (error: any) { - console.error(`Error processing user ${userId}:`, error); + logUnexpectedDiscordAPIError(error); if (error.code === 10007 || error.message?.includes("Unknown Member")) { notInGuildCount += 1; @@ -669,7 +661,7 @@ export const executeBulkRoleAssignment = async ( customUpdateChannelId, ); } catch (error) { - console.error("Error in bulk role assignment:", error); + logUnexpectedDiscordAPIError(error); await sendProgressUpdate( client, "❌ An error occurred during bulk role assignment", diff --git a/packages/gateway/src/commands/verbalGates.ts b/packages/gateway/src/commands/verbalGates.ts index 1fb655b..6032b86 100644 --- a/packages/gateway/src/commands/verbalGates.ts +++ b/packages/gateway/src/commands/verbalGates.ts @@ -1,5 +1,5 @@ import { Message } from "discord.js"; -import { replyWithCleanup } from "../utilities"; +import { logUnexpectedDiscordAPIError, replyWithCleanup } from "../utilities"; import { ROLEPLAY_GUILD_ID } from "../constants"; const RACECOURSES_CATEGORY_ID = "1428169113754402836"; @@ -120,7 +120,7 @@ export const handleVerbalGatesCommand = async ( try { await message.react("✅"); } catch (error) { - console.error("Error reacting to message:", error); + logUnexpectedDiscordAPIError(error); } return true; @@ -149,12 +149,12 @@ export const handleVerbalGatesCommand = async ( try { await message.react("✅"); } catch (error) { - console.error("Error reacting to message:", error); + logUnexpectedDiscordAPIError(error); } return true; } catch (error) { - console.error("Error in verbal gates command:", error); + logUnexpectedDiscordAPIError(error); await replyWithCleanup( message, "❌ An error occurred while managing channel permissions.", diff --git a/packages/gateway/src/listeners/channelDeletion.ts b/packages/gateway/src/listeners/channelDeletion.ts index c576f50..07aa139 100644 --- a/packages/gateway/src/listeners/channelDeletion.ts +++ b/packages/gateway/src/listeners/channelDeletion.ts @@ -1,5 +1,6 @@ import { Client, Events } from "discord.js"; import { CENTRAL_GUILD_ID, DISCORD_AUDIT_LOG_TYPES } from "../constants"; +import { logUnexpectedDiscordAPIError } from "../utilities"; const channelDeletionTracker = new Map< string, @@ -79,10 +80,7 @@ export const handleChannelDeletion = (client: Client) => { } } } catch (error) { - console.error( - `Failed to reset roles for user ${executor.tag}:`, - error, - ); + logUnexpectedDiscordAPIError(error); } channelDeletionTracker.delete(executor.id); @@ -101,7 +99,7 @@ export const handleChannelDeletion = (client: Client) => { if (now - data.firstDeletion > thirtySeconds) channelDeletionTracker.delete(userId); } catch (error) { - console.error("Error in channel deletion monitoring:", error); + logUnexpectedDiscordAPIError(error); } }); }; diff --git a/packages/gateway/src/listeners/clientReady/umagramCatchup.ts b/packages/gateway/src/listeners/clientReady/umagramCatchup.ts index 0506cb5..794d895 100644 --- a/packages/gateway/src/listeners/clientReady/umagramCatchup.ts +++ b/packages/gateway/src/listeners/clientReady/umagramCatchup.ts @@ -5,6 +5,7 @@ import { CENTRAL_UMAGRAM_CHANNEL_ID, ROLEPLAY_UMAGRAM_CHANNEL_ID, } from "../../constants"; +import { logUnexpectedDiscordAPIError } from "../../utilities"; const GUILD_UMAGRAM_CHANNELS = { [CENTRAL_GUILD_ID]: CENTRAL_UMAGRAM_CHANNEL_ID, @@ -28,7 +29,7 @@ const processUmagramChannel = async ( const processChannelMessages = async ( targetChannel: any, - channelName: string, + channelName: string /* eslint-disable-line @typescript-eslint/no-unused-vars, no-unused-vars */, ) => { let lastMessageId: string | undefined; let messageCount = 0; @@ -60,10 +61,7 @@ const processUmagramChannel = async ( await new Promise((resolve) => setTimeout(resolve, 100)); } catch (error) { - console.error( - `Failed to heart message ${message.id} in ${channelName}:`, - error, - ); + logUnexpectedDiscordAPIError(error); } } @@ -92,10 +90,10 @@ const processUmagramChannel = async ( `Guild ${guildId} - Thread: ${thread.name}`, ); } catch (error) { - console.error(`Error processing thread ${thread.name}:`, error); + logUnexpectedDiscordAPIError(error); } } catch (error) { - console.error("Error fetching threads:", error); + logUnexpectedDiscordAPIError(error); } }; @@ -104,6 +102,6 @@ export const handleUmagramCatchup = async (client: Client) => { for (const [guildId, channelId] of Object.entries(GUILD_UMAGRAM_CHANNELS)) await processUmagramChannel(client, guildId, channelId); } catch (error) { - console.error("Error adding hearts to existing messages:", error); + logUnexpectedDiscordAPIError(error); } }; diff --git a/packages/gateway/src/listeners/clientReady/voiceConnection.ts b/packages/gateway/src/listeners/clientReady/voiceConnection.ts index 3665536..4b2fd45 100644 --- a/packages/gateway/src/listeners/clientReady/voiceConnection.ts +++ b/packages/gateway/src/listeners/clientReady/voiceConnection.ts @@ -11,6 +11,8 @@ import { CENTRAL_VOICE_CHANNEL_ID, ROLEPLAY_VOICE_CHANNEL_ID, } from "../../constants"; +import { logUnexpectedDiscordAPIError } from "../../utilities"; +import { logUnexpectedDiscordAPIResult } from "../../../../shared/log"; const GUILD_VOICE_CHANNELS = { [CENTRAL_GUILD_ID]: CENTRAL_VOICE_CHANNEL_ID, @@ -25,8 +27,10 @@ const createVoiceConnection = ( const voiceChannel = client.channels.cache.get(channelId); if (!voiceChannel || !voiceChannel.isVoiceBased()) { - console.error( - `Voice channel ${channelId} not found or is not a voice channel for guild ${guildId}`, + logUnexpectedDiscordAPIResult( + new Error( + `Voice channel ${channelId} not found or is not a voice channel in guild ${guildId}`, + ), ); return; @@ -42,26 +46,28 @@ const createVoiceConnection = ( connection.subscribe(player); connection.on(VoiceConnectionStatus.Disconnected, (oldState, newState) => { - console.log( + logUnexpectedDiscordAPIResult( `Voice disconnected in guild ${guildId}: ${oldState.status} -> ${newState.status}`, ); if (newState.reason !== VoiceConnectionDisconnectReason.Manual) setTimeout(() => { - console.log(`Attempting to reconnect voice in guild ${guildId}...`); + logUnexpectedDiscordAPIResult( + `Attempting to reconnect voice in guild ${guildId} in 5 seconds ...`, + ); createVoiceConnection(client, guildId, channelId); }, 5000); }); connection.on(VoiceConnectionStatus.Destroyed, () => { - console.log( - `Voice connection destroyed in guild ${guildId}, attempting to reconnect...`, + logUnexpectedDiscordAPIResult( + `Voice connection destroyed in guild ${guildId}, attempting to reconnect in 10 seconds ...`, ); setTimeout(() => { createVoiceConnection(client, guildId, channelId); }, 10000); }); connection.on("error", (error) => { - console.error(`Voice connection error for guild ${guildId}:`, error); + logUnexpectedDiscordAPIError(error); const errorCode = (error as any).code; @@ -69,8 +75,8 @@ const createVoiceConnection = ( errorCode === "ENOTFOUND" || errorCode === "UND_ERR_CONNECT_TIMEOUT" ) { - console.log( - `Network error for guild ${guildId}, retrying in 30 seconds...`, + logUnexpectedDiscordAPIResult( + `Network error for guild ${guildId}, retrying in 30 seconds ...`, ); setTimeout(() => { createVoiceConnection(client, guildId, channelId); @@ -82,10 +88,7 @@ const createVoiceConnection = ( } }); } catch (error) { - console.error( - `Failed to connect to voice channel for guild ${guildId}:`, - error, - ); + logUnexpectedDiscordAPIError(error); setTimeout(() => { createVoiceConnection(client, guildId, channelId); }, 10000); @@ -94,10 +97,10 @@ const createVoiceConnection = ( export const handleVoiceConnection = (client: Client) => { process.on("uncaughtException", (error) => { - console.error("Uncaught Exception in voice connection:", error); + logUnexpectedDiscordAPIError(error); }); process.on("unhandledRejection", (reason) => { - console.error("Unhandled Rejection in voice connection:", reason); + logUnexpectedDiscordAPIError(reason); }); for (const [guildId, channelId] of Object.entries(GUILD_VOICE_CHANNELS)) diff --git a/packages/gateway/src/listeners/mediaModeration.ts b/packages/gateway/src/listeners/mediaModeration.ts index 1b63089..f20713a 100644 --- a/packages/gateway/src/listeners/mediaModeration.ts +++ b/packages/gateway/src/listeners/mediaModeration.ts @@ -1,5 +1,6 @@ import { Client, Events, Message } from "discord.js"; import { CENTRAL_GUILD_ID, CENTRAL_ART_MEDIA_CHANNEL_ID } from "../constants"; +import { logUnexpectedDiscordAPIError } from "../utilities"; const MONITORED_CHANNEL_ID = CENTRAL_ART_MEDIA_CHANNEL_ID; @@ -42,15 +43,12 @@ export const handleMediaModeration = (client: Client) => { return; } catch (error) { - console.error("Error processing image with IQDB:", error); + logUnexpectedDiscordAPIError(error); } } } } catch (error) { - console.error( - "Error checking/deleting message with sensitive content:", - error, - ); + logUnexpectedDiscordAPIError(error); } } }); diff --git a/packages/gateway/src/listeners/memberJoin.ts b/packages/gateway/src/listeners/memberJoin.ts index 3c31c69..cceae35 100644 --- a/packages/gateway/src/listeners/memberJoin.ts +++ b/packages/gateway/src/listeners/memberJoin.ts @@ -5,6 +5,8 @@ import { UMA_PERSONAS, } from "../constants"; import { WELCOME_GREETINGS, WELCOME_ENDINGS } from "./messageCreate/constants"; +import { logUnexpectedDiscordAPIError } from "../utilities"; +import { logUnexpectedDiscordAPIResult } from "../../../shared/log"; const CENTRAL_WELCOME_CHANNEL_ID = "1406422619087044670"; const ROLEPLAY_WELCOME_CHANNEL_ID = "1423919140606971927"; @@ -53,10 +55,7 @@ class WelcomeMessageSystem { } } } catch (error) { - console.error( - "Failed to initialize persistent central welcome webhook:", - error, - ); + logUnexpectedDiscordAPIError(error); } try { @@ -84,10 +83,7 @@ class WelcomeMessageSystem { } } } catch (error) { - console.error( - "Failed to initialize persistent roleplay welcome webhook:", - error, - ); + logUnexpectedDiscordAPIError(error); } } @@ -129,8 +125,8 @@ class WelcomeMessageSystem { } if (!webhookClient) { - console.error( - "Failed to send welcome message - webhook not initialized", + logUnexpectedDiscordAPIResult( + `Failed to send welcome message. Webhook not initialised in guild ${member.guild.id} for channel ${channelId}.`, ); return; @@ -151,10 +147,10 @@ class WelcomeMessageSystem { await message.react("✨"); } } catch (error) { - console.error("Failed to react to welcome message:", error); + logUnexpectedDiscordAPIError(error); } } catch (error) { - console.error("Failed to send welcome message:", error); + logUnexpectedDiscordAPIError(error); } } diff --git a/packages/gateway/src/listeners/messageCreate/announcementReaction.ts b/packages/gateway/src/listeners/messageCreate/announcementReaction.ts index 7f590cf..4c8bd74 100644 --- a/packages/gateway/src/listeners/messageCreate/announcementReaction.ts +++ b/packages/gateway/src/listeners/messageCreate/announcementReaction.ts @@ -4,6 +4,7 @@ import { CENTRAL_ROLEPLAY_ANNOUNCEMENTS_CHANNEL_ID, CENTRAL_OKBUDDY_EMOJI_ID, } from "../../constants"; +import { logUnexpectedDiscordAPIError } from "../../utilities"; export const handleAnnouncementReaction = async (message: Message) => { if ( @@ -17,6 +18,6 @@ export const handleAnnouncementReaction = async (message: Message) => { try { await message.react(CENTRAL_OKBUDDY_EMOJI_ID); } catch (error) { - console.error("Failed to add okbuddy reaction to announcement:", error); + logUnexpectedDiscordAPIError(error); } }; diff --git a/packages/gateway/src/listeners/messageCreate/botMentionAutoDelete.ts b/packages/gateway/src/listeners/messageCreate/botMentionAutoDelete.ts index 005c8ea..0b884b7 100644 --- a/packages/gateway/src/listeners/messageCreate/botMentionAutoDelete.ts +++ b/packages/gateway/src/listeners/messageCreate/botMentionAutoDelete.ts @@ -1,5 +1,6 @@ import { Message } from "discord.js"; import { BOT_ID } from "../../constants"; +import { logUnexpectedDiscordAPIError } from "../../utilities"; export function handleBotMentionAutoDelete(message: Message): void { if (!message.guildId) return; @@ -20,7 +21,7 @@ export function handleBotMentionAutoDelete(message: Message): void { try { await message.delete(); } catch (error) { - console.error("Error deleting bot mention message:", error); + logUnexpectedDiscordAPIError(error); } }, 5 * 60 * 1000, diff --git a/packages/gateway/src/listeners/messageCreate/roleplayThumbsUpReaction.ts b/packages/gateway/src/listeners/messageCreate/roleplayThumbsUpReaction.ts index fb8b32b..d46bab1 100644 --- a/packages/gateway/src/listeners/messageCreate/roleplayThumbsUpReaction.ts +++ b/packages/gateway/src/listeners/messageCreate/roleplayThumbsUpReaction.ts @@ -3,6 +3,7 @@ import { ROLEPLAY_ANNOUNCEMENTS_CHANNEL_ID, CENTRAL_SERVER_STAFF_ANNOUNCEMENTS_CHANNEL_ID, } from "../../constants"; +import { logUnexpectedDiscordAPIError } from "../../utilities"; const THUMBS_UP_CHANNELS = [ ROLEPLAY_ANNOUNCEMENTS_CHANNEL_ID, @@ -17,6 +18,6 @@ export const handleRoleplayThumbsUpReaction = async (message: Message) => { try { await message.react("👍"); } catch (error) { - console.error("Failed to add thumbs up reaction to message:", error); + logUnexpectedDiscordAPIError(error); } }; diff --git a/packages/gateway/src/listeners/messageDeletion.ts b/packages/gateway/src/listeners/messageDeletion.ts index 568448b..3398112 100644 --- a/packages/gateway/src/listeners/messageDeletion.ts +++ b/packages/gateway/src/listeners/messageDeletion.ts @@ -8,6 +8,7 @@ import { BOT_ID, DISCORD_AUDIT_LOG_TYPES, } from "../constants"; +import { logUnexpectedDiscordAPIError } from "../utilities"; const GUILD_CHANNEL_MAPPINGS = { [CENTRAL_GUILD_ID]: CENTRAL_MESSAGE_LOG_CHANNEL_ID, @@ -163,7 +164,7 @@ export const handleMessageDeletion = (client: Client) => { await sendAuditLog(client, embed, additionalContent, guildChannelId); } } catch (error) { - console.error("Error logging message deletion:", error); + logUnexpectedDiscordAPIError(error); } }); }; diff --git a/packages/gateway/src/listeners/messageEdit.ts b/packages/gateway/src/listeners/messageEdit.ts index a233463..77639f4 100644 --- a/packages/gateway/src/listeners/messageEdit.ts +++ b/packages/gateway/src/listeners/messageEdit.ts @@ -7,6 +7,7 @@ import { ROLEPLAY_MESSAGE_LOG_CHANNEL_ID, BOT_ID, } from "../constants"; +import { logUnexpectedDiscordAPIError } from "../utilities"; const GUILD_CHANNEL_MAPPINGS = { [CENTRAL_GUILD_ID]: CENTRAL_MESSAGE_LOG_CHANNEL_ID, @@ -119,7 +120,7 @@ export const handleMessageEdit = (client: Client) => { await sendAuditLog(client, embed, undefined, guildChannelId); } } catch (error) { - console.error("Error logging message edit:", error); + logUnexpectedDiscordAPIError(error); } }); }; diff --git a/packages/gateway/src/listeners/roleProtection.ts b/packages/gateway/src/listeners/roleProtection.ts index 2b9f6cc..3bd8805 100644 --- a/packages/gateway/src/listeners/roleProtection.ts +++ b/packages/gateway/src/listeners/roleProtection.ts @@ -5,6 +5,8 @@ import { COLOR_ROLE_IDS, DISCORD_AUDIT_LOG_TYPES, } from "../constants"; +import { logUnexpectedDiscordAPIError } from "../utilities"; +import { logUnexpectedDiscordAPIResult } from "../../../shared/log"; export const handleRoleProtection = (client: Client) => { client.on(Events.GuildMemberUpdate, async (oldMember, newMember) => { @@ -22,7 +24,7 @@ export const handleRoleProtection = (client: Client) => { ); if (!protectedRole) { - console.error("Protected role not found"); + logUnexpectedDiscordAPIResult(new Error("Protected role not found")); return; } @@ -54,38 +56,28 @@ export const handleRoleProtection = (client: Client) => { if (relevantLog && relevantLog.executor?.id === guildOwner.id) { continue; } - - console.warn( - `Unauthorized high role assignment: ${role.name} (${role.id}) to ${newMember.user.tag} (${newMember.id})`, + logUnexpectedDiscordAPIResult( + `Unauthorised high role assignment: ${role.name} (${role.id}) to ${newMember.user.tag} (${newMember.id})`, ); try { await newMember.roles.remove(role); } catch (error) { - console.error( - `Failed to remove high role ${role.name} from ${newMember.user.tag}:`, - error, - ); + logUnexpectedDiscordAPIError(error); } } catch (auditError) { - console.error("Error checking audit log:", auditError); - console.warn( - `High role ${role.name} (${role.id}) added to ${newMember.user.tag} (${newMember.id}), removing due to audit log error`, - ); + logUnexpectedDiscordAPIError(auditError); try { await newMember.roles.remove(role); } catch (error) { - console.error( - `Failed to remove high role ${role.name} from ${newMember.user.tag}:`, - error, - ); + logUnexpectedDiscordAPIError(error); } } } } } catch (error) { - console.error("Error in role protection handler:", error); + logUnexpectedDiscordAPIError(error); } }); }; diff --git a/packages/gateway/src/utilities.ts b/packages/gateway/src/utilities.ts index d5aebeb..3ca951b 100644 --- a/packages/gateway/src/utilities.ts +++ b/packages/gateway/src/utilities.ts @@ -1,4 +1,7 @@ import { Message } from "discord.js"; +import { logUnexpectedDiscordAPIError } from "../../shared/log"; + +export { log, LogLevel, logUnexpectedDiscordAPIError } from "../../shared/log"; export const replyWithCleanup = async ( message: Message, @@ -12,10 +15,10 @@ export const replyWithCleanup = async ( try { await reply.delete(); } catch (error) { - console.error("Error deleting cleanup reply:", error); + logUnexpectedDiscordAPIError(error); } }, cleanupTimeMs); } catch (error) { - console.error("Error sending cleanup reply:", error); + logUnexpectedDiscordAPIError(error); } }; diff --git a/packages/shared/log.ts b/packages/shared/log.ts index 965f7f5..04c516e 100644 --- a/packages/shared/log.ts +++ b/packages/shared/log.ts @@ -39,3 +39,10 @@ export const logUnexpectedDiscordAPIError = (error: unknown) => { LogLevel.Error, ); }; + +export const logUnexpectedDiscordAPIResult = (error: unknown) => { + log( + `Unexpected Discord API result: ${error instanceof Error ? error.message : String(error)}`, + LogLevel.Error, + ); +}; |