Debian Codegraph @

include/dpp/collector.h

raw symbols SCIP
1 /************************************************************************************
2 *
3 * D++, A Lightweight C++ library for Discord
4 *
5 * Copyright 2021 Craig Edwards and D++ contributors
6 * (https://github.com/brainboxdotcc/DPP/graphs/contributors)
7 *
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
11 *
12 * http://www.apache.org/licenses/LICENSE-2.0
13 *
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
19 *
20 ************************************************************************************/
21 #pragma once
22
23 #include <dpp/export.h>
24 #include <dpp/cluster.h>
25 #include <dpp/timed_listener.h>
26 #include <time.h>
27 #include <vector>
28 #include <functional>
29 #include <string>
30
31 namespace dpp {
32
33 /**
34 * @brief Collects objects from events during a specified time period.
35 *
36 * This template must be specialised. There are premade specialisations which you can use
37 * such as dpp::reaction_collector and dpp::message_collector. For these specialised instances
38 * all you need to do is derive a simple class from them which implements collector::completed().
39 *
40 * A collector will run for the specified number of seconds, attaching itself to the
41 * given event. During this time any events pass through the collector and collector::filter().
42 * This function can return a pointer to an object to allow a copy of that object to be stored
43 * to a vector, or it can return nullptr to do nothing with that object. For example a collector
44 * attached to on_message_create would receive an event with the type message_create_t, and from
45 * this may decide to extract the message_create_t::msg structure, returning a pointer to it, or
46 * instead may choose to return a nullptr.
47 *
48 * When either the predetermined timeout is reached, or the collector::cancel() method is called,
49 * or the collector is destroyed, the collector::completed() method is called, which will be
50 * passed a list of collected objects in the order they were collected.
51 *
52 * @tparam T parameter type of the event this collector will monitor
53 * @tparam C object type this collector will store
54 */
55 template<class T, class C> class collector
56 {
57 protected:
58 /**
59 * @brief Owning cluster.
60 */
61 class cluster* owner;
62 private:
63 /**
64 * @brief Timed listener.
65 */
66 timed_listener<event_router_t<T>, std::function<void(const T&)>>* tl;
67
68 /**
69 * @brief Stored list.
70 */
71 std::vector<C> stored;
72
73 /**
74 * @brief Trigger flag.
75 */
76 bool triggered;
77 public:
78 /**
79 * @brief Construct a new collector object.
80 *
81 * The timer for the collector begins immediately on construction of the object.
82 *
83 * @param cl Pointer to cluster which manages this collector
84 * @param duration Duration in seconds to run the collector for
85 * @param event Event to attach to, e.g. cluster::on_message_create
86 */
87 collector(class cluster* cl, uint64_t duration, event_router_t<T> & event) : owner(cl), triggered(false) {
88 std::function<void(const T&)> f = [this](const T& event) {
89 const C* v = filter(event);
90 if (v) {
91 stored.push_back(*v);
92 }
93 };
94 tl = new dpp::timed_listener<event_router_t<T>, std::function<void(const T&)>>(cl, duration, event, f, [this]([[maybe_unused]] dpp::timer timer_handle) {
95 if (!triggered) {
96 triggered = true;
97 completed(stored);
98 }
99 });
100 }
101
102 /**
103 * @brief You must implement this function to receive the completed list of
104 * captured objects.
105 * @param list The list of captured objects in captured order
106 */
107 virtual void completed(const std::vector<C>& list) = 0;
108
109 /**
110 * @brief Filter the list of elements.
111 *
112 * Every time an event is fired on the collector, this method wil be called
113 * to determine if we should add an object to the list or not. This function
114 * can then process the `element` value, extract the parts which are to be
115 * saved to a list (e.g. a dpp::message out of a dpp::message_create_t) and
116 * return it as the return value. Returning a value of nullptr causes no
117 * object to be stored.
118 *
119 * Here is an example of how to filter messages which have specific text in them.
120 * This should be used with the specialised type dpp::message_collector
121 *
122 * ```cpp
123 * virtual const dpp::message* filter(const dpp::message_create_t& m) {
124 * if (m.msg.content.find("something i want") != std::string::npos) {
125 * return &m.msg;
126 * } else {
127 * return nullptr;
128 * }
129 * }
130 * ```
131 *
132 * @param element The event data to filter
133 * @return const C* Returned object or nullptr
134 */
135 virtual const C* filter(const T& element) = 0;
136
137 /**
138 * @brief Immediately cancels the collector.
139 *
140 * Use this if you have met the conditions for which you are collecting objects
141 * early, e.g. you were watching for a message containing 'yes' or 'no' and have
142 * received it before the time is up.
143 *
144 * @note Causes calling of the completed() method if it has not yet been called.
145 */
146 virtual void cancel() {
147 delete tl;
148 tl = nullptr;
149 }
150
151 /**
152 * @brief Destroy the collector object.
153 * @note Causes calling of the completed() method if it has not yet been called.
154 */
155 virtual ~collector() {
156 delete tl;
157 }
158 };
159
160 /**
161 * @brief Represents a reaction.
162 * Can be filled for use in a collector
163 */
164 class collected_reaction : public managed {
165 public:
166 /**
167 * @brief Reacting user.
168 */
169 user react_user{};
170
171 /**
172 * @brief Reacting guild.
173 */
174 guild react_guild{};
175
176 /**
177 * @brief Reacting guild member.
178 */
179 guild_member react_member{};
180
181 /**
182 * @brief Reacting channel.
183 */
184 channel react_channel{};
185
186 /**
187 * @brief Reacted emoji.
188 */
189 emoji react_emoji{};
190
191 /**
192 * @brief Optional: ID of the user who authored the message which was reacted to.
193 */
194 snowflake message_author_id{};
195 };
196
197 /**
198 * @brief Template type for base class of channel collector
199 */
200 typedef dpp::collector<dpp::channel_create_t, dpp::channel> channel_collector_t;
201
202 /**
203 * @brief Template type for base class of thread collector
204 */
205 typedef dpp::collector<dpp::thread_create_t, dpp::thread> thread_collector_t;
206
207 /**
208 * @brief Template type for base class of role collector
209 */
210 typedef dpp::collector<dpp::guild_role_create_t, dpp::role> role_collector_t;
211
212 /**
213 * @brief Template type for base class of scheduled event collector
214 */
215 typedef dpp::collector<dpp::guild_scheduled_event_create_t, dpp::scheduled_event> scheduled_event_collector_t;
216
217 /**
218 * @brief Template type for base class of message collector
219 */
220 typedef dpp::collector<dpp::message_create_t, dpp::message> message_collector_t;
221
222 /**
223 * @brief Template type for base class of message reaction collector
224 */
225 typedef dpp::collector<dpp::message_reaction_add_t, dpp::collected_reaction> reaction_collector_t;
226
227 /**
228 * @brief Message collector.
229 * Collects messages during a set timeframe and returns them in a list via the completed() method.
230 */
231 class message_collector : public message_collector_t {
232 public:
233 /**
234 * @brief Construct a new message collector object
235 *
236 * @param cl cluster to associate the collector with
237 * @param duration Duration of time to run the collector for in seconds
238 */
239 message_collector(cluster* cl, uint64_t duration) : message_collector_t::collector(cl, duration, cl->on_message_create) { }
240
241 /**
242 * @brief Return the completed collection
243 *
244 * @param list items collected during the timeframe specified
245 */
246 virtual void completed(const std::vector<dpp::message>& list) = 0;
247
248 /**
249 * @brief Select and filter the items which are to appear in the list
250 * This is called every time a new event is fired, to filter the event and determine which
251 * of the items is sent to the list. Returning nullptr excludes the item from the list.
252 *
253 * @param element element to filter
254 * @return Returned item to add to the list, or nullptr to skip adding this element
255 */
256 virtual const dpp::message* filter(const dpp::message_create_t& element) { return &element.msg; }
257
258 /**
259 * @brief Destroy the message collector object
260 */
261 virtual ~message_collector() = default;
262 };
263
264 /**
265 * @brief Reaction collector.
266 * Collects message reactions during a set timeframe and returns them in a list via the completed() method.
267 */
268 class reaction_collector : public reaction_collector_t {
269 /**
270 * @brief The ID of the message.
271 */
272 snowflake message_id;
273
274 /**
275 * @brief The reaction.
276 */
277 collected_reaction react;
278 public:
279 /**
280 * @brief Construct a new reaction collector object
281 *
282 * @param cl cluster to associate the collector with
283 * @param duration Duration of time to run the collector for in seconds
284 * @param msg_id Optional message ID. If specified, only collects reactions for the given message
285 */
286 reaction_collector(cluster* cl, uint64_t duration, snowflake msg_id = 0) : reaction_collector_t::collector(cl, duration, cl->on_message_reaction_add), message_id(msg_id) { }
287
288 /**
289 * @brief Return the completed collection
290 *
291 * @param list items collected during the timeframe specified
292 */
293 virtual void completed(const std::vector<dpp::collected_reaction>& list) = 0;
294
295 /**
296 * @brief Select and filter the items which are to appear in the list
297 * This is called every time a new event is fired, to filter the event and determine which
298 * of the items is sent to the list. Returning nullptr excludes the item from the list.
299 *
300 * @param element element to filter
301 * @return Returned item to add to the list, or nullptr to skip adding this element
302 */
303 virtual const dpp::collected_reaction* filter(const dpp::message_reaction_add_t& element) {
304 /* Capture reactions for given message ID only */
305 if (message_id.empty() || element.message_id == message_id) {
306 react.id = element.message_id;
307 react.react_user = element.reacting_user;
308 react.react_guild = element.reacting_guild;
309 react.react_member = element.reacting_member;
310 react.react_channel = element.reacting_channel;
311 react.react_emoji = element.reacting_emoji;
312 react.message_author_id = element.message_author_id;
313 return &react;
314 } else {
315 return nullptr;
316 }
317 }
318
319 /**
320 * @brief Destroy the reaction collector object
321 */
322 virtual ~reaction_collector() = default;
323 };
324
325 /**
326 * @brief Channel collector.
327 * Collects channels during a set timeframe and returns them in a list via the completed() method.
328 */
329 class channel_collector : public channel_collector_t {
330 public:
331 /**
332 * @brief Construct a new channel collector object
333 *
334 * @param cl cluster to associate the collector with
335 * @param duration Duration of time to run the collector for in seconds
336 */
337 channel_collector(cluster* cl, uint64_t duration) : channel_collector_t::collector(cl, duration, cl->on_channel_create) { }
338
339 /**
340 * @brief Return the completed collection
341 *
342 * @param list items collected during the timeframe specified
343 */
344 virtual void completed(const std::vector<dpp::channel>& list) = 0;
345
346 /**
347 * @brief Select and filter the items which are to appear in the list
348 * This is called every time a new event is fired, to filter the event and determine which
349 * of the items is sent to the list. Returning nullptr excludes the item from the list.
350 *
351 * @param element element to filter
352 * @return Returned item to add to the list, or nullptr to skip adding this element
353 */
354 virtual const dpp::channel* filter(const dpp::channel_create_t& element) { return &element.created; }
355
356 /**
357 * @brief Destroy the channel collector object
358 */
359 virtual ~channel_collector() = default;
360 };
361
362 /**
363 * @brief Thread collector.
364 * Collects threads during a set timeframe and returns them in a list via the completed() method.
365 */
366 class thread_collector : public thread_collector_t {
367 public:
368 /**
369 * @brief Construct a new thread collector object
370 *
371 * @param cl cluster to associate the collector with
372 * @param duration Duration of time to run the collector for in seconds
373 */
374 thread_collector(cluster* cl, uint64_t duration) : thread_collector_t::collector(cl, duration, cl->on_thread_create) { }
375
376 /**
377 * @brief Return the completed collection
378 *
379 * @param list items collected during the timeframe specified
380 */
381 virtual void completed(const std::vector<dpp::thread>& list) = 0;
382
383 /**
384 * @brief Select and filter the items which are to appear in the list
385 * This is called every time a new event is fired, to filter the event and determine which
386 * of the items is sent to the list. Returning nullptr excludes the item from the list.
387 *
388 * @param element element to filter
389 * @return Returned item to add to the list, or nullptr to skip adding this element
390 */
391 virtual const dpp::thread* filter(const dpp::thread_create_t& element) { return &element.created; }
392
393 /**
394 * @brief Destroy the thread collector object
395 */
396 virtual ~thread_collector() = default;
397 };
398
399 /**
400 * @brief Role collector.
401 * Collects guild roles during a set timeframe and returns them in a list via the completed() method.
402 */
403 class role_collector : public role_collector_t {
404 public:
405 /**
406 * @brief Construct a new role collector object
407 *
408 * @param cl cluster to associate the collector with
409 * @param duration Duration of time to run the collector for in seconds
410 */
411 role_collector(cluster* cl, uint64_t duration) : role_collector_t::collector(cl, duration, cl->on_guild_role_create) { }
412
413 /**
414 * @brief Return the completed collection
415 *
416 * @param list items collected during the timeframe specified
417 */
418 virtual void completed(const std::vector<dpp::role>& list) = 0;
419
420 /**
421 * @brief Select and filter the items which are to appear in the list
422 * This is called every time a new event is fired, to filter the event and determine which
423 * of the items is sent to the list. Returning nullptr excludes the item from the list.
424 *
425 * @param element element to filter
426 * @return Returned item to add to the list, or nullptr to skip adding this element
427 */
428 virtual const dpp::role* filter(const dpp::guild_role_create_t& element) { return &element.created; }
429
430 /**
431 * @brief Destroy the role collector object
432 */
433 virtual ~role_collector() = default;
434 };
435
436 /**
437 * @brief Scheduled event collector.
438 * Collects messages during a set timeframe and returns them in a list via the completed() method.
439 */
440 class scheduled_event_collector : public scheduled_event_collector_t {
441 public:
442 /**
443 * @brief Construct a new scheduled event collector object
444 *
445 * @param cl cluster to associate the collector with
446 * @param duration Duration of time to run the collector for in seconds
447 */
448 scheduled_event_collector(cluster* cl, uint64_t duration) : scheduled_event_collector_t::collector(cl, duration, cl->on_guild_scheduled_event_create) { }
449
450 /**
451 * @brief Return the completed collection
452 *
453 * @param list items collected during the timeframe specified
454 */
455 virtual void completed(const std::vector<dpp::scheduled_event>& list) = 0;
456
457 /**
458 * @brief Select and filter the items which are to appear in the list
459 * This is called every time a new event is fired, to filter the event and determine which
460 * of the items is sent to the list. Returning nullptr excludes the item from the list.
461 *
462 * @param element element to filter
463 * @return Returned item to add to the list, or nullptr to skip adding this element
464 */
465 virtual const dpp::scheduled_event* filter(const dpp::guild_scheduled_event_create_t& element) { return &element.created; }
466
467 /**
468 * @brief Destroy the scheduled event collector object
469 */
470 virtual ~scheduled_event_collector() = default;
471 };
472
473 }