| 1 | #include <dpp/dpp.h> |
| 2 | |
| 3 | /* To create a collector we must derive from dpp::collector. As dpp::collector is a complicated template, |
| 4 | * various pre-made forms exist such as this one, reaction_collector. |
| 5 | */ |
| 6 | class react_collector : public dpp::reaction_collector { |
| 7 | public: |
| 8 | /* Collector will run for 20 seconds */ |
| 9 | react_collector(dpp::cluster* cl, dpp::snowflake id) : dpp::reaction_collector(cl, 20, id) { } |
| 10 | |
| 11 | /* Override the "completed" event and then output the number of collected reactions as a message. */ |
| 12 | virtual void completed(const std::vector<dpp::collected_reaction>& list) override { |
| 13 | if (list.size()) { |
| 14 | owner->message_create(dpp::message(list[0].react_channel.id, "I collected " + std::to_string(list.size()) + " reactions!")); |
| 15 | } else { |
| 16 | owner->message_create(dpp::message("... I got nothin'.")); |
| 17 | } |
| 18 | } |
| 19 | }; |
| 20 | |
| 21 | |
| 22 | int main() { |
| 23 | /* Create bot */ |
| 24 | dpp::cluster bot("token", dpp::i_default_intents | dpp::i_message_content); |
| 25 | |
| 26 | /* Pointer to reaction collector */ |
| 27 | react_collector* r = nullptr; |
| 28 | |
| 29 | bot.on_log(dpp::utility::cout_logger()); |
| 30 | |
| 31 | /* Message handler */ |
| 32 | bot.on_message_create([&r, &bot](const dpp::message_create_t& event) { |
| 33 | |
| 34 | /* If someone sends a message that has the text 'collect reactions!' start a reaction collector */ |
| 35 | if (event.msg.content == "collect reactions!" && r == nullptr) { |
| 36 | /* Create a new reaction collector to collect reactions */ |
| 37 | r = new react_collector(&bot, event.msg.id); |
| 38 | } |
| 39 | |
| 40 | }); |
| 41 | |
| 42 | /* Start bot */ |
| 43 | bot.start(dpp::st_wait); |
| 44 | |
| 45 | return 0; |
| 46 | } |