D++ (DPP)  10.0.30
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:2052
dpp.h
dpp::st_wait
@ st_wait
Wait forever on a condition variable.
Definition: cluster.h:101
dpp::snowflake
A container for a 64 bit unsigned value representing many things on discord.
Definition: snowflake.h:70
main
int main()
Definition: soak.cpp:28
dpp::message_create_t
Create message.
Definition: dispatcher.h:1655
dpp::message
Represents messages sent and received on Discord.
Definition: message.h:2007
dpp
The main namespace for D++ functions.
Definition: appcommand.h:35
dpp::utility::cout_logger
std::function< void(const dpp::log_t &)> DPP_EXPORT cout_logger()
Get a default logger that outputs to std::cout.
Definition: dispatcher.h:228
dpp::i_message_content
@ i_message_content
Intent for receipt of message content.
Definition: intents.h:152
dpp::unicode_emoji::id
constexpr const char id[]
Definition: unicode_emoji.h:5229
dpp::reaction_collector
Reaction collector.
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