From 55437b76eb9f14751e8919003f0f62662f68cc2a Mon Sep 17 00:00:00 2001 From: jellywx Date: Mon, 23 Nov 2020 16:04:42 +0000 Subject: [PATCH] reactions are permission gated properly so they work with different subsets of permissions --- src/commands/moderation_cmds.rs | 26 +++++++++++++++++++++++--- src/framework.rs | 24 ++++++++++++++++++------ 2 files changed, 41 insertions(+), 9 deletions(-) diff --git a/src/commands/moderation_cmds.rs b/src/commands/moderation_cmds.rs index 285dcb5..f027944 100644 --- a/src/commands/moderation_cmds.rs +++ b/src/commands/moderation_cmds.rs @@ -297,6 +297,14 @@ async fn language(ctx: &Context, msg: &Message, args: String) { .all_languages() .map(|(k, _)| ReactionType::Unicode(lm.get(k, "flag").to_string())); + let can_react = if let Some(guild) = msg.guild(&ctx).await { + guild + .user_permissions_in(msg.channel_id, ctx.cache.current_user().await) + .add_reactions() + } else { + true + }; + let reactor = msg .channel_id .send_message(&ctx, |m| { @@ -305,8 +313,13 @@ async fn language(ctx: &Context, msg: &Message, args: String) { .color(*THEME_COLOR) .description(lm.get(&user_data.language, "lang/select")) .fields(language_codes) - }) - .reactions(flags) + }); + + if can_react { + m.reactions(flags); + } + + m }) .await; @@ -339,7 +352,14 @@ async fn language(ctx: &Context, msg: &Message, args: String) { } } - let _ = sent_msg.delete_reactions(&ctx).await; + if let Some(guild) = msg.guild(&ctx).await { + let perms = + guild.user_permissions_in(msg.channel_id, ctx.cache.current_user().await); + + if perms.manage_messages() { + let _ = sent_msg.delete_reactions(&ctx).await; + } + } } } } diff --git a/src/framework.rs b/src/framework.rs index 3c722cf..4477d99 100644 --- a/src/framework.rs +++ b/src/framework.rs @@ -290,9 +290,9 @@ impl RegexFramework { } enum PermissionCheck { - None, // No permissions - Basic(bool, bool), // Send + Embed permissions (sufficient to reply) - All, // Above + Manage Webhooks (sufficient to operate) + None, // No permissions + Basic(bool, bool, bool, bool), // Send + Embed permissions (sufficient to reply) + All, // Above + Manage Webhooks (sufficient to operate) } #[async_trait] @@ -317,6 +317,8 @@ impl Framework for RegexFramework { PermissionCheck::Basic( guild_perms.manage_webhooks(), channel_perms.embed_links(), + channel_perms.add_reactions(), + channel_perms.manage_messages(), ) } else { PermissionCheck::None @@ -414,16 +416,26 @@ impl Framework for RegexFramework { } } - PermissionCheck::Basic(manage_webhooks, embed_links) => { + PermissionCheck::Basic( + manage_webhooks, + embed_links, + add_reactions, + manage_messages, + ) => { let response = lm .get(&language, "no_perms_general") .replace( "{manage_webhooks}", if manage_webhooks { "✅" } else { "❌" }, ) + .replace("{embed_links}", if embed_links { "✅" } else { "❌" }) .replace( - "{embed_links}", - if embed_links { "✅" } else { "❌" }, + "{add_reactions}", + if add_reactions { "✅" } else { "❌" }, + ) + .replace( + "{manage_messages}", + if manage_messages { "✅" } else { "❌" }, ); let _ = msg.channel_id.say(&ctx, response).await;