D++ (DPP)
C++ Discord API Bot Library
|
Collects objects from events during a specified time period. More...
#include <cluster.h>
Public Member Functions | |
collector (class cluster *cl, uint64_t duration, event_router_t< T > &event) | |
Construct a new collector object. More... | |
virtual void | completed (const std::vector< C > &list)=0 |
You must implement this function to receive the completed list of captured objects. More... | |
virtual const C * | filter (const T &element)=0 |
Filter the list of elements. More... | |
virtual void | cancel () |
Immediately cancels the collector. More... | |
virtual | ~collector () |
Destroy the collector object. More... | |
Protected Attributes | |
class cluster * | owner |
Owning cluster. More... | |
Collects objects from events during a specified time period.
This template must be specialised. There are premade specialisations which you can use such as dpp::reaction_collector and dpp::message_collector. For these specalised instances all you need to do is derive a simple class from them which implements collector::completed().
A collector will run for the specified number of seconds, attaching itself to the given event. During this time any events pass through the collector and collector::filter(). This function can return a pointer to an object to allow a copy of that object to be stored to a vector, or it can return nullptr to do nothing with that object. For example a collector attached to on_message_create would receive an event with the type message_create_t, and from this may decide to extract the message_create_t::msg structure, returning a pointer to it, or instead may choose to return a nullptr.
When either the predetermined timeout is reached, or the collector::cancel() method is called, or the collector is destroyed, the collector::completed() method is called, which will be passed a list of collected objects in the order they were collected.
T | parameter type of the event this collector will monitor |
C | object type this collector will store |
|
inline |
Construct a new collector object.
The timer for the collector begins immediately on construction of the object.
cl | Pointer to cluster which manages this collector |
duration | Duration in seconds to run the collector for |
event | Event to attach to, e.g. cluster::on_message_create |
|
inlinevirtual |
Destroy the collector object.
|
inlinevirtual |
Immediately cancels the collector.
Use this if you have met the conditions for which you are collecting objects early, e.g. you were watching for a message containing 'yes' or 'no' and have received it before the time is up.
|
pure virtual |
You must implement this function to receive the completed list of captured objects.
list | The list of captured objects in captured order |
|
pure virtual |
Filter the list of elements.
Every time an event is fired on the collector, this method wil be called to determine if we should add an object to the list or not. This function can then process the element
value, extract the parts which are to be saved to a list (e.g. a dpp::message out of a dpp::message_create_t) and return it as the return value. Returning a value of nullptr causes no object to be stored.
element | The event data to filter |
|
protected |
Owning cluster.