feat: made a function to check if a user has admin perms
This commit is contained in:
parent
a9f55da04d
commit
2761098c8d
2 changed files with 42 additions and 30 deletions
|
@ -7,40 +7,13 @@ use serenity::{
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
use skynet_discord_bot::get_data::get_wolves;
|
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};
|
use sqlx::{Error, Pool, Sqlite};
|
||||||
|
|
||||||
pub async fn run(command: &ApplicationCommandInteraction, ctx: &Context) -> String {
|
pub async fn run(command: &ApplicationCommandInteraction, ctx: &Context) -> String {
|
||||||
// check if user has high enough permisssions
|
// check if user has high enough permisssions
|
||||||
let mut admin = false;
|
if let Some(msg) = is_admin(command, ctx).await{
|
||||||
|
return msg;
|
||||||
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();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
let api_key = if let CommandDataOptionValue::String(key) = command
|
let api_key = if let CommandDataOptionValue::String(key) = command
|
||||||
|
|
39
src/lib.rs
39
src/lib.rs
|
@ -17,6 +17,7 @@ use sqlx::{
|
||||||
Error, FromRow, Pool, Row, Sqlite,
|
Error, FromRow, Pool, Row, Sqlite,
|
||||||
};
|
};
|
||||||
use std::{env, str::FromStr, sync::Arc};
|
use std::{env, str::FromStr, sync::Arc};
|
||||||
|
use serenity::model::prelude::application_command::ApplicationCommandInteraction;
|
||||||
use tokio::sync::RwLock;
|
use tokio::sync::RwLock;
|
||||||
|
|
||||||
pub struct Config {
|
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<String>{
|
||||||
|
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
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in a new issue