From 385977eef75418040899a69cec9cc27268bd48ff Mon Sep 17 00:00:00 2001 From: kingbri Date: Sat, 3 Oct 2020 08:52:06 -0400 Subject: [PATCH] Permissions: don't assume user is in guild 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 --- src/helpers/permissions_helper.rs | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/src/helpers/permissions_helper.rs b/src/helpers/permissions_helper.rs index c8e01f4..99fffe9 100644 --- a/src/helpers/permissions_helper.rs +++ b/src/helpers/permissions_helper.rs @@ -7,7 +7,10 @@ use std::borrow::Cow; pub async fn check_administrator(ctx: &Context, msg: &Message, user_id: Option) -> Result> { 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) @@ -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) }