diff --git a/src/settings.cpp b/src/settings.cpp index 067b137..f8cc34a 100644 --- a/src/settings.cpp +++ b/src/settings.cpp @@ -254,6 +254,14 @@ void settings::add_translatebot_webhook(dpp::snowflake webhook_id) m_webhookIds.push_back(webhook_id); } +void settings::erase_translatebot_webhook(dpp::snowflake webhook_id) +{ + const std::lock_guard guard(m_mutex); + auto webhook_it = std::find(m_webhookIds.begin(), m_webhookIds.end(), webhook_id); + if (webhook_it != m_webhookIds.end()) + m_webhookIds.erase(webhook_it); +} + uint16_t settings::avatar_size() { const std::lock_guard guard(m_mutex); diff --git a/src/settings.h b/src/settings.h index ed3b8b9..9bbc899 100644 --- a/src/settings.h +++ b/src/settings.h @@ -32,6 +32,9 @@ namespace bot { bool add_target(const target &target, dpp::snowflake guild_id, dpp::snowflake channel_id); void add_translatebot_webhook(dpp::snowflake webhook_id); + /* erase functions */ + void erase_translatebot_webhook(dpp::snowflake webhook_id); + /* get functions */ uint16_t avatar_size(); static channel* get_channel(guild *guild, dpp::snowflake channel_id); diff --git a/src/slashcommands.cpp b/src/slashcommands.cpp index d3a12f8..e16a51c 100644 --- a/src/slashcommands.cpp +++ b/src/slashcommands.cpp @@ -33,6 +33,10 @@ void bot::slashcommands::process_command_event(dpp::cluster *bot, bot::settings: void bot::slashcommands::process_edit_command(dpp::cluster *bot, bot::settings::settings *settings, const dpp::slashcommand_t &event) { try { + dpp::permission user_permissions = event.command.get_resolved_permission(event.command.usr.id); + if (user_permissions.has(dpp::p_manage_webhooks)) + throw dpp::exception("Unauthorized to use command"); + dpp::command_interaction interaction = event.command.get_command_interaction(); if (interaction.options[0].name == "delete") { const std::lock_guard guard(*settings); @@ -52,7 +56,7 @@ void bot::slashcommands::process_edit_command(dpp::cluster *bot, bot::settings:: } for (auto target = channel->targets.begin(); target != channel->targets.end();) { if (std::find(targets.begin(), targets.end(), target->target) != targets.end()) { - bot->delete_webhook(target->webhook.id); + bot->delete_webhook(target->webhook.id, std::bind(&bot::slashcommands::process_deleted_webhook, settings, target->webhook.id, std::placeholders::_1)); target = channel->targets.erase(target); } else { @@ -77,7 +81,7 @@ void bot::slashcommands::process_edit_command(dpp::cluster *bot, bot::settings:: if (target_found) { for (auto _target = channel->targets.begin(); _target != channel->targets.end(); _target++) { if (_target->target == target) { - bot->delete_webhook(_target->webhook.id); + bot->delete_webhook(_target->webhook.id, std::bind(&bot::slashcommands::process_deleted_webhook, settings, _target->webhook.id, std::placeholders::_1)); channel->targets.erase(_target); break; } @@ -137,12 +141,21 @@ void bot::slashcommands::process_edit_command(dpp::cluster *bot, bot::settings:: throw std::invalid_argument("Option " + interaction.options[0].name + " is not known"); } } - catch (const std::exception& exception) { + catch (const std::exception &exception) { std::cerr << "[Exception] " << exception.what() << std::endl; event.reply(dpp::message("Exception while processing command:\n"s + exception.what()).set_flags(dpp::m_ephemeral)); } } +void bot::slashcommands::process_deleted_webhook(bot::settings::settings *settings, dpp::snowflake webhook_id, const dpp::confirmation_callback_t &callback) +{ + if (callback.is_error()) { + std::cerr << "[Error] Failed to delete Webhook " << webhook_id << std::endl; + return; + } + settings->erase_translatebot_webhook(webhook_id); +} + void bot::slashcommands::process_list_command(dpp::cluster *bot, bot::settings::settings *settings, const dpp::slashcommand_t &event) { try { @@ -217,6 +230,10 @@ void bot::slashcommands::process_list_command(dpp::cluster *bot, bot::settings:: void bot::slashcommands::process_translate_command(dpp::cluster *bot, bot::settings::settings *settings, const dpp::slashcommand_t &event) { try { + dpp::permission user_permissions = event.command.get_resolved_permission(event.command.usr.id); + if (user_permissions.has(dpp::p_manage_webhooks)) + throw dpp::exception("Unauthorized to use command"); + std::variant v_target; const std::string source = std::get(event.get_parameter("source")); const std::string target = std::get(event.get_parameter("target")); diff --git a/src/slashcommands.h b/src/slashcommands.h index d981675..bc1629c 100644 --- a/src/slashcommands.h +++ b/src/slashcommands.h @@ -25,7 +25,8 @@ namespace bot { namespace slashcommands { extern void process_command_event(dpp::cluster *bot, bot::settings::settings *settings, const dpp::slashcommand_t &event); - extern void process_edit_command(dpp::cluster* bot, bot::settings::settings* settings, const dpp::slashcommand_t &event); + extern void process_edit_command(dpp::cluster *bot, bot::settings::settings *settings, const dpp::slashcommand_t &event); + extern void process_deleted_webhook(bot::settings::settings *settings, dpp::snowflake webhook_id, const dpp::confirmation_callback_t &callback); extern void process_list_command(dpp::cluster *bot, bot::settings::settings *settings, const dpp::slashcommand_t &event); extern void process_translate_command(dpp::cluster *bot, bot::settings::settings *settings, const dpp::slashcommand_t &event); extern void process_translate_webhook_add_target(dpp::cluster *bot, bot::settings::settings *settings, const dpp::slashcommand_t &event, const std::string &target, const dpp::confirmation_callback_t &callback);