import { Message } from "discord.js"; import { logUnexpectedDiscordAPIError, replyWithCleanup } from "../utilities"; export const handleReactCommand = async (message: Message) => { if (message.author.bot) return; if (!message.content.startsWith("uma!react")) return; const application = await message.client.application?.fetch(); const ownerId = application?.owner?.id; if (message.author.id !== ownerId) return; const parameters = message.content.split(" "); if (parameters.length < 3) { await replyWithCleanup( message, "❌ Usage: `uma!react [emoji2] [emoji3] ...` or `uma!react [emoji2] [emoji3] ...`\nExamples:\n- `uma!react 1234567890123456789 👍` (current channel)\n- `uma!react 1234567890123456789 👍 ❤️ 🎉` (multiple emojis)\n- `uma!react 1234567890123456789 🇭 🇪 🇱 🇵` (spell out word)\n- `uma!react #general 1234567890123456789 👍` (specific channel)\n- `uma!react 9876543210987654321 1234567890123456789 👍` (channel by ID)", ); return; } let targetChannel: any; let messageId: string; let emojis: string[]; const channelMatch = parameters[1].match(/<#(\d+)>/); const channelIdMatch = parameters[1].match(/^\d{17,19}$/); if (channelMatch || channelIdMatch) { if (parameters.length < 4) { await replyWithCleanup( message, "❌ Usage: `uma!react [emoji2] [emoji3] ...`", ); return; } const channelId = channelMatch ? channelMatch[1] : parameters[1]; messageId = parameters[2]; emojis = parameters.slice(3); try { targetChannel = await message.client.channels.fetch(channelId); } catch { await replyWithCleanup( message, "❌ Channel not found or not accessible.", ); return; } } else { targetChannel = message.channel; messageId = parameters[1]; emojis = parameters.slice(2); } if (!targetChannel || !targetChannel.isTextBased()) { await replyWithCleanup(message, "❌ Target channel is not a text channel."); return; } try { const targetMessage = await targetChannel.messages.fetch(messageId); if (!targetMessage) { await replyWithCleanup(message, "❌ Message not found."); return; } const results: string[] = []; const errors: string[] = []; for (const emoji of emojis) { try { const existingReaction = targetMessage.reactions.cache.get(emoji); const botReacted = existingReaction?.users.cache.has( message.client.user?.id || "", ); if (botReacted) { await targetMessage.reactions.cache .get(emoji) ?.users.remove(message.client.user?.id); results.push(`Removed ${emoji}`); } else { await targetMessage.react(emoji); results.push(`Added ${emoji}`); } } catch (error) { logUnexpectedDiscordAPIError(error); errors.push(emoji); } } let resultMessage = `✅ Processed ${emojis.length} emoji(s) in ${targetChannel}:\n`; if (results.length > 0) resultMessage += results.join(", ") + "\n"; if (errors.length > 0) resultMessage += `❌ Failed to process: ${errors.join(", ")}`; await replyWithCleanup(message, resultMessage); } catch (error) { logUnexpectedDiscordAPIError(error); await replyWithCleanup( message, "❌ Failed to toggle reactions on the message.", ); } };