From 2761098c8d81f59b4bd7da2f6003eb7def68c8b3 Mon Sep 17 00:00:00 2001 From: Brendan Golden Date: Sun, 3 Mar 2024 00:53:31 +0000 Subject: [PATCH] feat: made a function to check if a user has admin perms --- src/commands/add_server.rs | 33 +++----------------------------- src/lib.rs | 39 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 42 insertions(+), 30 deletions(-) diff --git a/src/commands/add_server.rs b/src/commands/add_server.rs index c576b16..4929d1b 100644 --- a/src/commands/add_server.rs +++ b/src/commands/add_server.rs @@ -7,40 +7,13 @@ use serenity::{ }, }; use skynet_discord_bot::get_data::get_wolves; -use skynet_discord_bot::{get_server_config, set_roles::update_server, DataBase, Servers}; +use skynet_discord_bot::{get_server_config, set_roles::update_server, DataBase, Servers, is_admin}; use sqlx::{Error, Pool, Sqlite}; pub async fn run(command: &ApplicationCommandInteraction, ctx: &Context) -> String { // check if user has high enough permisssions - let mut admin = false; - - let g_id = match command.guild_id { - None => return "Not in a server".to_string(), - Some(x) => x, - }; - - let roles_server = g_id.roles(&ctx.http).await.unwrap_or_default(); - - if let Ok(member) = g_id.member(&ctx.http, command.user.id).await { - if let Some(permissions) = member.permissions { - if permissions.administrator() { - admin = true; - } - } - - for role_id in member.roles { - if admin { - break; - } - if let Some(role) = roles_server.get(&role_id) { - if role.permissions.administrator() { - admin = true; - } - } - } - } - if !admin { - return "Administrator permission required".to_string(); + if let Some(msg) = is_admin(command, ctx).await{ + return msg; } let api_key = if let CommandDataOptionValue::String(key) = command diff --git a/src/lib.rs b/src/lib.rs index ca57dbc..45f22e7 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -17,6 +17,7 @@ use sqlx::{ Error, FromRow, Pool, Row, Sqlite, }; use std::{env, str::FromStr, sync::Arc}; +use serenity::model::prelude::application_command::ApplicationCommandInteraction; use tokio::sync::RwLock; pub struct Config { @@ -630,3 +631,41 @@ pub mod get_data { } } } + +/** + For any time ye need to check if a user who calls a command has admin privlages + */ +pub async fn is_admin(command: &ApplicationCommandInteraction, ctx: &Context) -> Option{ + let mut admin = false; + + let g_id = match command.guild_id { + None => return Some("Not in a server".to_string()), + Some(x) => x, + }; + + let roles_server = g_id.roles(&ctx.http).await.unwrap_or_default(); + + if let Ok(member) = g_id.member(&ctx.http, command.user.id).await { + if let Some(permissions) = member.permissions { + if permissions.administrator() { + admin = true; + } + } + + for role_id in member.roles { + if admin { + break; + } + if let Some(role) = roles_server.get(&role_id) { + if role.permissions.administrator() { + admin = true; + } + } + } + } + if !admin { + return Some("Administrator permission required".to_string()) + } else { + return None + } +} \ No newline at end of file