summaryrefslogtreecommitdiff
path: root/bot/src
diff options
context:
space:
mode:
authorgarder500 <jeremy27.clara22@gmail.com>2025-04-27 16:06:19 +0200
committergarder500 <jeremy27.clara22@gmail.com>2025-04-27 16:06:19 +0200
commit7cbd16f1b3115d3335c61e7c82e349594a726f52 (patch)
tree7a36ded6b73d924b6891ac052140f69bf73c33ac /bot/src
parentb3791125200154278d31514217c4f417d8961e84 (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/src')
-rw-r--r--bot/src/main.cpp81
1 files changed, 69 insertions, 12 deletions
diff --git a/bot/src/main.cpp b/bot/src/main.cpp
index 500170d..06a03b0 100644
--- a/bot/src/main.cpp
+++ b/bot/src/main.cpp
@@ -1,26 +1,83 @@
#include <dpp/dpp.h>
+#include <string>
#include "../include/utils.hpp"
-const std::string BOT_TOKEN = getenv("BOT_TOKEN");
+#include "../include/server.cpp"
-int main() {
- dpp::cluster bot(BOT_TOKEN);
+int main(int argc, char *argv[])
+{
+ if (argc > 1)
+ {
+ std::string token = argv[1];
+ setenv("BOT_TOKEN", token.c_str(), 1);
+ }
+
+ 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([](const dpp::slashcommand_t& event) {
+ bot.on_slashcommand([&json_data](const dpp::slashcommand_t &event)
+ {
// let's generate the key-value map
- std::map<std::string, std::string> key_values = app::generate_key_values(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 = "Pong! ((userName))";
-
- if (event.command.get_command_name() == "ping") {
- event.reply(app::update_string(response, key_values));
+ 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
+ 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"))
+ {
+ std::cout << "Response found: " << command_data.at("response") << std::endl;
+ response = command_data.at("response");
+ }else
+ {
+ // let's display the command data
+ std::cout << "Command data: " << command_data.dump(4) << std::endl;
+ std::cout << "No response found for command: " << event.command.get_command_name() << std::endl;
+ }
+ }
}
+ event.reply(app::update_string(response, key_values));
});
- bot.on_ready([&bot](const dpp::ready_t& event) {
- if (dpp::run_once<struct register_bot_commands>()) {
- bot.global_command_create(dpp::slashcommand("ping", "Ping pong!", bot.me.id));
+ 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
+
+ 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);
+ }
+ // ... [gestion des messages entrants]
+ }
+ });
}
});