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 +++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 111 insertions(+), 1 deletion(-) (limited to 'bot/include/utils.cpp') 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; -- cgit v1.2.3