Finishing Reaction Logic (2/2)
The final reaction logic and handling.
I've split this part, based on the comments before, into pre, a, b, c, and d.
pre. Removing reactions.
A quick function to add right above the reaction function, but after the pages object is this:
const removeReaction = async (m, msg, emoji) => {
try { m.reactions.find(r => r.emoji.name == emoji).users.remove(msg.author.id); } catch(err) {}
}
This will make removing reactions easier, when a user reacts.
a. Going back:
This will handle going back a page.
if (reaction.emoji.name === '⬅') {
// remove the back reaction if possible
await removeReaction(m, msg, '⬅');
// check if the page can go back one
if (page != min) {
// change the page
page = page - 1;
await m.edit({ embed: pages[page] });
}
// restart the listener
awaitReactions(msg, m, options, filter);
}
I'll keep comments inside the code to keep it cleaner and easier to understand, but this code here will remove the reaction (if the bot has permission), check if it can go back, go back if it can, then start listening again.
b. Going forward:
This will, ironically, handle going forward a page.
else if (reaction.emoji.name === '➡') {
// remove the back reaction if possible
await removeReaction(m, msg, '➡');
// check if the page can go forward one
if (page != max) {
// change the page
page = page + 1;
await m.edit({ embed: pages[page] });
}
// restart the listener
awaitReactions(msg, m, options, filter);
}
That handles, well, that. Same thing happens here, just the code is changed a little.
c. Handling the delete:
Incase anyone wants to delete the help command, we can add a trash reaction to do that.
else if (reaction.emoji.name === '🗑') {
// trash the message instantly, returning so the listener fully stops
return await m.delete();
}
This will delete the embed fully, and stop listening.
d. Handling mis-reactions:
Just in case something happens, this code is here to restart the listener if something goes wrong:
else {
awaitReactions(msg, m, options, filter);
};
And that's it! Your reaction help menu is done now, and all you have to do is do some proper pages, and give it a test run!
I recorded a (very terrible) .gif of mine finished, click here to check it out!
Last updated
Was this helpful?