From e6dae3ae487428a0f619c11cedc3f8a94fbed81f Mon Sep 17 00:00:00 2001 From: Harry Date: Fri, 20 Dec 2024 18:02:28 -0500 Subject: [PATCH] MessageReactionAdd.ts MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Pins a message when a partner adds a 📌 reaction to it Signed-off-by: Harry --- discord/events/MessageReactionAdd.ts | 38 ++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 discord/events/MessageReactionAdd.ts diff --git a/discord/events/MessageReactionAdd.ts b/discord/events/MessageReactionAdd.ts new file mode 100644 index 0000000..c537207 --- /dev/null +++ b/discord/events/MessageReactionAdd.ts @@ -0,0 +1,38 @@ +import DiscordEvent from "../../util/DiscordEvent"; +import { MessageReaction, PartialMessageReaction, PartialUser, User, Events } from "discord.js"; +import MemberUtil from "../../util/MemberUtil"; + +const PUSH_PIN_EMOJI = "📌"; // Unicode + +export default class MessageReactionAdd extends DiscordEvent { + constructor(client) { + super(Events.MessageReactionAdd, client); + } + + public async execute(reaction: MessageReaction | PartialMessageReaction, user: User | PartialUser): Promise { + try { + // Fetch partials if necessary + if (reaction.partial) await reaction.fetch(); + + // Condensed check for guild existence and emoji name + if (!reaction.message.guild || reaction.emoji.name !== PUSH_PIN_EMOJI) return; + + const guild = reaction.message.guild; + const member = await guild.members.fetch(user.id); + + // Combine all partner roles from PartnerDiscordRoleMap + const allPartnerRoles = Object.values(MemberUtil.PartnerDiscordRoleMap).flat(); + + // Check if the user has any of the partner roles + if (!member.roles.cache.some(role => allPartnerRoles.includes(role.id))) return; + + // Attempt to pin the message + await reaction.message.pin(); + const channel = reaction.message.channel; + await channel.send(`Pinned message: ${reaction.message.id}`); + } catch (error) { + const channel = reaction.message.channel; + await channel.send(`Error pinning message: ${error}`); + } + } +}