forked from engineering/crv2
67 lines
2.1 KiB
TypeScript
67 lines
2.1 KiB
TypeScript
|
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<void> {
|
||
|
// 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<void> {
|
||
|
// 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);
|
||
|
}
|
||
|
}
|
||
|
}
|