1
0
Fork 0
crv2-MessagePinReactionHand.../discord/events/MessageReactionRemove.ts

45 lines
1.6 KiB
TypeScript
Raw Normal View History

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<void> {
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();
console.log(`Unpinned message: ${reaction.message.id}`);
} catch (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}`);
}
}
}
}