From f6b78c3b5b69ae4238be1fec0358774d63ac5a5f Mon Sep 17 00:00:00 2001 From: Fuwn Date: Sun, 28 Sep 2025 20:15:18 -0700 Subject: feat(commands): Clean up bot replies --- packages/gateway/src/commands/delete.ts | 9 +++++++-- packages/gateway/src/commands/pin.ts | 32 ++++++++++++++++++++++++-------- packages/gateway/src/commands/react.ts | 11 +++++++---- packages/gateway/src/commands/say.ts | 21 ++++++++++++++------- packages/gateway/src/utilities.ts | 21 +++++++++++++++++++++ 5 files changed, 73 insertions(+), 21 deletions(-) create mode 100644 packages/gateway/src/utilities.ts (limited to 'packages/gateway') diff --git a/packages/gateway/src/commands/delete.ts b/packages/gateway/src/commands/delete.ts index 8b36eb9..20d0881 100644 --- a/packages/gateway/src/commands/delete.ts +++ b/packages/gateway/src/commands/delete.ts @@ -1,4 +1,5 @@ import { Message } from "discord.js"; +import { replyWithCleanup } from "../utilities"; export const handleDeleteCommand = async (message: Message) => { if (message.author.bot) return; @@ -8,7 +9,10 @@ export const handleDeleteCommand = async (message: Message) => { const ownerId = application?.owner?.id; if (message.author.id !== ownerId) { - await message.reply("❌ Only the server owner can use this command."); + await replyWithCleanup( + message, + "❌ Only the server owner can use this command.", + ); return; } @@ -16,7 +20,8 @@ export const handleDeleteCommand = async (message: Message) => { const parameters = message.content.split(" ").slice(1); if (parameters.length < 1) { - await message.reply( + await replyWithCleanup( + message, "❌ Usage: `uma!delete [channel_id]`\nExamples:\n- `uma!delete 1234567890123456789` (current channel)\n- `uma!delete 1234567890123456789 9876543210987654321` (specific channel)", ); diff --git a/packages/gateway/src/commands/pin.ts b/packages/gateway/src/commands/pin.ts index da89cf1..4980d43 100644 --- a/packages/gateway/src/commands/pin.ts +++ b/packages/gateway/src/commands/pin.ts @@ -1,4 +1,5 @@ import { Message } from "discord.js"; +import { replyWithCleanup } from "../utilities"; export const handlePinCommand = async (message: Message) => { if (message.author.bot) return; @@ -13,7 +14,7 @@ export const handlePinCommand = async (message: Message) => { const parameters = message.content.split(" "); if (parameters.length < 2) { - await message.reply("❌ Usage: `uma!pin `"); + await replyWithCleanup(message, "❌ Usage: `uma!pin `"); return; } @@ -24,13 +25,13 @@ export const handlePinCommand = async (message: Message) => { const targetMessage = await message.channel.messages.fetch(messageId); if (!targetMessage) { - await message.reply("❌ Message not found."); + await replyWithCleanup(message, "❌ Message not found."); return; } if (targetMessage.pinned) { - await message.reply("❌ Message is already pinned."); + await replyWithCleanup(message, "❌ Message is already pinned."); return; } @@ -40,12 +41,27 @@ export const handlePinCommand = async (message: Message) => { } catch (error) { console.error("Error pinning message:", error); - if (error instanceof Error && error.message.includes("Missing Permissions")) { - await message.reply("❌ Missing permissions to pin messages in this channel."); - } else if (error instanceof Error && error.message.includes("Maximum number of pins")) { - await message.reply("❌ Channel has reached maximum number of pinned messages (50). Unpin another message first."); + if ( + error instanceof Error && + error.message.includes("Missing Permissions") + ) { + await replyWithCleanup( + message, + "❌ Missing permissions to pin messages in this channel.", + ); + } else if ( + error instanceof Error && + error.message.includes("Maximum number of pins") + ) { + await replyWithCleanup( + message, + "❌ Channel has reached maximum number of pinned messages (50). Unpin another message first.", + ); } else { - await message.reply("❌ Failed to pin the message. Message ID may be invalid."); + await replyWithCleanup( + message, + "❌ Failed to pin the message. Message ID may be invalid.", + ); } } }; diff --git a/packages/gateway/src/commands/react.ts b/packages/gateway/src/commands/react.ts index d8a8021..84dce37 100644 --- a/packages/gateway/src/commands/react.ts +++ b/packages/gateway/src/commands/react.ts @@ -1,4 +1,5 @@ import { Message } from "discord.js"; +import { replyWithCleanup } from "../utilities"; export const handleReactCommand = async (message: Message) => { if (message.author.bot) return; @@ -10,7 +11,10 @@ export const handleReactCommand = async (message: Message) => { const parameters = message.content.split(" "); if (parameters.length < 3) { - await message.reply("Usage: `uma!react `"); + await replyWithCleanup( + message, + "❌ Usage: `uma!react `", + ); return; } @@ -22,7 +26,7 @@ export const handleReactCommand = async (message: Message) => { const targetMessage = await message.channel.messages.fetch(messageId); if (!targetMessage) { - await message.reply("Message not found."); + await replyWithCleanup(message, "❌ Message not found."); return; } @@ -30,7 +34,6 @@ export const handleReactCommand = async (message: Message) => { await targetMessage.react(emoji); } catch (error) { console.error("Error reacting to message:", error); - - await message.reply("Failed to react to the message."); + await replyWithCleanup(message, "❌ Failed to react to the message."); } }; diff --git a/packages/gateway/src/commands/say.ts b/packages/gateway/src/commands/say.ts index ec19a33..8a9ab5a 100644 --- a/packages/gateway/src/commands/say.ts +++ b/packages/gateway/src/commands/say.ts @@ -1,5 +1,6 @@ import { Message } from "discord.js"; import { GUILD_ID } from "../constants"; +import { replyWithCleanup } from "../utilities"; export const handleSayCommand = async (message: Message) => { if (message.author.bot) return; @@ -13,7 +14,8 @@ export const handleSayCommand = async (message: Message) => { const parameters = message.content.split(" ").slice(1); if (parameters.length < 2) { - await message.reply( + await replyWithCleanup( + message, "❌ Usage: `uma!say `\nExamples:\n- `uma!say #general Hello everyone!`\n- `uma!say 1234567890123456789 Thanks for the info!`", ); @@ -31,7 +33,7 @@ export const handleSayCommand = async (message: Message) => { const guild = message.client.guilds.cache.get(GUILD_ID); if (!guild) { - await message.reply("❌ Guild not found."); + await replyWithCleanup(message, "❌ Guild not found."); return; } @@ -56,12 +58,12 @@ export const handleSayCommand = async (message: Message) => { } if (!foundMessage) { - await message.reply("❌ Message not found."); + await replyWithCleanup(message, "❌ Message not found."); return; } } catch { - await message.reply("❌ Error finding message."); + await replyWithCleanup(message, "❌ Error finding message."); return; } @@ -69,7 +71,8 @@ export const handleSayCommand = async (message: Message) => { const channelMatch = firstParameter.match(/<#(\d+)>/); if (!channelMatch) { - await message.reply( + await replyWithCleanup( + message, "❌ Please mention a channel or provide a message ID. Example: `#general` or `1234567890123456789`", ); @@ -81,7 +84,10 @@ export const handleSayCommand = async (message: Message) => { targetChannel = message.client.channels.cache.get(channelId); if (!targetChannel || !targetChannel.isTextBased()) { - await message.reply("❌ Channel not found or is not a text channel."); + await replyWithCleanup( + message, + "❌ Channel not found or is not a text channel.", + ); return; } @@ -114,7 +120,8 @@ export const handleSayCommand = async (message: Message) => { console.error("Error executing say command:", error); try { - await message.reply( + await replyWithCleanup( + message, "❌ Failed to execute the say command. Please check permissions.", ); } catch (replyError) { diff --git a/packages/gateway/src/utilities.ts b/packages/gateway/src/utilities.ts new file mode 100644 index 0000000..d5aebeb --- /dev/null +++ b/packages/gateway/src/utilities.ts @@ -0,0 +1,21 @@ +import { Message } from "discord.js"; + +export const replyWithCleanup = async ( + message: Message, + content: string, + cleanupTimeMs: number = 10000, +): Promise => { + try { + const reply = await message.reply(content); + + setTimeout(async () => { + try { + await reply.delete(); + } catch (error) { + console.error("Error deleting cleanup reply:", error); + } + }, cleanupTimeMs); + } catch (error) { + console.error("Error sending cleanup reply:", error); + } +}; -- cgit v1.2.3