check user permissions and erase deleted webhooks

This commit is contained in:
Syping 2024-02-19 20:52:02 +01:00
parent a6feb1273c
commit d5c8574063
4 changed files with 33 additions and 4 deletions

View file

@ -254,6 +254,14 @@ void settings::add_translatebot_webhook(dpp::snowflake webhook_id)
m_webhookIds.push_back(webhook_id); m_webhookIds.push_back(webhook_id);
} }
void settings::erase_translatebot_webhook(dpp::snowflake webhook_id)
{
const std::lock_guard<std::recursive_mutex> 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() uint16_t settings::avatar_size()
{ {
const std::lock_guard<std::recursive_mutex> guard(m_mutex); const std::lock_guard<std::recursive_mutex> guard(m_mutex);

View file

@ -32,6 +32,9 @@ namespace bot {
bool add_target(const target &target, dpp::snowflake guild_id, dpp::snowflake channel_id); bool add_target(const target &target, dpp::snowflake guild_id, dpp::snowflake channel_id);
void add_translatebot_webhook(dpp::snowflake webhook_id); void add_translatebot_webhook(dpp::snowflake webhook_id);
/* erase functions */
void erase_translatebot_webhook(dpp::snowflake webhook_id);
/* get functions */ /* get functions */
uint16_t avatar_size(); uint16_t avatar_size();
static channel* get_channel(guild *guild, dpp::snowflake channel_id); static channel* get_channel(guild *guild, dpp::snowflake channel_id);

View file

@ -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) void bot::slashcommands::process_edit_command(dpp::cluster *bot, bot::settings::settings *settings, const dpp::slashcommand_t &event)
{ {
try { 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(); dpp::command_interaction interaction = event.command.get_command_interaction();
if (interaction.options[0].name == "delete") { if (interaction.options[0].name == "delete") {
const std::lock_guard<bot::settings::settings> guard(*settings); const std::lock_guard<bot::settings::settings> 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();) { for (auto target = channel->targets.begin(); target != channel->targets.end();) {
if (std::find(targets.begin(), targets.end(), target->target) != 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); target = channel->targets.erase(target);
} }
else { else {
@ -77,7 +81,7 @@ void bot::slashcommands::process_edit_command(dpp::cluster *bot, bot::settings::
if (target_found) { if (target_found) {
for (auto _target = channel->targets.begin(); _target != channel->targets.end(); _target++) { for (auto _target = channel->targets.begin(); _target != channel->targets.end(); _target++) {
if (_target->target == 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); channel->targets.erase(_target);
break; break;
} }
@ -143,6 +147,15 @@ void bot::slashcommands::process_edit_command(dpp::cluster *bot, bot::settings::
} }
} }
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) void bot::slashcommands::process_list_command(dpp::cluster *bot, bot::settings::settings *settings, const dpp::slashcommand_t &event)
{ {
try { 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) void bot::slashcommands::process_translate_command(dpp::cluster *bot, bot::settings::settings *settings, const dpp::slashcommand_t &event)
{ {
try { 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<dpp::channel,dpp::webhook> v_target; std::variant<dpp::channel,dpp::webhook> v_target;
const std::string source = std::get<std::string>(event.get_parameter("source")); const std::string source = std::get<std::string>(event.get_parameter("source"));
const std::string target = std::get<std::string>(event.get_parameter("target")); const std::string target = std::get<std::string>(event.get_parameter("target"));

View file

@ -26,6 +26,7 @@ namespace bot {
namespace slashcommands { namespace slashcommands {
extern void process_command_event(dpp::cluster *bot, bot::settings::settings *settings, const dpp::slashcommand_t &event); 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_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_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); 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);