summaryrefslogtreecommitdiff
path: root/bot/src/main.cpp
blob: da38d438963374f5bcb6c5c36203555546b5b817 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
#include <dpp/dpp.h>
#include <string>
#include "../include/utils.hpp"
#include "../include/http_webhook_server.hpp"
#include <thread>

int main(int argc, char* argv[]) {
    if (argc > 2) {
        setenv("BOT_TOKEN", argv[1], 1);
        setenv("PORT", argv[2], 1);
    }

    const std::string BOT_TOKEN = getenv("BOT_TOKEN");
    const std::string PORT = getenv("PORT");

    dpp::cluster bot(BOT_TOKEN);
    std::unique_ptr<nlohmann::json> json_data = std::make_unique<nlohmann::json>();

    bot.on_log(dpp::utility::cout_logger());

    bot.on_slashcommand([&json_data, &bot](const dpp::slashcommand_t& event) {
        std::unordered_map<std::string, std::string> key_values = app::generate_key_values(event);
        std::string command_name = event.command.get_command_name();
        std::string response = "Interaction found, but no response found.";

        if (!command_name.empty() && json_data->contains(command_name)) {
            auto& command_data = (*json_data)[command_name];
            bool no_error = true;
            if (command_data.contains("action")) {
                auto& action = command_data["action"];
                // Actions are a list of Objects
                if (action.is_array()) {
                   no_error = app::handle_actions(event, action, key_values, bot);
                }
            }
            if (command_data.contains("response") && no_error) {
                response = command_data["response"];
                std::cout << "Command: " << command_name << " → Response: " << response << std::endl;
            }
        }

        event.reply(app::update_string(response, key_values));
    });

    bot.on_ready([&bot, &json_data, &PORT](const dpp::ready_t& event) {
        if (dpp::run_once<struct register_bot_commands>()) {
            std::thread http_thread([&json_data, &PORT]() {
                try {
                    HttpWebhookServer server(std::stoi(PORT), [&json_data](const HttpWebhookServer::HttpRequest& req) {
                        HttpWebhookServer::HttpResponse res;

                        if (req.method == "POST") {
                            res.status_code = 200;
                            res.headers["Content-Type"] = "application/json";

                            try {
                                nlohmann::json body_json = app::json_from_string(req.body);
                                res.body = R"({"received": "POST request received"})";

                                if (body_json.contains("command") && body_json["command"] == "update") {
                                    json_data = std::make_unique<nlohmann::json>(body_json["data"]);
                                }
                            } catch (const std::exception& e) {
                                res.status_code = 400;
                                res.body = std::string("{\"error\": \"") + e.what() + "\"}";
                            }
                        } else {
                            res.status_code = 400;
                            res.headers["Content-Type"] = "text/plain";
                            res.body = "Invalid request method.";
                        }

                        return res;
                    });

                    std::cout << "[BOT] Webhook server running on port " << PORT << "..." << std::endl;
                    server.start();
                } catch (const std::exception& e) {
                    std::cerr << "[BOT] Server error: " << e.what() << std::endl;
                }
            });

            http_thread.detach();
        }
    });

    bot.start(dpp::st_wait);
}