D++ (DPP)
C++ Discord API Bot Library
Collecting Reactions

D++ comes with many useful helper classes, but amongst these is something called dpp::collector. Collector is a template which can be specialised to automatically collect objects of a predetermined type from events for a specific interval of time. Once this time period is up, or the class is otherwise signalled, a method is called with the complete set of collected objects.

In the example below, we will use it to collect all reactions on a message.

#include <dpp/dpp.h>
/* To create a collector we must derive from dpp::collector. As dpp::collector is a complicated template,
* various pre-made forms exist such as this one, reaction_collector.
*/
class react_collector : public dpp::reaction_collector {
public:
/* Collector will run for 20 seconds */
react_collector(dpp::cluster* cl, dpp::snowflake id) : dpp::reaction_collector(cl, 20, id) { }
/* Override the "completed" event and then output the number of collected reactions as a message. */
virtual void completed(const std::vector<dpp::collected_reaction>& list) override {
if (list.size()) {
owner->message_create(dpp::message(list[0].react_channel->id, "I collected " + std::to_string(list.size()) + " reactions!"));
} else {
owner->message_create(dpp::message("... I got nothin'."));
}
}
};
int main() {
/* Create bot */
/* Pointer to reaction collector */
react_collector* r = nullptr;
/* Message handler */
bot.on_message_create([&r, &bot](const dpp::message_create_t& event) {
/* If someone sends a message that has the text 'collect reactions!' start a reaction collector */
if (event.msg.content == "collect reactions!" && r == nullptr) {
/* Create a new reaction collector to collect reactions */
r = new react_collector(&bot, event.msg.id);
}
});
/* Start bot */
bot.start(dpp::st_wait);
return 0;
}
dpp::unicode_emoji::cl
constexpr const char cl[]
Definition: unicode_emoji.h:5257
dpp::message_create_t::msg
message msg
message that was created (sent).
Definition: dispatcher.h:1662
dpp::reaction_collector::completed
virtual void completed(const std::vector< dpp::collected_reaction > &list)=0
Return the completed collection.
dpp::i_default_intents
@ i_default_intents
Default D++ intents (all non-privileged intents).
Definition: intents.h:172
dpp::message::content
std::string content
Contents of the message.
Definition: message.h:1817
dpp::st_wait
@ st_wait
Wait forever on a condition variable. The cluster will spawn threads for each shard and start() will ...
Definition: cluster.h:101
dpp::snowflake
A container for a 64 bit unsigned value representing many things on discord. This value is known in d...
Definition: snowflake.h:70
dpp::message_create_t
Create message.
Definition: dispatcher.h:1655
dpp::message
Represents messages sent and received on Discord.
Definition: message.h:1772
dpp
The main namespace for D++ functions. classes and types.
Definition: appcommand.h:35
dpp::i_message_content
@ i_message_content
Intent for receipt of message content.
Definition: intents.h:152
dpp::utility::cout_logger
std::function< void(const dpp::log_t &)> DPP_EXPORT cout_logger()
Get a default logger that outputs to std::cout. e.g.
Definition: dispatcher.h:228
dpp::unicode_emoji::id
constexpr const char id[]
Definition: unicode_emoji.h:5229
dpp::reaction_collector
Reaction collector. Collects message reactions during a set timeframe and returns them in a list via ...
Definition: collector.h:287
dpp::cluster
The cluster class represents a group of shards and a command queue for sending and receiving commands...
Definition: cluster.h:99
D++ Library version 9.0.13D++ Library version 9.0.12D++ Library version 9.0.11D++ Library version 9.0.10D++ Library version 9.0.9D++ Library version 9.0.8D++ Library version 9.0.7D++ Library version 9.0.6D++ Library version 9.0.5D++ Library version 9.0.4D++ Library version 9.0.3D++ Library version 9.0.2D++ Library version 9.0.1D++ Library version 9.0.0D++ Library version 1.0.2D++ Library version 1.0.1D++ Library version 1.0.0