discord-starboard-plus
Version:
Discord Starboard Plus: A clean, maintainable starboard system for Discord.js bots. Features per-guild configuration, TypeScript support. Highlight your community's favorite messages with customizable starboards.
101 lines • 3.81 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.ReactionRemoveHandler = void 0;
const BaseHandler_1 = require("./BaseHandler");
/**
* Handler for messageReactionRemove events.
* Updates or removes starboard messages when reactions are removed.
*/
class ReactionRemoveHandler extends BaseHandler_1.BaseHandler {
/**
* Handle the messageReactionRemove event.
*/
async handle(reaction, _user) {
try {
const message = reaction.message;
if (!message.guild)
return;
const options = this.getOptionsForGuild(message.guild.id);
const emojiMatch = reaction.emoji.name === options.starEmoji ||
reaction.emoji.toString() === options.starEmoji;
if (!emojiMatch) {
return;
}
await this.processReactionRemoval(reaction, message, options);
}
catch (error) {
this.logger.error('Error handling reaction remove', error, {
messageId: reaction.message?.id
});
}
}
/**
* Process reaction removal.
*/
async processReactionRemoval(reaction, message, options) {
const starboardChannel = this.validation.getStarboardChannel(message.guild.id, options, this.client);
if (!starboardChannel)
return;
const searchResult = await this.messageSearch.findStarboardMessage(starboardChannel, message.id, options.maxSearchDepth);
if (!searchResult.found || !searchResult.message) {
return;
}
const reactionCount = await this.validation.getValidReactionCount(reaction, options);
if (reactionCount < options.requiredReactions) {
await this.removeFromStarboard(starboardChannel, searchResult.message);
}
else {
await this.updateCount(searchResult.message, reactionCount, options);
}
}
/**
* Remove a message from the starboard.
*/
async removeFromStarboard(_channel, starboardMessage) {
try {
await starboardMessage.delete();
this.logger.info('Starboard message removed due to insufficient reactions', {
starboardId: starboardMessage.id
});
}
catch (error) {
if (error.code === 10008) {
this.logger.warn('Starboard message already deleted', {
starboardId: starboardMessage.id
});
return;
}
this.logger.error('Failed to remove starboard message', error, {
starboardId: starboardMessage.id
});
}
}
/**
* Update the reaction count on an existing starboard message.
*/
async updateCount(starboardMessage, reactionCount, options) {
if (!options.updateOnReaction)
return;
try {
const newContent = this.embedBuilder.updateReactionCount(starboardMessage.content, reactionCount);
await starboardMessage.edit({ content: newContent });
this.logger.info('Starboard message count updated', {
starboardId: starboardMessage.id,
newCount: reactionCount
});
}
catch (error) {
if (error.code === 10008) {
this.logger.warn('Starboard message was deleted externally', {
starboardId: starboardMessage.id
});
return;
}
this.logger.error('Failed to update starboard count', error, {
starboardId: starboardMessage.id
});
}
}
}
exports.ReactionRemoveHandler = ReactionRemoveHandler;
//# sourceMappingURL=ReactionRemoveHandler.js.map