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()
.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;
}
}
}
}
}

View File

@ -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;