reactions are permission gated properly so they work with different subsets of permissions

This commit is contained in:
jellywx 2020-11-23 16:04:42 +00:00
parent 3c1fe1f091
commit 55437b76eb
2 changed files with 41 additions and 9 deletions

View File

@ -297,6 +297,14 @@ async fn language(ctx: &Context, msg: &Message, args: String) {
.all_languages() .all_languages()
.map(|(k, _)| ReactionType::Unicode(lm.get(k, "flag").to_string())); .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 let reactor = msg
.channel_id .channel_id
.send_message(&ctx, |m| { .send_message(&ctx, |m| {
@ -305,8 +313,13 @@ async fn language(ctx: &Context, msg: &Message, args: String) {
.color(*THEME_COLOR) .color(*THEME_COLOR)
.description(lm.get(&user_data.language, "lang/select")) .description(lm.get(&user_data.language, "lang/select"))
.fields(language_codes) .fields(language_codes)
}) });
.reactions(flags)
if can_react {
m.reactions(flags);
}
m
}) })
.await; .await;
@ -339,10 +352,17 @@ async fn language(ctx: &Context, msg: &Message, args: String) {
} }
} }
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; let _ = sent_msg.delete_reactions(&ctx).await;
} }
} }
} }
}
}
#[command] #[command]
#[supports_dm(false)] #[supports_dm(false)]

View File

@ -291,7 +291,7 @@ impl RegexFramework {
enum PermissionCheck { enum PermissionCheck {
None, // No permissions None, // No permissions
Basic(bool, bool), // Send + Embed permissions (sufficient to reply) Basic(bool, bool, bool, bool), // Send + Embed permissions (sufficient to reply)
All, // Above + Manage Webhooks (sufficient to operate) All, // Above + Manage Webhooks (sufficient to operate)
} }
@ -317,6 +317,8 @@ impl Framework for RegexFramework {
PermissionCheck::Basic( PermissionCheck::Basic(
guild_perms.manage_webhooks(), guild_perms.manage_webhooks(),
channel_perms.embed_links(), channel_perms.embed_links(),
channel_perms.add_reactions(),
channel_perms.manage_messages(),
) )
} else { } else {
PermissionCheck::None 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 let response = lm
.get(&language, "no_perms_general") .get(&language, "no_perms_general")
.replace( .replace(
"{manage_webhooks}", "{manage_webhooks}",
if manage_webhooks { "" } else { "" }, if manage_webhooks { "" } else { "" },
) )
.replace("{embed_links}", if embed_links { "" } else { "" })
.replace( .replace(
"{embed_links}", "{add_reactions}",
if embed_links { "" } else { "" }, if add_reactions { "" } else { "" },
)
.replace(
"{manage_messages}",
if manage_messages { "" } else { "" },
); );
let _ = msg.channel_id.say(&ctx, response).await; let _ = msg.channel_id.say(&ctx, response).await;