ReVanced / revanced-helper

πŸ€– NLP backed bots assisting ReVanced
https://revanced.app
GNU General Public License v3.0
20 stars 8 forks source link

fix(bots/discord): do not use collectors for buttons #6

Closed PalmDevs closed 1 year ago

PalmDevs commented 1 year ago

πŸšͺ Leaving collectors behind

Collecters are a Discord.js feature which makes it easier to listen for events from the Discord API in a specific context (eg. listen for a button click in a specific message, etc.).

Collectors work by creating a new listener and attaching it to the Client instance of Discord.js which means each time we create a collector, we will attach another listener. These listeners are cleared after a certain set timeout.

❔ How it is being used

Currently, the Discord bot heavily uses collectors everywhere for every message it listens for buttons. However, this approach is costly as the count of listeners can grow limitlessly and can cause errors with the EventEmitter listener limit.

πŸ’¬ Alternative performant approach

Buttons in Discord can have a custom ID, we can utilize this custom ID to store specific metadata like message IDs. It can be something like upvote_<message id> and downvote_<message id>.

Now instead of using the collectors, we can instead attach a single listener which listens for button interactions with the user. This listener will retrieve the button ID, parse it and decide what to do next.

Since the button ID also contains a message ID, the listener can grab those messages from Client's message cache. If a cache doesn't exist, then it means the button has expired. Message caches are removed at a certain limit threshold which is configurable.