diff options
| author | soler_j <soler_j@etna-alternance.net> | 2025-04-30 22:30:54 +0200 | 
|---|---|---|
| committer | soler_j <soler_j@etna-alternance.net> | 2025-04-30 22:30:54 +0200 | 
| commit | 976c7a7466ef2852607a83ead4e0ed93550742d8 (patch) | |
| tree | f3a6996d9b2e283b2501cf1d8b8f0bb82a21a967 /bot | |
| parent | e557ff8f867d777332ab397ba5b3b6be57767972 (diff) | |
Refactor project structure and remove unused files
- Updated CMakeLists.txt to include source files from the 'src' directory instead of 'include'.
- Deleted 'http_webhook_server.cpp' and 'utils.cpp' files as they were no longer needed.
- Added 'handle_actions.hpp' and 'handle_actions.cpp' to manage slash command actions.
- Implemented 'http_webhook_server.cpp' to handle HTTP webhook server functionality.
- Updated 'utils.hpp' to include necessary headers and declarations.
- Refactored 'main.cpp' to include 'handle_actions.hpp' and updated slash command handling logic.
- Enhanced 'utils.cpp' with utility functions for handling user and guild data.
Diffstat (limited to 'bot')
| -rw-r--r-- | bot/CMakeLists.txt | 2 | ||||
| -rw-r--r-- | bot/include/handle_actions.hpp | 3 | ||||
| -rw-r--r-- | bot/include/http_webhook_server.hpp | 2 | ||||
| -rw-r--r-- | bot/include/utils.hpp | 4 | ||||
| -rw-r--r-- | bot/src/handle_actions.cpp | 136 | ||||
| -rw-r--r-- | bot/src/http_webhook_server.cpp (renamed from bot/include/http_webhook_server.cpp) | 2 | ||||
| -rw-r--r-- | bot/src/main.cpp | 25 | ||||
| -rw-r--r-- | bot/src/utils.cpp (renamed from bot/include/utils.cpp) | 87 | 
8 files changed, 164 insertions, 97 deletions
diff --git a/bot/CMakeLists.txt b/bot/CMakeLists.txt index 2ac0346..048faaf 100644 --- a/bot/CMakeLists.txt +++ b/bot/CMakeLists.txt @@ -11,7 +11,7 @@ find_package(dpp REQUIRED      HINTS ${DPP_INCLUDE_DIR}  # Add path if needed: /nix/store/.../include  )  file(GLOB_RECURSE SRC_FILES -    include/*.cpp +    src/*.cpp      include/*.hpp  )  # Add executable diff --git a/bot/include/handle_actions.hpp b/bot/include/handle_actions.hpp new file mode 100644 index 0000000..107e93d --- /dev/null +++ b/bot/include/handle_actions.hpp @@ -0,0 +1,3 @@ +#include <dpp/dpp.h> + +dpp::task<bool> handle_actions(const dpp::slashcommand_t& event, const nlohmann::json& actions, const std::unordered_map<std::string, std::string>& key_values); diff --git a/bot/include/http_webhook_server.hpp b/bot/include/http_webhook_server.hpp index f840a16..a42829f 100644 --- a/bot/include/http_webhook_server.hpp +++ b/bot/include/http_webhook_server.hpp @@ -1,3 +1,5 @@ +#pragma once +  #include <sys/epoll.h>  #include <netinet/in.h>  #include <functional> diff --git a/bot/include/utils.hpp b/bot/include/utils.hpp index 3052ecf..820fd01 100644 --- a/bot/include/utils.hpp +++ b/bot/include/utils.hpp @@ -1,5 +1,5 @@  // utils.hpp - +#pragma once  #ifndef UTILS_HPP  #define UTILS_HPP @@ -63,7 +63,7 @@ namespace app       * @param actions The JSON object containing actions to be handled       * @param key_values The map of key-value pairs to be used in the actions       */ -    bool handle_actions(const slashcommand_t &event, const nlohmann::json &actions, const std::unordered_map<std::string, std::string> &key_values, dpp::cluster &bot); +    bool handle_actions(const slashcommand_t &event, const nlohmann::json &actions, const std::unordered_map<std::string, std::string> &key_values);      /**       * @brief Parses a JSON string into a JSON object diff --git a/bot/src/handle_actions.cpp b/bot/src/handle_actions.cpp new file mode 100644 index 0000000..38d9982 --- /dev/null +++ b/bot/src/handle_actions.cpp @@ -0,0 +1,136 @@ +#include <dpp/dpp.h> + +dpp::task<bool> handle_actions(const dpp::slashcommand_t &event, const nlohmann::json &actions, const std::unordered_map<std::string, std::string> &key_values) +{ + +    dpp::cluster *cluster = event.owner; +    dpp::user user_ptr = event.command.get_issuing_user(); +    dpp::async thinking = event.co_thinking(false); +    if (actions.is_array()) +    { +        int i = 0; +        for (const auto &action : actions) +        { +            i++; +            if (action.contains("type")) +            { +                std::string action_type = action["type"]; +                if (action_type == "delete_messages" && event.command.is_guild_interaction()) +                { +                    dpp::guild guild_ptr = event.command.get_guild(); +                    // let's retrieve the member. +                    dpp::guild_member member_ptr = guild_ptr.members.find(user_ptr.id)->second; +                    dpp::guild_member bot_member_ptr = guild_ptr.members.find(cluster->me.id)->second; +                    std::unordered_map<std::string, std::string> error_messages = { +                        {"error", "You need to wait a bit before deleting messages."}, +                        {"error_amount", "The amount of messages to delete must be between 1 and 100."}, +                        {"error_perm_channel", "You do not have permission to delete messages in this channel."}}; + +                    if (action.contains("error_amount")) +                    { +                        error_messages["error_amount"] = action["error_amount"].get<std::string>(); +                    } + +                    if (action.contains("error_perm_channel")) +                    { +                        error_messages["error_perm_channel"] = action["error_perm_channel"].get<std::string>(); +                    } + +                    if (action.contains("error")) +                    { +                        error_messages["error"] = action["error"].get<std::string>(); +                    } +                    // let's retrieve the current channel +                    const dpp::channel *channel_ptr = &event.command.get_channel(); + +                    // let's check if the user has permission to delete messages +                    if (!channel_ptr->get_user_permissions(member_ptr).has(dpp::p_manage_messages) && +                        !channel_ptr->get_user_permissions(bot_member_ptr).has(dpp::p_manage_messages)) +                    { +                        co_await thinking; +                        event.edit_response(error_messages["error_perm_channel"]); +                        co_return false; +                    } +                    int amount = 0; +                    if (action.contains("depend_on")) +                    { +                        std::string depend_on = action["depend_on"]; +                        auto it = key_values.find(depend_on); +                        if (it != key_values.end()) +                        { +                            std::string depend_on_value = it->second; + +                            // let's convert the depend_on_value to an int +                            amount = std::stoi(depend_on_value); +                            if (amount < 0 || amount > 100) +                            { +                                co_await thinking; +                                event.edit_response(error_messages["error_amount"]); +                                co_return false; +                            } +                        } +                    } +                    if (amount > 0) +                    { +                        dpp::confirmation_callback_t callback = co_await cluster->co_messages_get(channel_ptr->id, 0, 0, 0, amount); +                        if (callback.is_error()) +                        { +                            printf("Error: %s\n", callback.get_error().message.c_str()); +                            co_await thinking; +                            event.edit_response(error_messages["error"]); +                            co_return false; +                        } +                        auto messages = callback.get<dpp::message_map>(); +                        if (messages.empty()) +                        { +                            printf("No messages to delete\n"); +                            co_await thinking; +                            event.edit_response("No messages to delete."); +                            co_return false; +                        } +                        std::vector<dpp::snowflake> msg_ids; + +                        for (const auto &msg : messages) +                        { +                            // let's check if the message is older than 2 weeks +                            if (msg.second.get_creation_time() < dpp::utility::time_f() - 1209600) +                            { +                                printf("Message is older than 2 weeks\n"); +                                continue; +                            } +                            else +                            { +                                msg_ids.push_back(msg.second.id); +                            } +                        } + +                        if (!msg_ids.empty()) +                        { +                            dpp::confirmation_callback_t result; +                            if(msg_ids.size() == 1){ +                                result =  co_await cluster->co_message_delete(msg_ids[0], channel_ptr->id); +                            }else{ +                                result = co_await cluster->co_message_delete_bulk(msg_ids, channel_ptr->id); +                            } +                            if (result.is_error()) +                            { +                                printf("Error: %s\n", result.get_error().message.c_str()); +                                co_await thinking; +                                event.edit_response(error_messages["error"]); +                                co_return false; +                            } + +                            co_await thinking; +                        } +                    } +                } +            } +            if (i == actions.size()) +            { + +                co_await thinking; +                co_return true; +            } +        } +    } +} diff --git a/bot/include/http_webhook_server.cpp b/bot/src/http_webhook_server.cpp index 84815b7..a6daa63 100644 --- a/bot/include/http_webhook_server.cpp +++ b/bot/src/http_webhook_server.cpp @@ -1,4 +1,4 @@ -#include "http_webhook_server.hpp" +#include "../include/http_webhook_server.hpp"  #include <fcntl.h>  #include <unistd.h>  #include <cstring> diff --git a/bot/src/main.cpp b/bot/src/main.cpp index da38d43..557463e 100644 --- a/bot/src/main.cpp +++ b/bot/src/main.cpp @@ -2,6 +2,7 @@  #include <string>  #include "../include/utils.hpp"  #include "../include/http_webhook_server.hpp" +#include "../include/handle_actions.hpp"  #include <thread>  int main(int argc, char* argv[]) { @@ -18,22 +19,34 @@ int main(int argc, char* argv[]) {      bot.on_log(dpp::utility::cout_logger()); -    bot.on_slashcommand([&json_data, &bot](const dpp::slashcommand_t& event) { +    bot.on_slashcommand([&json_data, &bot](const dpp::slashcommand_t& event) -> dpp::task<void> {          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"]; +            if (command_data.contains("actions")) { +                auto& action = command_data["actions"];                  // Actions are a list of Objects                  if (action.is_array()) { -                   no_error = app::handle_actions(event, action, key_values, bot); +                    std::cout << "Executing → Actions: " << action.dump() << std::endl; +                    auto already_returned_message = co_await handle_actions(event, action, key_values); +                    if(!already_returned_message) { +                        std::cout << "Command: " << command_name << " → Action: " << action.dump() << std::endl; +                        co_return; +                    }else { +                        // This mean we need to edit the response, not reply +                        if(command_data.contains("response")) { +                            response = command_data["response"]; +                            std::cout << "Command: " << command_name << " → Response: " << response << std::endl; +                        } +                        event.edit_response(app::update_string(response, key_values)); +                        co_return; +                    }                  }              } -            if (command_data.contains("response") && no_error) { +            if (command_data.contains("response")) {                  response = command_data["response"];                  std::cout << "Command: " << command_name << " → Response: " << response << std::endl;              } diff --git a/bot/include/utils.cpp b/bot/src/utils.cpp index 712a156..349f584 100644 --- a/bot/include/utils.cpp +++ b/bot/src/utils.cpp @@ -185,93 +185,6 @@ namespace app          }      } -    bool handle_actions(const slashcommand_t &event, const nlohmann::json &actions, const std::unordered_map<std::string, std::string> &key_values, dpp::cluster &bot) -    { -        // Actions are a list of Objects -        if (actions.is_array()) -        { -            for (const auto &action : actions) -            { -                if (action.contains("type")) -                { -                    std::string action_type = action["type"]; -                    if (action_type == "delete_messages" && event.command.is_guild_interaction()) -                    { - -                        std::string error = "Amount must be between 1 and 100"; -                        if (action.contains("error")) -                        { -                            error = action["error"].get<std::string>(); -                        } -                        // let's retrieve the current channel -                        const dpp::channel *channel_ptr = &event.command.get_channel(); -                        int amount = 0; -                        if (action.contains("amount")) -                        { -                            amount = action["amount"]; -                            // let's retrieve the amount of messages to delete -                            if (amount < 0 || amount > 100) -                            { -                                event.reply(error); -                                return false; -                            } -                        } -                        else if (action.contains("depend_on")) -                        { -                            std::string depend_on = action["depend_on"]; -                            auto it = key_values.find(depend_on); -                            if (it != key_values.end()) -                            { -                                std::string depend_on_value = it->second; - -                                // let's convert the depend_on_value to an int -                                int amount = std::stoi(depend_on_value); -                                if (amount < 0 || amount > 100) -                                { -                                    event.reply(error); -                                    return false; -                                } -                            } -                        } - -                        if (amount > 0) -                        { - -                            bot.messages_get(event.command.channel_id, 0, 0, 0, amount, [&event, &bot, &error, &channel_ptr](const dpp::confirmation_callback_t &callback) -                                             { -                                                 if (callback.is_error()) -                                                 { -                                                     event.reply(error); -                                                     return false; -                                                 } - -                                                 auto messages = callback.get<dpp::message_map>(); - -                                                 std::vector<snowflake> msg_ids; - -                                                 for (const auto &msg : messages) -                                                 { -                                                     // let's check if the message is older than 2 weeks -                                                     if (std::chrono::system_clock::from_time_t(msg.second.get_creation_time()) < std::chrono::system_clock::now() - std::chrono::hours(24 * 14)) -                                                     { -                                                         // delete the message -                                                         msg_ids.push_back(msg.second.id); -                                                     } -                                                 } - -                                                 if (!msg_ids.empty()) -                                                 { -                                                     bot.co_message_delete_bulk(msg_ids, channel_ptr->id); -                                                 } }); -                        } -                    } -                } -            } -        } - -        return true; -    } -      nlohmann::json json_from_string(const std::string &str)      {          nlohmann::json j;  | 
