Collectors are a way to handle interactions from components for a specific time, for example, 10 seconds. So if for some reason the the node.js process closes, and then you restart it, the collector won’t collect new interactions.
Building collectors
Collectors are built using createComponentCollector method in a message, which is inherited by BaseMessage. This method returns an object representing a collector.
Here is an example of how to build a simple collector after sending a message with one button attached to it in a hello world command.
Handling interactions within a collector
Having created a collector from a message we are going to handle the interaction of the button with the run function of the collector.
Here is an example:
Filtering interactions
You might have thought about filtering the interaction received in the run function for limiting, for example the user who’s interacting with the button.
You would have added a condition inside the run function like this:
This will limit the use of the button to only the one which run the command.
But seyfert implements just a simply filter option when creating the collector which expects a callback that returns a boolean.
We are going to implement the filter for filtering the user who ran the interaction and filter the interaction only for button interactions.
Handling collector onStop
A collector could stop this mean the collector won’t be collecting more interactions of the message. To handle the stop we have to pass a callback into the onStop option when creating the collector.
The callback will take two parameters:
reason. A string which indicates the reason of why the collector has stopped. The most common is timeout or idle if we added the timeout or idle property to our collector. You can set the reason when you manually stop the collector within the collector.stop() function.
refresh. A function which you can execute to refresh the collector, making it collecting interactions as it did before.
Here is an example of how we add an idle to the collector of 1000ms and then everytime it enters into onStop callback we refresh it.
Handling Modals with collectors
As modals aren’t message components it is not possible to create a message components collector but Seyfert introduces the possiblity to create it by using the run method within the modal builder which expects a callback that shall handle the interactions.
Here is an example using the run within the modal builder: