From 4062965e2a96759e0fb6c94f038bb3c35a5e0f56 Mon Sep 17 00:00:00 2001 From: ruslan-ilesik Date: Tue, 8 Oct 2024 22:27:38 +0200 Subject: [PATCH 01/10] try to fix formatting --- include/dpp/cluster.h | 70 +++++++++++++++++++++++++++++++++++++++++++ src/dpp/cluster.cpp | 60 +++++++++++++++++++++++++++++++++++++ 2 files changed, 130 insertions(+) diff --git a/include/dpp/cluster.h b/include/dpp/cluster.h index 4dad2327f2..39641f98ca 100644 --- a/include/dpp/cluster.h +++ b/include/dpp/cluster.h @@ -133,6 +133,38 @@ class DPP_EXPORT cluster { */ 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; + +#ifdef DPP_CORO + /** + * @brief Typedef for coroutines based slashcommand handler type + */ + using co_slashcommand_handler_t = std::function(const slashcommand_t&)>; + + /** + * @brief Typedef for variant of coroutines based slashcommand handler type and regular version of it + */ + using slashcommand_handler_variant = std::variant; + + /** + * @brief Container to store relation between command name and it's handler + */ + std::map named_commands; +#else + /** + * @brief Container to store relation between command name and it's handler + */ + std::map named_commands; +#endif + /** * @brief Tick active timers */ @@ -436,6 +468,44 @@ class DPP_EXPORT cluster { /* 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); + +#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. + */ + bool co_register_command(const std::string& name, const co_slashcommand_handler_t handler); +#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 * diff --git a/src/dpp/cluster.cpp b/src/dpp/cluster.cpp index ff7345428c..3399536ef3 100644 --- a/src/dpp/cluster.cpp +++ b/src/dpp/cluster.cpp @@ -120,6 +120,40 @@ cluster::cluster(const std::string &_token, uint32_t _intents, uint32_t _shards, i_message_content, "You have attached an event to cluster::on_message_update() but have not specified the privileged intent dpp::i_message_content. Message content, embeds, attachments, and components on received guild messages will be empty.") ); + + /* Add slashcommand callback for named commands. */ +#ifdef DPP_CORO + on_slashcommand([this](const slashcommand_t& event) -> task { + slashcommand_handler_variant copy; + { + std::shared_lock lk(named_commands_mutex); + auto it = named_commands.find(event.command.get_command_name()); + if (it == named_commands.end()) { + co_return; // Command not found + } + copy = it->second; + } + if (std::holds_alternative(copy)) { + co_await std::get(copy)(event); + } else if (std::holds_alternative(copy)) { + std::get(copy)(event); + } + co_return; + }); +#else + on_slashcommand([this](const slashcommand_t& event) { + slashcommand_handler_t copy; + { + std::shared_lock lk(named_commands_mutex); + auto it = named_commands.find(event.command.get_command_name()); + if (it == named_commands.end()) { + return; // Command not found + } + copy = it->second; + } + copy(event); + }); +#endif } cluster::~cluster() @@ -466,4 +500,30 @@ cluster& cluster::set_request_timeout(uint16_t timeout) { return *this; } +bool cluster::register_command(const std::string &name, const slashcommand_handler_t handler) { + std::unique_lock lk(named_commands_mutex); + if (named_commands.count(name) != 0) { + return false; + } + named_commands.emplace(name, handler); + return true; +} + +#ifdef DPP_CORO +bool cluster::co_register_command(const std::string& name, const co_slashcommand_handler_t handler) { + std::unique_lock lk(named_commands_mutex); + if (named_commands.count(name) != 0) { + return false; + } + named_commands.emplace(name, handler); + return true; +} +#endif + +bool cluster::unregister_command(const std::string &name) { + std::unique_lock lk(named_commands_mutex); + return named_commands.erase(name) == 1; +} + + }; From 2d1f71bffd6c1a3ac7f13c962d6a137bd29f177e Mon Sep 17 00:00:00 2001 From: ruslan-ilesik Date: Tue, 8 Oct 2024 22:52:15 +0200 Subject: [PATCH 02/10] fix with templates --- include/dpp/cluster.h | 11 ++++++++++- src/dpp/cluster.cpp | 11 ----------- 2 files changed, 10 insertions(+), 12 deletions(-) diff --git a/include/dpp/cluster.h b/include/dpp/cluster.h index 39641f98ca..3e817711da 100644 --- a/include/dpp/cluster.h +++ b/include/dpp/cluster.h @@ -489,7 +489,16 @@ class DPP_EXPORT cluster { * @return bool Returns `true` if the command was registered successfully, or `false` if * the command with the same name already exists. */ - bool co_register_command(const std::string& name, const co_slashcommand_handler_t handler); + template + std::enable_if_t, dpp::task>, bool> + register_command(const std::string& name, F&& handler){ + std::unique_lock lk(named_commands_mutex); + if (named_commands.count(name) != 0) { + return false; + } + named_commands.emplace(name, handler); + return true; + }; #endif /** diff --git a/src/dpp/cluster.cpp b/src/dpp/cluster.cpp index 3399536ef3..36a60d249c 100644 --- a/src/dpp/cluster.cpp +++ b/src/dpp/cluster.cpp @@ -509,17 +509,6 @@ bool cluster::register_command(const std::string &name, const slashcommand_handl return true; } -#ifdef DPP_CORO -bool cluster::co_register_command(const std::string& name, const co_slashcommand_handler_t handler) { - std::unique_lock lk(named_commands_mutex); - if (named_commands.count(name) != 0) { - return false; - } - named_commands.emplace(name, handler); - return true; -} -#endif - bool cluster::unregister_command(const std::string &name) { std::unique_lock lk(named_commands_mutex); return named_commands.erase(name) == 1; From 90fb093e550d626998e5064f478982f3b7653b97 Mon Sep 17 00:00:00 2001 From: ruslan-ilesik Date: Tue, 8 Oct 2024 23:06:24 +0200 Subject: [PATCH 03/10] remove comments --- src/dpp/cluster.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/dpp/cluster.cpp b/src/dpp/cluster.cpp index 36a60d249c..105bab0bf2 100644 --- a/src/dpp/cluster.cpp +++ b/src/dpp/cluster.cpp @@ -129,7 +129,7 @@ cluster::cluster(const std::string &_token, uint32_t _intents, uint32_t _shards, std::shared_lock lk(named_commands_mutex); auto it = named_commands.find(event.command.get_command_name()); if (it == named_commands.end()) { - co_return; // Command not found + co_return; } copy = it->second; } @@ -147,7 +147,7 @@ cluster::cluster(const std::string &_token, uint32_t _intents, uint32_t _shards, std::shared_lock lk(named_commands_mutex); auto it = named_commands.find(event.command.get_command_name()); if (it == named_commands.end()) { - return; // Command not found + return; } copy = it->second; } From bad343db0347705675b2fe2401cf0969ffdf1d40 Mon Sep 17 00:00:00 2001 From: ruslan-ilesik Date: Tue, 8 Oct 2024 23:07:31 +0200 Subject: [PATCH 04/10] fix other thing --- include/dpp/cluster.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/include/dpp/cluster.h b/include/dpp/cluster.h index 3e817711da..8c96862f2f 100644 --- a/include/dpp/cluster.h +++ b/include/dpp/cluster.h @@ -495,7 +495,7 @@ class DPP_EXPORT cluster { std::unique_lock lk(named_commands_mutex); if (named_commands.count(name) != 0) { return false; - } + } named_commands.emplace(name, handler); return true; }; From a8306527b3b4a2f17821846a447c040f49b38b0a Mon Sep 17 00:00:00 2001 From: ruslan-ilesik Date: Tue, 8 Oct 2024 23:08:11 +0200 Subject: [PATCH 05/10] fix other thing --- src/dpp/cluster.cpp | 1 - 1 file changed, 1 deletion(-) diff --git a/src/dpp/cluster.cpp b/src/dpp/cluster.cpp index 105bab0bf2..b08a5a1885 100644 --- a/src/dpp/cluster.cpp +++ b/src/dpp/cluster.cpp @@ -514,5 +514,4 @@ bool cluster::unregister_command(const std::string &name) { return named_commands.erase(name) == 1; } - }; From 083e204d23668f520331af9d73ff8696375d0514 Mon Sep 17 00:00:00 2001 From: ruslan-ilesik Date: Tue, 8 Oct 2024 23:08:39 +0200 Subject: [PATCH 06/10] fix other thing --- include/dpp/cluster.h | 1 - 1 file changed, 1 deletion(-) diff --git a/include/dpp/cluster.h b/include/dpp/cluster.h index 8c96862f2f..4e8734b125 100644 --- a/include/dpp/cluster.h +++ b/include/dpp/cluster.h @@ -514,7 +514,6 @@ class DPP_EXPORT cluster { */ bool unregister_command(const std::string& name); - /** * @brief on voice state update event * From 376870f7f6c3ee8ac646fe3de561e521f4ecd6ed Mon Sep 17 00:00:00 2001 From: "Craig Edwards (Brain)" Date: Tue, 8 Oct 2024 22:28:24 +0100 Subject: [PATCH 07/10] style: indentation of lambdas --- src/dpp/cluster.cpp | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/src/dpp/cluster.cpp b/src/dpp/cluster.cpp index b08a5a1885..544902ade1 100644 --- a/src/dpp/cluster.cpp +++ b/src/dpp/cluster.cpp @@ -126,12 +126,12 @@ cluster::cluster(const std::string &_token, uint32_t _intents, uint32_t _shards, on_slashcommand([this](const slashcommand_t& event) -> task { slashcommand_handler_variant copy; { - std::shared_lock lk(named_commands_mutex); - auto it = named_commands.find(event.command.get_command_name()); - if (it == named_commands.end()) { - co_return; - } - copy = it->second; + std::shared_lock lk(named_commands_mutex); + auto it = named_commands.find(event.command.get_command_name()); + if (it == named_commands.end()) { + co_return; + } + copy = it->second; } if (std::holds_alternative(copy)) { co_await std::get(copy)(event); @@ -144,12 +144,12 @@ cluster::cluster(const std::string &_token, uint32_t _intents, uint32_t _shards, on_slashcommand([this](const slashcommand_t& event) { slashcommand_handler_t copy; { - std::shared_lock lk(named_commands_mutex); - auto it = named_commands.find(event.command.get_command_name()); - if (it == named_commands.end()) { - return; - } - copy = it->second; + std::shared_lock lk(named_commands_mutex); + auto it = named_commands.find(event.command.get_command_name()); + if (it == named_commands.end()) { + return; + } + copy = it->second; } copy(event); }); From 2a36704a87c5aa01e8fc5a3e0800931111563a5b Mon Sep 17 00:00:00 2001 From: "Craig Edwards (Brain)" Date: Wed, 9 Oct 2024 00:01:46 +0100 Subject: [PATCH 08/10] refactor: better registration with try_emplace --- src/dpp/cluster.cpp | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/src/dpp/cluster.cpp b/src/dpp/cluster.cpp index 544902ade1..26e333d194 100644 --- a/src/dpp/cluster.cpp +++ b/src/dpp/cluster.cpp @@ -502,11 +502,8 @@ cluster& cluster::set_request_timeout(uint16_t timeout) { bool cluster::register_command(const std::string &name, const slashcommand_handler_t handler) { std::unique_lock lk(named_commands_mutex); - if (named_commands.count(name) != 0) { - return false; - } - named_commands.emplace(name, handler); - return true; + auto [_, inserted] = named_commands.try_emplace(name, handler); + return inserted; } bool cluster::unregister_command(const std::string &name) { From 08b3841a8cf362d97fcea64c178dc12eeeb19a54 Mon Sep 17 00:00:00 2001 From: "Craig Edwards (Brain)" Date: Wed, 9 Oct 2024 00:02:56 +0100 Subject: [PATCH 09/10] Update cluster.h --- include/dpp/cluster.h | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/include/dpp/cluster.h b/include/dpp/cluster.h index 4e8734b125..787b95d854 100644 --- a/include/dpp/cluster.h +++ b/include/dpp/cluster.h @@ -493,11 +493,8 @@ class DPP_EXPORT cluster { std::enable_if_t, dpp::task>, bool> register_command(const std::string& name, F&& handler){ std::unique_lock lk(named_commands_mutex); - if (named_commands.count(name) != 0) { - return false; - } - named_commands.emplace(name, handler); - return true; + auto [_, inserted] = named_commands.try_emplace(name, handler); + return inserted; }; #endif From 95270f523199ff5b463872fc9fdcf911534258cf Mon Sep 17 00:00:00 2001 From: "Craig Edwards (Brain)" Date: Wed, 9 Oct 2024 00:08:42 +0100 Subject: [PATCH 10/10] refactor: std::forward --- include/dpp/cluster.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/include/dpp/cluster.h b/include/dpp/cluster.h index 787b95d854..0b6a980e65 100644 --- a/include/dpp/cluster.h +++ b/include/dpp/cluster.h @@ -493,7 +493,7 @@ class DPP_EXPORT cluster { std::enable_if_t, dpp::task>, bool> register_command(const std::string& name, F&& handler){ std::unique_lock lk(named_commands_mutex); - auto [_, inserted] = named_commands.try_emplace(name, handler); + auto [_, inserted] = named_commands.try_emplace(name, std::forward(handler)); return inserted; }; #endif