diff options
| author | soler_j <soler_j@etna-alternance.net> | 2025-04-29 03:07:48 +0200 |
|---|---|---|
| committer | soler_j <soler_j@etna-alternance.net> | 2025-04-29 03:07:48 +0200 |
| commit | 2a58d43c3b820eb8ffec01bb51905146a7278533 (patch) | |
| tree | d05b0b815aee8520ae1375b69a503511f7b260fa /bot/src | |
| parent | 0c53f3e452830b300c598c9370946ae54f443a78 (diff) | |
Ajout de la prise en charge de ZeroMQ dans le bot, mise à jour des dépendances et réorganisation du Dockerfile. Suppression de l'implémentation de serveur Unix obsolète et amélioration de la gestion des messages.
Diffstat (limited to 'bot/src')
| -rw-r--r-- | bot/src/main.cpp | 90 |
1 files changed, 42 insertions, 48 deletions
diff --git a/bot/src/main.cpp b/bot/src/main.cpp index 06a03b0..853e829 100644 --- a/bot/src/main.cpp +++ b/bot/src/main.cpp @@ -1,12 +1,11 @@ #include <dpp/dpp.h> #include <string> +#include <zmq.hpp> #include "../include/utils.hpp" -#include "../include/server.cpp" +#include <thread> -int main(int argc, char *argv[]) -{ - if (argc > 1) - { +int main(int argc, char *argv[]) { + if (argc > 1) { std::string token = argv[1]; setenv("BOT_TOKEN", token.c_str(), 1); } @@ -14,32 +13,21 @@ int main(int argc, char *argv[]) const std::string BOT_TOKEN = getenv("BOT_TOKEN"); dpp::cluster bot(BOT_TOKEN); - std::unique_ptr<UnixSocketServer> server; std::unique_ptr<nlohmann::json> json_data = std::make_unique<nlohmann::json>(); + bot.on_log(dpp::utility::cout_logger()); - bot.on_slashcommand([&json_data](const dpp::slashcommand_t &event) - { - // let's generate the key-value map + bot.on_slashcommand([&json_data](const dpp::slashcommand_t &event) { std::unordered_map<std::string, std::string> key_values = app::generate_key_values(event); - // let's create a string to send std::string response = "Interaction found, but no response found."; - // let's first check if it's a command or not. - if(event.command.get_command_name() != ""){ - // let's check if the command is in the json_data - // display json_data as string in the console - if (json_data->contains(event.command.get_command_name())) - { - // let's check if it does exist + if (event.command.get_command_name() != "") { + if (json_data->contains(event.command.get_command_name())) { std::cout << "Command found: " << event.command.get_command_name() << std::endl; auto command_data = json_data->at(event.command.get_command_name()); - if (command_data.contains("response")) - { + if (command_data.contains("response")) { std::cout << "Response found: " << command_data.at("response") << std::endl; response = command_data.at("response"); - }else - { - // let's display the command data + } else { std::cout << "Command data: " << command_data.dump(4) << std::endl; std::cout << "No response found for command: " << event.command.get_command_name() << std::endl; } @@ -48,36 +36,42 @@ int main(int argc, char *argv[]) event.reply(app::update_string(response, key_values)); }); - bot.on_ready([&bot, &server, &json_data](const dpp::ready_t &event) - { - if (dpp::run_once<struct register_bot_commands>()) - { - // let's start the server - std::string socket_path = "/tmp/" + bot.me.id.str() + ".sock"; - server = std::make_unique<UnixSocketServer>(socket_path); // Création explicite + bot.on_ready([&bot, &json_data](const dpp::ready_t &event) { + if (dpp::run_once<struct register_bot_commands>()) { + // Lancer la boucle ZMQ dans un thread séparé + std::thread zmq_thread([&json_data]() { + zmq::context_t ctx; + zmq::socket_t responder(ctx, zmq::socket_type::req); + responder.connect("tcp://localhost:5555"); + zmq::message_t ready_msg(5); + memcpy(ready_msg.data(), "ready", 5); + responder.send(ready_msg, zmq::send_flags::none); - server->start([&json_data, &server](const std::string& msg) - { - // Traitement du message reçu - nlohmann::json j = app::json_from_string(msg); - if (j.contains("command")) - { - std::string command = j["command"]; - // ... [traitement de la commande] - if (command == "update") - { - // ... [traitement de la commande update] - json_data = std::make_unique<nlohmann::json>(j["data"]); - } - else if (command == "stop") - { - server->stop(); - std::cout << "Server stopped." << std::endl; - exit(0); + while (true) { + zmq::message_t reply; + if (responder.recv(reply, zmq::recv_flags::none)) { + std::string json_str(static_cast<char*>(reply.data()), reply.size()); + try { + nlohmann::json j = app::json_from_string(json_str); + if (j.contains("command")) { + std::string command = j["command"]; + if (command == "update") { + json_data = std::make_unique<nlohmann::json>(j["data"]); + } + } + // Répondre de nouveau si nécessaire + zmq::message_t ping(4); + memcpy(ping.data(), "pong", 4); + responder.send(ping, zmq::send_flags::none); + } catch (const std::exception& e) { + std::cerr << "[BOT] Error parsing JSON: " << e.what() << std::endl; + } } - // ... [gestion des messages entrants] } + }); + + zmq_thread.detach(); // Le thread tourne en fond } }); |
