-
-
Notifications
You must be signed in to change notification settings - Fork 168
/
cluster.h
4080 lines (3645 loc) · 240 KB
/
cluster.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/************************************************************************************
*
* D++, A Lightweight C++ library for Discord
*
* Copyright 2021 Craig Edwards and D++ contributors
* (https://github.com/brainboxdotcc/DPP/graphs/contributors)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
************************************************************************************/
#pragma once
#include <dpp/export.h>
#include <string>
#include <map>
#include <variant>
#include <thread>
#include <dpp/snowflake.h>
#include <dpp/dispatcher.h>
#include <dpp/misc-enum.h>
#include <dpp/timer.h>
#include <dpp/json_fwd.h>
#include <dpp/discordclient.h>
#include <dpp/discordvoiceclient.h>
#include <dpp/voiceregion.h>
#include <dpp/dtemplate.h>
#include <dpp/prune.h>
#include <dpp/auditlog.h>
#include <dpp/queues.h>
#include <dpp/cache.h>
#include <dpp/intents.h>
#include <dpp/discordevents.h>
#include <algorithm>
#include <iostream>
#include <shared_mutex>
#include <cstring>
#include <dpp/restresults.h>
#include <dpp/event_router.h>
#include <dpp/coro/async.h>
#include <dpp/socketengine.h>
namespace dpp {
/**
* @brief Pass this value into the constructor of dpp::cluster for the shard count to create a cluster with no shards.
* A cluster with no shards does not connect to a websocket, but can still use the event loop to dispatch HTTPS API
* requests to Discord. This is useful for bots that do not need to receive websocket events as it will save a lot of
* resources.
*/
constexpr uint32_t NO_SHARDS = ~0U;
/**
* @brief Types of startup for cluster::start()
*/
enum start_type : bool {
/**
* @brief Wait forever on a condition variable.
* The cluster will spawn threads for each shard
* and start() will not return in normal operation.
*/
st_wait = false,
/**
* @brief Return immediately after starting shard threads.
* If you set the parameter of cluster::start() to
* this value, you will have to manage the lifetime
* and scope of your cluster object yourself. Taking it
* out of scope or deleting its pointer will terminate
* the bot.
*/
st_return = true,
};
/** @brief The cluster class represents a group of shards and a command queue for sending and
* receiving commands from discord via HTTP. You should usually instantiate a cluster object
* at the very least to make use of the library.
*/
class DPP_EXPORT cluster {
friend class discord_client;
friend class discord_voice_client;
/**
* @brief default gateway for connecting the websocket.
*/
std::string default_gateway;
/**
* @brief queue system for commands sent to Discord, and any replies
*/
request_queue* rest;
/**
* @brief queue system for arbitrary HTTP requests sent by the user to sites other than Discord
*/
request_queue* raw_rest;
/**
* @brief True if to use compression on shards
*/
bool compressed;
/**
* @brief Lock to prevent concurrent access to dm_channels
*/
std::mutex dm_list_lock;
/**
* @brief Start time of cluster
*/
time_t start_time;
/**
* @brief Active DM channels for the bot
*/
std::unordered_map<snowflake, snowflake> dm_channels;
/**
* @brief Active shards on this cluster. Shard IDs may have gaps between if there
* are multiple clusters.
*/
shard_list shards;
/**
* @brief List of shards waiting for reconnection
*/
reconnect_list reconnections;
/**
* @brief Ephemeral list of deleted timer ids
*/
timers_deleted_t deleted_timers;
/**
* @brief Priority queue of of timers by time
*/
timer_next_t next_timer;
/**
* @brief Mutex to work with named_commands and synchronize read write access
*/
std::shared_mutex named_commands_mutex;
/**
* @brief Typedef for slashcommand handler type
*/
using slashcommand_handler_t = std::function<void(const slashcommand_t &)>;
#ifdef DPP_CORO
/**
* @brief Typedef for coroutines based slashcommand handler type
*/
using co_slashcommand_handler_t = std::function<dpp::task<void>(const slashcommand_t&)>;
/**
* @brief Typedef for variant of coroutines based slashcommand handler type and regular version of it
*/
using slashcommand_handler_variant = std::variant<slashcommand_handler_t,co_slashcommand_handler_t>;
/**
* @brief Container to store relation between command name and it's handler
*/
std::map<std::string,slashcommand_handler_variant> named_commands;
#else
/**
* @brief Container to store relation between command name and it's handler
*/
std::map<std::string,slashcommand_handler_t> named_commands;
#endif
/**
* @brief Thread pool
*/
std::unique_ptr<thread_pool> pool{nullptr};
/**
* @brief Used to spawn the socket engine into its own thread if
* the cluster is started with dpp::st_return. It is unused otherwise.
*/
std::thread engine_thread;
/**
* @brief Protection mutex for timers
*/
std::mutex timer_guard;
/**
* @brief Mark a shard as requiring reconnection.
* Destructs the old shard in 5 seconds and creates a new one attempting to resume.
*
* @param shard_id Shard ID
*/
void add_reconnect(uint32_t shard_id);
public:
/**
* @brief Current bot token for all shards on this cluster and all commands sent via HTTP
*/
std::string token;
/**
* @brief Last time the bot sent an IDENTIFY
*/
time_t last_identify;
/**
* @brief Current bitmask of gateway intents
*/
uint32_t intents;
/**
* @brief Total number of shards across all clusters
*/
uint32_t numshards;
/**
* @brief ID of this cluster, between 0 and MAXCLUSTERS-1 inclusive
*/
uint32_t cluster_id;
/**
* @brief Total number of clusters that are active
*/
uint32_t maxclusters;
/**
* @brief REST latency (HTTPS ping) in seconds
*/
double rest_ping;
/**
* @brief The details of the bot user. This is assumed to be identical across all shards
* in the cluster. Each connecting shard updates this information.
*/
dpp::user me;
/**
* @brief Current cache policy for the cluster
*/
cache_policy_t cache_policy;
/**
* @brief Websocket mode for all shards in the cluster, either ws_json or ws_etf.
* Production bots should use ETF, while development bots should use JSON.
*/
websocket_protocol_t ws_mode;
/**
* @brief Atomic bool to set to true when the cluster is terminating.
*
* D++ itself does not set this value, it is for library users to set if they want
* the cluster to terminate outside of a flow where they may have simple access to
* destruct the cluster object.
*/
std::atomic_bool terminating{false};
/**
* @brief The time (in seconds) that a request is allowed to take.
*/
uint16_t request_timeout = 60;
/**
* @brief Socket engine instance
*/
std::unique_ptr<socket_engine_base> socketengine;
/**
* @brief Constructor for creating a cluster without a token.
* A cluster created without a token has no shards, and just runs the event loop. You can use this to make asynchronous
* HTTP requests via e.g. dpp::cluster::request without having to connect to a websocket to receive shard events.
* @param pool_threads The number of threads to allocate for the thread pool. This defaults to half your system concurrency and if set to a number less than 4, will default to 4.
* All callbacks and events are placed into the thread pool. The bigger you make this pool (but generally no bigger than your number of cores), the more your bot will scale.
* @throw dpp::exception Thrown on windows, if WinSock fails to initialise, or on any other system if a dpp::request_queue fails to construct
*/
explicit cluster(uint32_t pool_threads = std::thread::hardware_concurrency() / 2);
/**
* @brief Constructor for creating a cluster. All but the token are optional.
* @param token The bot token to use for all HTTP commands and websocket connections
* @param intents A bitmask of dpd::intents values for all shards on this cluster. This is required to be sent for all bots with over 100 servers.
* @param shards The total number of shards on this bot. If there are multiple clusters, then (shards / clusters) actual shards will run on this cluster.
* If you omit this value, the library will attempt to query the Discord API for the correct number of shards to start.
* @param cluster_id The ID of this cluster, should be between 0 and MAXCLUSTERS-1
* @param maxclusters The total number of clusters that are active, which may be on separate processes or even separate machines.
* @param compressed Whether or not to use compression for shards on this cluster. Saves a ton of bandwidth at the cost of some CPU
* @param policy Set the caching policy for the cluster, either lazy (only cache users/members when they message the bot) or aggressive (request whole member lists on seeing new guilds too)
* @param pool_threads The number of threads to allocate for the thread pool. This defaults to half your system concurrency and if set to a number less than 4, will default to 4.
* All callbacks and events are placed into the thread pool. The bigger you make this pool (but generally no bigger than your number of cores), the more your bot will scale.
* @throw dpp::exception Thrown on windows, if WinSock fails to initialise, or on any other system if a dpp::request_queue fails to construct
*/
cluster(const std::string& token, uint32_t intents = i_default_intents, uint32_t shards = 0, uint32_t cluster_id = 0, uint32_t maxclusters = 1, bool compressed = true, cache_policy_t policy = cache_policy::cpol_default, uint32_t pool_threads = std::thread::hardware_concurrency() / 2);
/**
* @brief Place some arbitrary work into the thread pool for execution when time permits.
*
* Work units are fetched into threads on the thread pool from the queue in order of priority,
* lowest numeric values first. Low numeric values should be reserved for API replies from Discord,
* guild creation events, etc.
*
* @param priority Priority of the work unit
* @param task Task to queue
*/
void queue_work(int priority, work_unit task);
/**
* @brief dpp::cluster is non-copyable
*/
cluster(const cluster&) = delete;
/**
* @brief dpp::cluster is non-moveable
*/
cluster(const cluster&&) = delete;
/**
* @brief dpp::cluster is non-copyable
*/
cluster& operator=(const cluster&) = delete;
/**
* @brief dpp::cluster is non-moveable
*/
cluster& operator=(const cluster&&) = delete;
/**
* @brief Destroy the cluster object
*/
virtual ~cluster();
/**
* @brief End cluster execution without destructing it.
* To restart the cluster, call cluster::start() again.
*/
void shutdown();
/**
* @brief Get the rest_queue object which handles HTTPS requests to Discord
* @return request_queue* pointer to request_queue object
*/
request_queue* get_rest();
/**
* @brief Get the raw rest_queue object which handles all HTTP(S) requests that are not directed at Discord
* @return request_queue* pointer to request_queue object
*/
request_queue* get_raw_rest();
/**
* @brief Set the websocket protocol for all shards on this cluster.
* You should call this method before cluster::start.
* Generally ws_etf is faster, but provides less facilities for debugging should something
* go wrong. It is recommended to use ETF in production and JSON in development.
*
* @param mode websocket protocol to use, either ws_json or ws_etf.
* @return cluster& Reference to self for chaining.
* @throw dpp::logic_exception If called after the cluster is started (this is not supported)
*/
cluster& set_websocket_protocol(websocket_protocol_t mode);
/**
* @brief Tick active timers
*/
void tick_timers();
/**
* @brief Set the audit log reason for the next REST call to be made.
* This is set per-thread, so you must ensure that if you call this method, your request that
* is associated with the reason happens on the same thread where you set the reason.
* Once the next call is made, the audit log reason is cleared for this thread automatically.
*
* Example:
* ```
* bot.set_audit_reason("Too much abusive content")
* .channel_delete(my_channel_id);
* ```
*
* @param reason The reason to set for the next REST call on this thread
* @return cluster& Reference to self for chaining.
*/
cluster& set_audit_reason(const std::string &reason);
/**
* @brief Clear the audit log reason for the next REST call to be made.
* This is set per-thread, so you must ensure that if you call this method, your request that
* is associated with the reason happens on the same thread where you set the reason.
* Once the next call is made, the audit log reason is cleared for this thread automatically.
*
* Example:
* ```
* bot.set_audit_reason("Won't be sent")
* .clear_audit_reason()
* .channel_delete(my_channel_id);
* ```
*
* @return cluster& Reference to self for chaining.
*/
cluster& clear_audit_reason();
/**
* @brief Get the audit reason set for the next REST call to be made on this thread.
* This is set per-thread, so you must ensure that if you call this method, your request that
* is associated with the reason happens on the same thread where you set the reason.
* Once the next call is made, the audit log reason is cleared for this thread automatically.
*
* @note This method call clears the audit reason when it returns it.
*
* @return std::string The audit reason to be used.
*
*/
std::string get_audit_reason();
/**
* @brief Sets the address of the default gateway, for connecting the websockets.
*
* @return cluster& Reference to self for chaining.
*/
cluster& set_default_gateway(const std::string& default_gateway);
/**
* @brief Log a message to whatever log the user is using.
* The logged message is passed up the chain to the on_log event in user code which can then do whatever
* it wants to do with it.
* @param severity The log level from dpp::loglevel
* @param msg The log message to output
*/
void log(dpp::loglevel severity, const std::string &msg) const;
/**
* @brief Start a timer. Every `frequency` seconds, the callback is called.
*
* @param on_tick The callback lambda to call for this timer when ticked
* @param on_stop The callback lambda to call for this timer when it is stopped
* @param frequency How often to tick the timer in seconds
* @return timer A handle to the timer, used to remove that timer later
*/
timer start_timer(timer_callback_t on_tick, uint64_t frequency, timer_callback_t on_stop = {});
#ifdef DPP_CORO
/**
* @brief Start a coroutine timer. Every `frequency` seconds, the callback is called.
*
* @param on_tick The callback lambda to call for this timer when ticked
* @param on_stop The callback lambda to call for this timer when it is stopped
* @param frequency How often to tick the timer in seconds
* @return timer A handle to the timer, used to remove that timer later
*/
template <std::invocable<timer> T, std::invocable<timer> U = std::function<void(timer)>>
requires (dpp::awaitable_type<typename std::invoke_result<T, timer>::type>)
timer start_timer(T&& on_tick, uint64_t frequency, U&& on_stop = {}) {
std::function<void(timer)> ticker = [fun = std::forward<T>(on_tick)](timer t) mutable -> dpp::job {
co_await std::invoke(fun, t);
};
std::function<void(timer)> stopper;
if constexpr (dpp::awaitable_type<typename std::invoke_result<U, timer>::type>) {
stopper = [fun = std::forward<U>(on_stop)](timer t) mutable -> dpp::job {
co_await std::invoke(fun, t);
};
} else {
stopper = std::forward<U>(on_stop);
}
return start_timer(std::move(ticker), frequency, std::move(stopper));
}
#endif
/**
* @brief Stop a ticking timer
*
* @param t Timer handle received from cluster::start_timer
* @return bool True if the timer was stopped, false if it did not exist
* @note If the timer has an on_stop lambda, the on_stop lambda will be called.
*/
bool stop_timer(timer t);
#ifdef DPP_CORO
/**
* @brief Get an awaitable to wait a certain amount of seconds. Use the co_await keyword on its return value to suspend the coroutine until the timer ends
*
* @param seconds How long to wait for
* @return async<timer> Object that can be co_await-ed to suspend the function for a certain time
*/
[[nodiscard]] async<timer> co_sleep(uint64_t seconds);
#endif
/**
* @brief Get the dm channel for a user id
*
* @param user_id the user id to get the dm channel for
* @return Returns 0 on failure
*/
snowflake get_dm_channel(snowflake user_id);
/**
* @brief Set the dm channel id for a user id
*
* @param user_id user id to set the dm channel for
* @param channel_id dm channel to set
*/
void set_dm_channel(snowflake user_id, snowflake channel_id);
/**
* @brief Returns the uptime of the cluster
*
* @return dpp::utility::uptime The uptime of the cluster
*/
dpp::utility::uptime uptime();
/**
* @brief Start the cluster, connecting all its shards.
*
* Returns once all shards are connected if return_after is true,
* otherwise enters an infinite loop while the shards run.
*
* @param return_after If true the bot will return to your program after starting shards, if false this function will never return.
*/
void start(start_type return_after = st_wait);
/**
* @brief Set the presence for all shards on the cluster
*
* @param p The presence to set. Only the online status and the first activity are sent.
*/
void set_presence(const class dpp::presence &p);
/**
* @brief Get a shard by id, returning the discord_client
*
* @param id Shard ID
* @return discord_client* shard, or null
*/
discord_client* get_shard(uint32_t id);
/**
* @brief Get the list of shards
*
* @return shard_list& Reference to map of shards for this cluster
*/
const shard_list& get_shards();
/**
* @brief Sets the request timeout.
*
* @param timeout The length of time (in seconds) that requests are allowed to take. Default: 20.
*
* @return cluster& Reference to self for chaining.
*/
cluster& set_request_timeout(uint16_t timeout);
/* Functions for attaching to event handlers */
/**
* @brief Register a slash command handler.
*
* @param name The name of the slash command to register
* @param handler A handler function of type `slashcommand_handler_t`
*
* @return bool Returns `true` if the command was registered successfully, or `false` if
* the command with the same name already exists
*/
bool register_command(const std::string& name, const slashcommand_handler_t handler);
/**
* @brief Get the number of currently active HTTP(S) requests active in the cluster.
* This total includes all in-flight API requests and calls to dpp::cluster::request().
* Note that once a request is passed to the thread pool it is no longer counted here.
* @return Total active request count
*/
size_t active_requests();
#ifdef DPP_CORO
/**
* @brief Register a coroutine-based slash command handler.
*
* @param name The name of the slash command to register.
* @param handler A coroutine handler function of type `co_slashcommand_handler_t`.
*
* @return bool Returns `true` if the command was registered successfully, or `false` if
* the command with the same name already exists.
*/
template <typename F>
std::enable_if_t<std::is_same_v<std::invoke_result_t<F, const slashcommand_handler_t&>, dpp::task<void>>, bool>
register_command(const std::string& name, F&& handler){
std::unique_lock lk(named_commands_mutex);
auto [_, inserted] = named_commands.try_emplace(name, std::forward<F>(handler));
return inserted;
};
#endif
/**
* @brief Unregister a slash command.
*
* This function unregisters (removes) a previously registered slash command by name.
* If the command is successfully removed, it returns `true`.
*
* @param name The name of the slash command to unregister.
*
* @return bool Returns `true` if the command was successfully unregistered, or `false`
* if the command was not found.
*/
bool unregister_command(const std::string& name);
/**
* @brief on voice state update event
*
* @see https://discord.com/developers/docs/topics/gateway-events#voice-state-update
* @note Use operator() to attach a lambda to this event, and the detach method to detach the listener using the returned ID.
* The function signature for this event takes a single `const` reference of type voice_state_update_t&, and returns void.
*/
event_router_t<voice_state_update_t> on_voice_state_update;
/**
* @brief on voice client platform event
* After a client connects, or on joining a vc, you will receive the platform type of each client. This is either desktop
* or mobile.
*
* @note Use operator() to attach a lambda to this event, and the detach method to detach the listener using the returned ID.
* The function signature for this event takes a single `const` reference of type voice_client_disconnect_t&, and returns void.
*/
event_router_t<voice_client_platform_t> on_voice_client_platform;
/**
* @brief on voice client disconnect event
*
* @note Use operator() to attach a lambda to this event, and the detach method to detach the listener using the returned ID.
* The function signature for this event takes a single `const` reference of type voice_client_disconnect_t&, and returns void.
*/
event_router_t<voice_client_disconnect_t> on_voice_client_disconnect;
/**
* @brief on voice client speaking event
*
* @note Use operator() to attach a lambda to this event, and the detach method to detach the listener using the returned ID.
* The function signature for this event takes a single `const` reference of type voice_client_speaking_t&, and returns void.
*/
event_router_t<voice_client_speaking_t> on_voice_client_speaking;
/**
* @brief Called when a log message is to be written to the log.
* You can attach any logging system here you wish, e.g. spdlog, or even just a simple
* use of std::cout or printf. If nothing attaches this log event, then the
* library will be silent.
*
* @note Use operator() to attach a lambda to this event, and the detach method to detach the listener using the returned ID.
* The function signature for this event takes a single `const` reference of type log_t&, and returns void.
*/
event_router_t<log_t> on_log;
/**
* @brief on guild join request delete.
* Triggered when a user declines the membership screening questionnaire for a guild.
*
* @note Use operator() to attach a lambda to this event, and the detach method to detach the listener using the returned ID.
* The function signature for this event takes a single `const` reference of type guild_join_request_delete_t&, and returns void.
*/
event_router_t<guild_join_request_delete_t> on_guild_join_request_delete;
/**
* @brief Called when a new interaction is created.
* Interactions are created by discord when commands you have registered are issued
* by a user. For an example of this in action please see \ref slashcommands
*
* @see https://discord.com/developers/docs/topics/gateway-events#interaction-create
* @note Use operator() to attach a lambda to this event, and the detach method to detach the listener using the returned ID.
* The function signature for this event takes a single `const` reference of type interaction_create_t&, and returns void.
*
* @note There are dedicated events to handle slashcommands (See dpp::cluster::on_slashcommand),
* user context menus (See dpp::cluster::on_user_context_menu) and message context menus (See dpp::cluster::on_message_context_menu)
*/
event_router_t<interaction_create_t> on_interaction_create;
/**
* @brief Called when a slash command is issued.
* Only dpp::ctxm_chat_input types of interaction are routed to this event.
* For an example of this in action please see \ref slashcommands
*
* @note Use operator() to attach a lambda to this event, and the detach method to detach the listener using the returned ID.
* The function signature for this event takes a single `const` reference of type slashcommand_t&, and returns void.
*/
event_router_t<slashcommand_t> on_slashcommand;
/**
* @brief Called when a button is clicked attached to a message.
* Button clicks are triggered by discord when buttons are clicked which you have
* associated with a message using dpp::component.
*
* @note Use operator() to attach a lambda to this event, and the detach method to detach the listener using the returned ID.
* The function signature for this event takes a single `const` reference of type button_click_t&, and returns void.
*/
event_router_t<button_click_t> on_button_click;
/**
* @brief Called when an auto completed field needs suggestions to present to the user
* This is triggered by discord when option choices have auto completion enabled which you have
* associated with a dpp::slashcommand.
*
* @note Use operator() to attach a lambda to this event, and the detach method to detach the listener using the returned ID.
* The function signature for this event takes a single `const` reference of type autocomplete_t&, and returns void.
*/
event_router_t<autocomplete_t> on_autocomplete;
/**
* @brief Called when a select menu is clicked attached to a message.
* Select menu clicks are triggered by discord when select menus are clicked which you have
* associated with a message using dpp::component.
*
* @note Use operator() to attach a lambda to this event, and the detach method to detach the listener using the returned ID.
* The function signature for this event takes a single `const` reference of type select_click_t&, and returns void.
*/
event_router_t<select_click_t> on_select_click;
/**
* @brief Called when a user right-clicks or long-presses on a message,
* where a slash command is bound to the dpp::ctxm_message command type.
*
* @note Use operator() to attach a lambda to this event, and the detach method to detach the listener using the returned ID.
* The function signature for this event takes a single `const` reference of type message_context_menu_t&, and returns void.
*/
event_router_t<message_context_menu_t> on_message_context_menu;
/**
* @brief Called when a user right-clicks or long-presses on a user,
* where a slash command is bound to the dpp::ctxm_user command type.
*
* @note Use operator() to attach a lambda to this event, and the detach method to detach the listener using the returned ID.
* The function signature for this event takes a single `const` reference of type user_context_menu_t&, and returns void.
*/
event_router_t<user_context_menu_t> on_user_context_menu;
/**
* @brief Called when a modal dialog is submitted.
* Form submits are triggered by discord when modal dialogs are submitted which you have
* associated with a slash command using dpp::interaction_modal_response.
*
* @note Use operator() to attach a lambda to this event, and the detach method to detach the listener using the returned ID.
* The function signature for this event takes a single `const` reference of type form_submit_t&, and returns void.
*/
event_router_t<form_submit_t> on_form_submit;
/**
* @brief Called when a guild is deleted.
* A guild can be deleted via the bot being kicked, the bot leaving the guild
* explicitly with dpp::cluster::guild_delete, or via the guild being unavailable due to
* an outage.
*
* @see https://discord.com/developers/docs/topics/gateway-events#guild-delete
* @note Use operator() to attach a lambda to this event, and the detach method to detach the listener using the returned ID.
* The function signature for this event takes a single `const` reference of type guild_delete_t&, and returns void.
*/
event_router_t<guild_delete_t> on_guild_delete;
/**
* @brief Called when a channel is deleted from a guild.
* The channel will still be temporarily available in the cache. Pointers to the
* channel should not be retained long-term as they will be deleted by the garbage
* collector.
*
* @see https://discord.com/developers/docs/topics/gateway-events#channel-delete
* @note Use operator() to attach a lambda to this event, and the detach method to detach the listener using the returned ID.
* The function signature for this event takes a single `const` reference of type channel_delete_t&, and returns void.
*/
event_router_t<channel_delete_t> on_channel_delete;
/**
* @brief Called when a channel is edited on a guild.
* The new channel details have already been applied to the guild when you
* receive this event.
*
* @see https://discord.com/developers/docs/topics/gateway-events#channel-update
* @note Use operator() to attach a lambda to this event, and the detach method to detach the listener using the returned ID.
* The function signature for this event takes a single `const` reference of type channel_update_t&, and returns void.
*/
event_router_t<channel_update_t> on_channel_update;
/**
* @brief Called when a shard is connected and ready.
* A set of cluster::on_guild_create events will follow this event.
*
* @see https://discord.com/developers/docs/topics/gateway-events#ready
* @note Use operator() to attach a lambda to this event, and the detach method to detach the listener using the returned ID.
* The function signature for this event takes a single `const` reference of type ready_t&, and returns void.
*/
event_router_t<ready_t> on_ready;
/**
* @brief Called when a message is deleted.
* The message has already been deleted from Discord when you
* receive this event.
*
* @see https://discord.com/developers/docs/topics/gateway-events#message-delete
* @note Use operator() to attach a lambda to this event, and the detach method to detach the listener using the returned ID.
* The function signature for this event takes a single `const` reference of type message_delete_t&, and returns void.
*/
event_router_t<message_delete_t> on_message_delete;
/**
* @brief Called when a user leaves a guild (either through being kicked, or choosing to leave)
*
* @see https://discord.com/developers/docs/topics/gateway-events#guild-member-remove
* @note Use operator() to attach a lambda to this event, and the detach method to detach the listener using the returned ID.
* The function signature for this event takes a single `const` reference of type guild_member_remove_t&, and returns void.
*/
event_router_t<guild_member_remove_t> on_guild_member_remove;
/**
* @brief Called when a connection to a shard successfully resumes.
* A resumed session does not need to re-synchronise guilds, members, etc.
* This is generally non-fatal and informational only.
*
* @see https://discord.com/developers/docs/topics/gateway-events#resumed
* @note Use operator() to attach a lambda to this event, and the detach method to detach the listener using the returned ID.
* The function signature for this event takes a single `const` reference of type resumed_t&, and returns void.
*/
event_router_t<resumed_t> on_resumed;
/**
* @brief Called when a new role is created on a guild.
*
* @see https://discord.com/developers/docs/topics/gateway-events#guild-role-create
* @note Use operator() to attach a lambda to this event, and the detach method to detach the listener using the returned ID.
* The function signature for this event takes a single `const` reference of type guild_role_create_t&, and returns void.
*/
event_router_t<guild_role_create_t> on_guild_role_create;
/**
* @brief Called when a user is typing on a channel.
*
* @see https://discord.com/developers/docs/topics/gateway-events#typing-start
* @note Use operator() to attach a lambda to this event, and the detach method to detach the listener using the returned ID.
* The function signature for this event takes a single `const` reference of type typing_start_t&, and returns void.
*/
event_router_t<typing_start_t> on_typing_start;
/**
* @brief Called when a new reaction is added to a message.
*
* @see https://discord.com/developers/docs/topics/gateway-events#message-reaction-add
* @note Use operator() to attach a lambda to this event, and the detach method to detach the listener using the returned ID.
* The function signature for this event takes a single `const` reference of type message_reaction_add_t&, and returns void.
*/
event_router_t<message_reaction_add_t> on_message_reaction_add;
/**
* @brief Called when a set of members is received for a guild.
* D++ will request these for all new guilds if needed, after the cluster::on_guild_create
* events.
*
* @see https://discord.com/developers/docs/topics/gateway-events#guild-members-chunk
* @note Use operator() to attach a lambda to this event, and the detach method to detach the listener using the returned ID.
* The function signature for this event takes a single `const` reference of type guild_members_chunk_t&, and returns void.
*/
event_router_t<guild_members_chunk_t> on_guild_members_chunk;
/**
* @brief Called when a single reaction is removed from a message.
*
* @see https://discord.com/developers/docs/topics/gateway-events#message-reaction-remove
* @note Use operator() to attach a lambda to this event, and the detach method to detach the listener using the returned ID.
* The function signature for this event takes a single `const` reference of type message_reaction_remove_t&, and returns void.
*/
event_router_t<message_reaction_remove_t> on_message_reaction_remove;
/**
* @brief Called when a new guild is created.
* D++ will request members for the guild for its cache using guild_members_chunk.
*
* @warning If the cache policy has disabled guild caching, the pointer in this event will become invalid after the
* event ends. You should make a copy of any data you wish to preserve beyond this.
*
* @see https://discord.com/developers/docs/topics/gateway-events#guild-create
* @note Use operator() to attach a lambda to this event, and the detach method to detach the listener using the returned ID.
* The function signature for this event takes a single `const` reference of type guild_create_t&, and returns void.
*/
event_router_t<guild_create_t> on_guild_create;
/**
* @brief Called when a new channel is created on a guild.
*
* @warning If the cache policy has disabled channel caching, the pointer in this event will become invalid after the
* event ends. You should make a copy of any data you wish to preserve beyond this.
*
* @see https://discord.com/developers/docs/topics/gateway-events#channel-create
* @note Use operator() to attach a lambda to this event, and the detach method to detach the listener using the returned ID.
* The function signature for this event takes a single `const` reference of type channel_create_t&, and returns void.
*/
event_router_t<channel_create_t> on_channel_create;
/**
* @brief Called when all reactions for a particular emoji are removed from a message.
*
* @see https://discord.com/developers/docs/topics/gateway-events#message-reaction-remove-emoji
* @note Use operator() to attach a lambda to this event, and the detach method to detach the listener using the returned ID.
* The function signature for this event takes a single `const` reference of type message_reaction_remove_emoji_t&, and returns void.
*/
event_router_t<message_reaction_remove_emoji_t> on_message_reaction_remove_emoji;
/**
* @brief Called when multiple messages are deleted from a channel or DM.
*
* @see https://discord.com/developers/docs/topics/gateway-events#message-delete-bulk
* @note Use operator() to attach a lambda to this event, and the detach method to detach the listener using the returned ID.
* The function signature for this event takes a single `const` reference of type message_delete_bulk_t&, and returns void.
*/
event_router_t<message_delete_bulk_t> on_message_delete_bulk;
/**
* @brief Called when an existing role is updated on a guild.
*
* @warning If the cache policy has disabled guild caching, the pointer to the guild in this event may be nullptr.
*
* @see https://discord.com/developers/docs/topics/gateway-events#guild-role-update
* @note Use operator() to attach a lambda to this event, and the detach method to detach the listener using the returned ID.
* The function signature for this event takes a single `const` reference of type guild_role_update_t&, and returns void.
*/
event_router_t<guild_role_update_t> on_guild_role_update;
/**
* @brief Called when a role is deleted in a guild.
*
* @warning If the cache policy has disabled guild caching, the pointer to the guild in this event may be nullptr.
*
* @see https://discord.com/developers/docs/topics/gateway-events#guild-role-delete
* @note Use operator() to attach a lambda to this event, and the detach method to detach the listener using the returned ID.
* The function signature for this event takes a single `const` reference of type guild_role_delete_t&, and returns void.
*/
event_router_t<guild_role_delete_t> on_guild_role_delete;
/**
* @brief Called when a message is pinned.
* Note that the pinned message is not returned to this event, just the timestamp
* of the last pinned message.
*
* @see https://discord.com/developers/docs/topics/gateway-events#channel-pins-update
* @note Use operator() to attach a lambda to this event, and the detach method to detach the listener using the returned ID.
* The function signature for this event takes a single `const` reference of type channel_pins_update_t&, and returns void.
*/
event_router_t<channel_pins_update_t> on_channel_pins_update;
/**
* @brief Called when all reactions are removed from a message.
*
* @see https://discord.com/developers/docs/topics/gateway-events#message-reaction-remove-all
* @note Use operator() to attach a lambda to this event, and the detach method to detach the listener using the returned ID.
* The function signature for this event takes a single `const` reference of type message_reaction_remove_all_t&, and returns void.
*/
event_router_t<message_reaction_remove_all_t> on_message_reaction_remove_all;
/**
* @brief Called when we are told which voice server we can use.
* This will be sent either when we establish a new voice channel connection,
* or as discord rearrange their infrastructure.
*
* @warning If the cache policy has disabled guild caching, the pointer to the guild in this event may be nullptr.
*
* @note Use operator() to attach a lambda to this event, and the detach method to detach the listener using the returned ID.
* The function signature for this event takes a single `const` reference of type voice_server_update_t&, and returns void.
*/
event_router_t<voice_server_update_t> on_voice_server_update;
/**
* @brief Called when new emojis are added to a guild.
* The complete set of emojis is sent every time.
*
* @warning If the cache policy has disabled guild caching, the pointer to the guild in this event may be nullptr.
*
* @see https://discord.com/developers/docs/topics/gateway-events#guild-emojis-update