From 7cbd16f1b3115d3335c61e7c82e349594a726f52 Mon Sep 17 00:00:00 2001 From: garder500 Date: Sun, 27 Apr 2025 16:06:19 +0200 Subject: Ajout de la logique de connexion et de gestion des signaux pour le bot Discord, ainsi que des améliorations dans la gestion des chaînes JSON et des mises à jour des commandes. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- bot/include/utils.cpp | 38 +++++++++++++++++++++++++++++++++----- 1 file changed, 33 insertions(+), 5 deletions(-) (limited to 'bot/include/utils.cpp') diff --git a/bot/include/utils.cpp b/bot/include/utils.cpp index 6a47d42..081fd4e 100644 --- a/bot/include/utils.cpp +++ b/bot/include/utils.cpp @@ -20,7 +20,7 @@ namespace app return g.get_icon_url(1024, i_webp); } - std::string update_string(const std::string &initial, const std::map &updates) + std::string update_string(const std::string &initial, const std::unordered_map &updates) { static const std::regex placeholderRegex(R"(\(\((.*?)\)\))", std::regex::icase); @@ -62,12 +62,12 @@ namespace app return result; } // Forward declaration - void process_interaction_option(const slashcommand_t &event, const command_data_option &option, std::map &kv); + void process_interaction_option(const slashcommand_t &event, const command_data_option &option, std::unordered_map &kv); // Génère la map clé/valeur - std::map generate_key_values(const slashcommand_t &event) + std::unordered_map generate_key_values(const slashcommand_t &event) { - std::map key_values; + std::unordered_map key_values; const guild *g = event.command.is_guild_interaction() ? &event.command.get_guild() : nullptr; const channel &c = event.command.get_channel(); const user &u = event.command.get_issuing_user(); @@ -98,7 +98,7 @@ namespace app } // Traite une option d'interaction récursivement - void process_interaction_option(const slashcommand_t &event, const command_data_option &option, std::map &kv) + void process_interaction_option(const slashcommand_t &event, const command_data_option &option, std::unordered_map &kv) { switch (option.type) { @@ -184,4 +184,32 @@ namespace app break; } } + + nlohmann::json json_from_string(const std::string &str) + { + nlohmann::json j; + try + { + j = nlohmann::json::parse(str); + } + catch (const nlohmann::json::parse_error &e) + { + std::cerr << "JSON parse error: " << e.what() << std::endl; + } + return j; + } + + std::string string_from_json(const nlohmann::json &j) + { + std::string str; + try + { + str = j.dump(); + } + catch (const nlohmann::json::exception &e) + { + std::cerr << "JSON exception: " << e.what() << std::endl; + } + return str; + } } -- cgit v1.2.3