From 76b447e1d67389618a027c692f76f937162c5646 Mon Sep 17 00:00:00 2001 From: soler_j Date: Wed, 30 Apr 2025 01:24:34 +0200 Subject: Ajout de l'handle des "actions" - Première action disponible "suppression de message" MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- bot/include/utils.cpp | 112 +++++++++++++++++++++++++++++++++++++++++++++++++- bot/include/utils.hpp | 9 ++++ bot/src/main.cpp | 14 +++++-- 3 files changed, 130 insertions(+), 5 deletions(-) diff --git a/bot/include/utils.cpp b/bot/include/utils.cpp index 553e6d0..5bd7692 100644 --- a/bot/include/utils.cpp +++ b/bot/include/utils.cpp @@ -150,7 +150,7 @@ namespace app snowflake mentionable_id = std::get(option.value); auto member_ptr = event.command.get_resolved_member(mentionable_id); const user &u = *member_ptr.get_user(); - kv["opts." +option.name] = u.username; + kv["opts." + option.name] = u.username; kv["opts." + option.name + ".id"] = u.id.str(); kv["opts." + option.name + ".avatar"] = make_avatar_url(u); kv["opts." + option.name + ".discriminator"] = std::to_string(u.discriminator); @@ -185,6 +185,116 @@ namespace app } } + bool handle_actions(const slashcommand_t &event, const nlohmann::json &actions, const std::unordered_map &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(); + } + // let's retrieve the current channel + const dpp::channel *channel_ptr = &event.command.get_channel(); + if (action.contains("amount")) + { + int amount = action["amount"]; + // let's retrieve the amount of messages to delete + if (amount < 0 || amount > 100) + { + event.reply(error); + return false; + } + + 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(); + + std::vector 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); + } }); + } + 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; + } + // Handle delete messages action based on depend_on + 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(); + + std::vector 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; diff --git a/bot/include/utils.hpp b/bot/include/utils.hpp index 57f4005..3052ecf 100644 --- a/bot/include/utils.hpp +++ b/bot/include/utils.hpp @@ -56,6 +56,15 @@ namespace app */ std::unordered_map generate_key_values(const slashcommand_t &event); + /** + * @brief Handles actions specified in the slash command event + * + * @param event The slash command event + * @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 &key_values, dpp::cluster &bot); + /** * @brief Parses a JSON string into a JSON object * diff --git a/bot/src/main.cpp b/bot/src/main.cpp index 4418b6f..da38d43 100644 --- a/bot/src/main.cpp +++ b/bot/src/main.cpp @@ -18,18 +18,24 @@ int main(int argc, char* argv[]) { bot.on_log(dpp::utility::cout_logger()); - bot.on_slashcommand([&json_data](const dpp::slashcommand_t& event) { + bot.on_slashcommand([&json_data, &bot](const dpp::slashcommand_t& event) { std::unordered_map 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]; - if (command_data.contains("response")) { + 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; - } else { - std::cout << "No response found for command: " << command_name << std::endl; } } -- cgit v1.2.3