From 6f561f3b4458ff5cc4a597e824cfb6e8f7253c99 Mon Sep 17 00:00:00 2001 From: Harry Date: Fri, 20 Dec 2024 02:12:27 -0500 Subject: [PATCH 1/6] MessagePinReactionHandler.ts /** * Handles both 'messageReactionAdd' and 'messageReactionRemove' events. * When a reaction is added or removed: * - If the reaction matches the specified emoji and the user has the required role, * the message will be pinned or unpinned in the channel accordingly. */ Signed-off-by: Harry --- discord/events/MessagePinReactionHandler.ts | 66 +++++++++++++++++++++ 1 file changed, 66 insertions(+) create mode 100644 discord/events/MessagePinReactionHandler.ts diff --git a/discord/events/MessagePinReactionHandler.ts b/discord/events/MessagePinReactionHandler.ts new file mode 100644 index 0000000..d11e52e --- /dev/null +++ b/discord/events/MessagePinReactionHandler.ts @@ -0,0 +1,66 @@ +import DiscordEvent from "../../util/DiscordEvent"; +import { Client, MessageReaction, PartialMessageReaction, PartialUser, User, Events } from "discord.js"; + +const ROLE_ID = '446104438969466890'; // Allowed role ID +const PUSH_PIN_EMOJI = '📌'; // Unicode + +export default class MessageReactionHandler extends DiscordEvent { + constructor(client: Client) { + super(Events.MessageReactionAdd, client); // Register the "messageReactionAdd" event + client.on(Events.MessageReactionRemove, this.handleRemove.bind(this)); // Register "messageReactionRemove" within the same class + } + + public async execute(reaction: MessageReaction | PartialMessageReaction, user: User | PartialUser): Promise { + // Handle reaction add (pinning a message) + if (reaction.partial) { + try { + await reaction.fetch(); + } catch (error) { + console.error('Error fetching reaction:', error); + return; + } + } + + if (reaction.emoji.name !== PUSH_PIN_EMOJI) return; + + const guild = reaction.message.guild; + if (!guild) return; + + const member = await guild.members.fetch(user.id); + if (!member.roles.cache.has(ROLE_ID)) return; + + try { + await reaction.message.pin(); + console.log(`Pinned message: ${reaction.message.id}`); + } catch (error) { + console.error('Failed to pin message:', error); + } + } + + private async handleRemove(reaction: MessageReaction | PartialMessageReaction, user: User | PartialUser): Promise { + // Handle reaction remove (unpinning a message) + if (reaction.partial) { + try { + await reaction.fetch(); + } catch (error) { + console.error('Error fetching reaction:', error); + return; + } + } + + if (reaction.emoji.name !== PUSH_PIN_EMOJI) return; + + const guild = reaction.message.guild; + if (!guild) return; + + const member = await guild.members.fetch(user.id); + if (!member.roles.cache.has(ROLE_ID)) return; + + try { + await reaction.message.unpin(); + console.log(`Unpinned message: ${reaction.message.id}`); + } catch (error) { + console.error('Failed to unpin message:', error); + } + } +} -- 2.20.1 From b37ac915c2e8b111bbb50d6020bc3881683fe1b5 Mon Sep 17 00:00:00 2001 From: Harry Date: Fri, 20 Dec 2024 17:54:35 -0500 Subject: [PATCH 2/6] MessageReactionAdd.ts MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Pins messages in a channel when a partner adds a 📌 reaction Signed-off-by: Harry --- MessageReactionAdd.ts | 38 ++++++++++++ discord/events/MessagePinReactionHandler.ts | 66 --------------------- 2 files changed, 38 insertions(+), 66 deletions(-) create mode 100644 MessageReactionAdd.ts delete mode 100644 discord/events/MessagePinReactionHandler.ts diff --git a/MessageReactionAdd.ts b/MessageReactionAdd.ts new file mode 100644 index 0000000..c537207 --- /dev/null +++ b/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}`); + } + } +} diff --git a/discord/events/MessagePinReactionHandler.ts b/discord/events/MessagePinReactionHandler.ts deleted file mode 100644 index d11e52e..0000000 --- a/discord/events/MessagePinReactionHandler.ts +++ /dev/null @@ -1,66 +0,0 @@ -import DiscordEvent from "../../util/DiscordEvent"; -import { Client, MessageReaction, PartialMessageReaction, PartialUser, User, Events } from "discord.js"; - -const ROLE_ID = '446104438969466890'; // Allowed role ID -const PUSH_PIN_EMOJI = '📌'; // Unicode - -export default class MessageReactionHandler extends DiscordEvent { - constructor(client: Client) { - super(Events.MessageReactionAdd, client); // Register the "messageReactionAdd" event - client.on(Events.MessageReactionRemove, this.handleRemove.bind(this)); // Register "messageReactionRemove" within the same class - } - - public async execute(reaction: MessageReaction | PartialMessageReaction, user: User | PartialUser): Promise { - // Handle reaction add (pinning a message) - if (reaction.partial) { - try { - await reaction.fetch(); - } catch (error) { - console.error('Error fetching reaction:', error); - return; - } - } - - if (reaction.emoji.name !== PUSH_PIN_EMOJI) return; - - const guild = reaction.message.guild; - if (!guild) return; - - const member = await guild.members.fetch(user.id); - if (!member.roles.cache.has(ROLE_ID)) return; - - try { - await reaction.message.pin(); - console.log(`Pinned message: ${reaction.message.id}`); - } catch (error) { - console.error('Failed to pin message:', error); - } - } - - private async handleRemove(reaction: MessageReaction | PartialMessageReaction, user: User | PartialUser): Promise { - // Handle reaction remove (unpinning a message) - if (reaction.partial) { - try { - await reaction.fetch(); - } catch (error) { - console.error('Error fetching reaction:', error); - return; - } - } - - if (reaction.emoji.name !== PUSH_PIN_EMOJI) return; - - const guild = reaction.message.guild; - if (!guild) return; - - const member = await guild.members.fetch(user.id); - if (!member.roles.cache.has(ROLE_ID)) return; - - try { - await reaction.message.unpin(); - console.log(`Unpinned message: ${reaction.message.id}`); - } catch (error) { - console.error('Failed to unpin message:', error); - } - } -} -- 2.20.1 From 9374a1bca6d2a6acdcc185977d29dde37589ad88 Mon Sep 17 00:00:00 2001 From: Harry Date: Fri, 20 Dec 2024 18:00:58 -0500 Subject: [PATCH 3/6] MessageReactionRemove.ts MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Unpins a message when a partner removes the 📌 reaction from it Signed-off-by: Harry --- discord/events/MessageReactionRemove.ts | 38 +++++++++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 discord/events/MessageReactionRemove.ts diff --git a/discord/events/MessageReactionRemove.ts b/discord/events/MessageReactionRemove.ts new file mode 100644 index 0000000..0323297 --- /dev/null +++ b/discord/events/MessageReactionRemove.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 MessageReactionRemove extends DiscordEvent { + constructor(client) { + super(Events.MessageReactionRemove, 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 unpin the message + await reaction.message.unpin(); + const channel = reaction.message.channel; + await channel.send(`Unpinned message: ${reaction.message.id}`); + } catch (error) { + const channel = reaction.message.channel; + await channel.send(`Error unpinning message: ${error}`); + } + } +} -- 2.20.1 From e6dae3ae487428a0f619c11cedc3f8a94fbed81f Mon Sep 17 00:00:00 2001 From: Harry Date: Fri, 20 Dec 2024 18:02:28 -0500 Subject: [PATCH 4/6] 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}`); + } + } +} -- 2.20.1 From 65f4bd92dcb8a7e64183ed68f6d5f6501f6ee41d Mon Sep 17 00:00:00 2001 From: Harry Date: Fri, 20 Dec 2024 20:33:31 -0500 Subject: [PATCH 5/6] Update discord/events/MessageReactionAdd.ts switched error messages to Ephemeral: true Signed-off-by: Harry --- discord/events/MessageReactionAdd.ts | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/discord/events/MessageReactionAdd.ts b/discord/events/MessageReactionAdd.ts index c537207..e518c55 100644 --- a/discord/events/MessageReactionAdd.ts +++ b/discord/events/MessageReactionAdd.ts @@ -28,11 +28,17 @@ export default class MessageReactionAdd extends DiscordEvent { // Attempt to pin the message await reaction.message.pin(); - const channel = reaction.message.channel; - await channel.send(`Pinned message: ${reaction.message.id}`); + console.log(`Pinned message: ${reaction.message.id}`); } catch (error) { - const channel = reaction.message.channel; - await channel.send(`Error pinning message: ${error}`); + try { + const dmChannel = await user.createDM(); + await dmChannel.send({ + content: `There was an error pinning the message: ${error.message}`, + ephemeral: true, + }); + } catch (dmError) { + console.error(`Failed to send ephemeral error message to ${user.tag}: ${dmError}`); + } } } } -- 2.20.1 From 54845033f1d4bf29aff6f8bd73b079c83c58c2e6 Mon Sep 17 00:00:00 2001 From: Harry Date: Fri, 20 Dec 2024 20:34:31 -0500 Subject: [PATCH 6/6] Update discord/events/MessageReactionRemove.ts Switched error messages to ephermal: true Signed-off-by: Harry --- discord/events/MessageReactionRemove.ts | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/discord/events/MessageReactionRemove.ts b/discord/events/MessageReactionRemove.ts index 0323297..f16135c 100644 --- a/discord/events/MessageReactionRemove.ts +++ b/discord/events/MessageReactionRemove.ts @@ -28,11 +28,17 @@ export default class MessageReactionRemove extends DiscordEvent { // Attempt to unpin the message await reaction.message.unpin(); - const channel = reaction.message.channel; - await channel.send(`Unpinned message: ${reaction.message.id}`); + console.log(`Unpinned message: ${reaction.message.id}`); } catch (error) { - const channel = reaction.message.channel; - await channel.send(`Error unpinning message: ${error}`); + try { + const dmChannel = await user.createDM(); + await dmChannel.send({ + content: `There was an error unpinning the message: ${error.message}`, + ephemeral: true, + }); + } catch (dmError) { + console.error(`Failed to send ephemeral error message to ${user.tag}: ${dmError}`); + } } } } -- 2.20.1