Skip to content

Commit

Permalink
More partials for reactions, add removeUserReaction util function
Browse files Browse the repository at this point in the history
  • Loading branch information
brandoningli committed Jul 16, 2023
1 parent f7db863 commit 28a7ab7
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 1 deletion.
5 changes: 4 additions & 1 deletion src/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ const bot = new Client({
GatewayIntentBits.DirectMessages,
GatewayIntentBits.DirectMessageReactions,
],
partials: [Partials.Channel],
partials: [Partials.Message, Partials.Channel, Partials.Reaction, Partials.User],
});

bot.on("ready", async () => {
Expand Down Expand Up @@ -211,6 +211,9 @@ bot.on("messageReactionAdd", async (reaction, user) => {
// If the message this reaction belongs to was removed, the fetching might result in an API error which should be handled
try {
await reaction.fetch();
if (reaction.message.partial) {
await reaction.message.fetch();
}
} catch (error) {
utils.logger.error(
`Something went wrong when fetching the message: ${error}`
Expand Down
12 changes: 12 additions & 0 deletions src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,17 @@ function createEmbed(options = DEFAULT_EMBED) {
.addFields(...options["additionalFields"]);
}

async function removeUserReaction(message, user, emojiName) {
const reactions = message.reactions.cache.filter(r => r.users.cache.has(user.id) && r.emoji.name === emojiName);
for (const r of reactions.values()) {
try {
await r.users.remove(user.id)
} catch (e) {
// Do nothing
}
}
}

module.exports = {
SECOND,
MINUTE,
Expand All @@ -195,4 +206,5 @@ module.exports = {
createEmbed,
logger,
plugins,
removeUserReaction
};

0 comments on commit 28a7ab7

Please sign in to comment.