Skip to content

Commit

Permalink
Permissions: don't assume user is in guild
Browse files Browse the repository at this point in the history
When executing a command using an ID instead of a mention, the bot
would error out when checking if a user is a moderator. This is
because it checked if a user had a certain guild role.

Now, if there's an error when unwrapping the role, the function
returns false since the user doesn't exist in the guild.

Signed-off-by: kingbri <bdashore3@gmail.com>
  • Loading branch information
bdashore3 committed Oct 3, 2020
1 parent c9d74cd commit 385977e
Showing 1 changed file with 9 additions and 2 deletions.
11 changes: 9 additions & 2 deletions src/helpers/permissions_helper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@ use std::borrow::Cow;

pub async fn check_administrator(ctx: &Context, msg: &Message, user_id: Option<UserId>) -> Result<bool, Box<dyn std::error::Error + Send + Sync>> {
let channel = msg.channel(ctx).await.unwrap().guild().unwrap();
let permissions = channel.permissions_for_user(ctx, user_id.unwrap_or(msg.author.id)).await?;
let permissions = match channel.permissions_for_user(ctx, user_id.unwrap_or(msg.author.id)).await {
Ok(permissions) => permissions,
Err(_) => return Ok(false)
};

if permissions.administrator() {
Ok(true)
Expand Down Expand Up @@ -81,5 +84,9 @@ async fn check_moderator_internal(ctx: &Context, msg: &Message, user: &User) ->
return Ok(false)
}

Ok(user.has_role(ctx, msg.guild_id.unwrap(), RoleId::from(data.mod_role_id.unwrap() as u64)).await.unwrap())
let role_id = RoleId::from(data.mod_role_id.unwrap() as u64);
let has_role = user.has_role(ctx, msg.guild_id.unwrap(), role_id)
.await.unwrap_or(false);

Ok(has_role)
}

0 comments on commit 385977e

Please sign in to comment.