diff options
| author | garder500 <jeremy27.clara22@gmail.com> | 2025-04-27 16:06:19 +0200 |
|---|---|---|
| committer | garder500 <jeremy27.clara22@gmail.com> | 2025-04-27 16:06:19 +0200 |
| commit | 7cbd16f1b3115d3335c61e7c82e349594a726f52 (patch) | |
| tree | 7a36ded6b73d924b6891ac052140f69bf73c33ac /bot/include/utils.cpp | |
| parent | b3791125200154278d31514217c4f417d8961e84 (diff) | |
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.
Diffstat (limited to 'bot/include/utils.cpp')
| -rw-r--r-- | bot/include/utils.cpp | 38 |
1 files changed, 33 insertions, 5 deletions
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<std::string, std::string> &updates) + std::string update_string(const std::string &initial, const std::unordered_map<std::string, std::string> &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<std::string, std::string> &kv); + void process_interaction_option(const slashcommand_t &event, const command_data_option &option, std::unordered_map<std::string, std::string> &kv); // Génère la map clé/valeur - std::map<std::string, std::string> generate_key_values(const slashcommand_t &event) + std::unordered_map<std::string, std::string> generate_key_values(const slashcommand_t &event) { - std::map<std::string, std::string> key_values; + std::unordered_map<std::string, std::string> 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<std::string, std::string> &kv) + void process_interaction_option(const slashcommand_t &event, const command_data_option &option, std::unordered_map<std::string, std::string> &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; + } } |
