1
0
Fork 0

MessageReactionAdd.ts

Pins messages in a channel when a partner adds a 📌 reaction

Signed-off-by: Harry <harry@harryrosestudios.com>
master
Harry 2024-12-20 17:54:35 -05:00
parent 6f561f3b44
commit b37ac915c2
2 changed files with 38 additions and 66 deletions

38
MessageReactionAdd.ts Normal file
View File

@ -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<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 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}`);
}
}
}

View File

@ -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<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);
}
}
}