import { Message } from "discord.js"; import { replyWithCleanup } from "../utilities"; export const handleSayCommand = async (message: Message) => { if (message.author.bot) return; if ((message as any).saycHandled) return; if (message.content.toLowerCase().startsWith("uma!say")) { const application = await message.client.application?.fetch(); const ownerId = application?.owner?.id; if (message.author.id !== ownerId) return; const parameters = message.content.split(" ").slice(1); if (parameters.length < 2) { await replyWithCleanup( message, "❌ Usage: `uma!say [message_id] `\nExamples:\n- `uma!say 1234567890123456789 Hello everyone!`\n- `uma!say 1234567890123456789 9876543210987654321 Thanks for the info!`", ); return; } const firstParameter = parameters[0]; const secondParameter = parameters[1]; const messageContent = parameters.slice(2).join(" "); let targetChannel: any; let targetMessage: any = null; const channelIdMatch = firstParameter.match(/^\d{17,19}$/); if (!channelIdMatch) { await replyWithCleanup( message, "❌ First parameter must be a channel ID (17-19 digits). Example: `1234567890123456789`", ); return; } targetChannel = message.client.channels.cache.get(firstParameter); if (!targetChannel || !targetChannel.isTextBased()) { await replyWithCleanup( message, "❌ Channel not found or is not a text channel.", ); return; } const messageIdMatch = secondParameter?.match(/^\d{17,19}$/); if (messageIdMatch) { try { targetMessage = await targetChannel.messages.fetch(secondParameter); } catch { await replyWithCleanup( message, "❌ Message not found in the specified channel.", ); return; } } if (!messageIdMatch && parameters.length < 2) { await replyWithCleanup( message, "❌ Usage: `uma!say [message_id] `\nExamples:\n- `uma!say 1234567890123456789 Hello everyone!`\n- `uma!say 1234567890123456789 9876543210987654321 Thanks for the info!`", ); return; } if (messageIdMatch && parameters.length < 3) { await replyWithCleanup( message, "❌ When providing a message ID, you need at least 3 parameters: channel_id, message_id, and message content.", ); return; } try { await message.delete(); const baseDuration = Math.max(1, messageContent.length / 20); const complexityMultiplier = (messageContent.match(/[.!?]/g) || []).length * 0.5; const wordCount = messageContent.split(" ").length; const wordComplexityMultiplier = Math.min(wordCount / 10, 2); const typingDuration = Math.min( baseDuration + complexityMultiplier + wordComplexityMultiplier, 8, ); await (targetChannel as any).sendTyping(); await new Promise((resolve) => setTimeout(resolve, typingDuration * 1000), ); if (targetMessage) { await targetMessage.reply(messageContent); } else { await (targetChannel as any).send(messageContent); } } catch (error) { console.error("Error executing say command:", error); try { await replyWithCleanup( message, "❌ Failed to execute the say command. Please check permissions.", ); } catch (replyError) { console.error("Failed to send error reply:", replyError); } } } };