import { Message } from "discord.js"; import { 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 ` or `uma!react `\nExamples:\n- `uma!react 1234567890123456789 👍` (current channel)\n- `uma!react #general 1234567890123456789 👍` (specific channel)\n- `uma!react 9876543210987654321 1234567890123456789 👍` (channel by ID)", ); return; } let targetChannel: any; let messageId: string; let emoji: 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 `", ); return; } const channelId = channelMatch ? channelMatch[1] : parameters[1]; messageId = parameters[2]; emoji = parameters[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]; emoji = parameters[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 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); await replyWithCleanup( message, `✅ Removed reaction ${emoji} from message in ${targetChannel}`, ); } else { await targetMessage.react(emoji); await replyWithCleanup( message, `✅ Reacted with ${emoji} to message in ${targetChannel}`, ); } } catch (error) { console.error("Error toggling reaction:", error); await replyWithCleanup( message, "❌ Failed to toggle reaction on the message.", ); } };