import { Client, Events, Message } from "discord.js"; import { GUILD_ID } from "../constants"; export const handleSayCommand = (client: Client) => { client.on(Events.MessageCreate, async (message: Message) => { if (message.author.bot) return; if (message.content.toLowerCase().startsWith("uma!say")) { const application = await 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 message.reply( "❌ Usage: `uma!say `\nExamples:\n- `uma!say #general Hello everyone!`\n- `uma!say 1234567890123456789 Thanks for the info!`", ); return; } const firstParameter = parameters[0]; const messageContent = parameters.slice(1).join(" "); let targetChannel: any; let targetMessage: any = null; const messageIdMatch = firstParameter.match(/^\d{17,19}$/); if (messageIdMatch) { try { const guild = client.guilds.cache.get(GUILD_ID); if (!guild) { await message.reply("❌ Guild not found."); return; } let foundMessage = null; for (const channel of guild.channels.cache.values()) { if (channel.isTextBased()) { try { foundMessage = await channel.messages.fetch(firstParameter); if (foundMessage) { targetChannel = channel; targetMessage = foundMessage; break; } } catch { continue; } } } if (!foundMessage) { await message.reply("❌ Message not found."); return; } } catch { await message.reply("❌ Error finding message."); return; } } else { const channelMatch = firstParameter.match(/<#(\d+)>/); if (!channelMatch) { await message.reply( "❌ Please mention a channel or provide a message ID. Example: `#general` or `1234567890123456789`", ); return; } const channelId = channelMatch[1]; targetChannel = client.channels.cache.get(channelId); if (!targetChannel || !targetChannel.isTextBased()) { await message.reply("❌ Channel not found or is not a text channel."); 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 message.reply( "❌ Failed to execute the say command. Please check permissions.", ); } catch (replyError) { console.error("Failed to send error reply:", replyError); } } } }); };