From 7406f0e6206b3fcec4c471949d6c428503fa9e91 Mon Sep 17 00:00:00 2001 From: Brendan Golden Date: Wed, 26 Feb 2025 15:55:42 +0000 Subject: [PATCH] feat: change commands to use the "Manage Server" permission instead of just admin. This measn that the commands only show up if you can manage teh server --- src/commands/add_server.rs | 7 +------ src/commands/minecraft.rs | 40 ++++++++++++++++++-------------------- src/commands/role_adder.rs | 7 +------ src/lib.rs | 40 -------------------------------------- 4 files changed, 21 insertions(+), 73 deletions(-) diff --git a/src/commands/add_server.rs b/src/commands/add_server.rs index 5a9dbee..70ec3c8 100644 --- a/src/commands/add_server.rs +++ b/src/commands/add_server.rs @@ -3,15 +3,9 @@ use serenity::{builder::CreateCommand, client::Context}; use skynet_discord_bot::common::database::{get_server_config, DataBase, Servers}; use skynet_discord_bot::common::set_roles::normal::update_server; use skynet_discord_bot::common::wolves::cns::get_wolves; -use skynet_discord_bot::is_admin; use sqlx::{Error, Pool, Sqlite}; pub async fn run(command: &CommandInteraction, ctx: &Context) -> String { - // check if user has high enough permisssions - if let Some(msg) = is_admin(command, ctx).await { - return msg; - } - let wolves_api = if let Some(CommandDataOption { value: CommandDataOptionValue::String(key), .. @@ -83,6 +77,7 @@ pub async fn run(command: &CommandInteraction, ctx: &Context) -> String { pub fn register() -> CreateCommand { CreateCommand::new("add") .description("Enable the bot for this discord") + .default_member_permissions(serenity::model::Permissions::MANAGE_GUILD) .add_option(CreateCommandOption::new(CommandOptionType::String, "api_key", "UL Wolves API Key").required(true)) .add_option(CreateCommandOption::new(CommandOptionType::Role, "role_current", "Role for Current members").required(true)) .add_option(CreateCommandOption::new(CommandOptionType::Channel, "bot_channel", "Safe space for folks to use the bot commands.").required(true)) diff --git a/src/commands/minecraft.rs b/src/commands/minecraft.rs index 54bf35c..5f1321a 100644 --- a/src/commands/minecraft.rs +++ b/src/commands/minecraft.rs @@ -192,19 +192,19 @@ pub(crate) mod server { use super::*; use skynet_discord_bot::common::minecraft::update_server; use skynet_discord_bot::common::minecraft::Minecraft; - use skynet_discord_bot::{is_admin, Config}; + use skynet_discord_bot::Config; pub fn register() -> CreateCommand { - CreateCommand::new("minecraft_add").description("Add a minecraft server").add_option( - CreateCommandOption::new(CommandOptionType::String, "server_id", "ID of the Minecraft server hosted by the Computer Society").required(true), - ) + CreateCommand::new("minecraft_add") + .description("Add a minecraft server") + .default_member_permissions(serenity::model::Permissions::MANAGE_GUILD) + .add_option( + CreateCommandOption::new(CommandOptionType::String, "server_id", "ID of the Minecraft server hosted by the Computer Society") + .required(true), + ) } pub async fn run(command: &CommandInteraction, ctx: &Context) -> String { - // check if user has high enough permisssions - 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, @@ -265,16 +265,15 @@ pub(crate) mod server { use serenity::client::Context; use skynet_discord_bot::common::database::DataBase; use skynet_discord_bot::common::minecraft::{get_minecraft_config_server, server_information}; - use skynet_discord_bot::{is_admin, Config}; + use skynet_discord_bot::Config; pub fn register() -> CreateCommand { - CreateCommand::new("minecraft_list").description("List your minecraft servers") + CreateCommand::new("minecraft_list") + .default_member_permissions(serenity::model::Permissions::MANAGE_GUILD) + .description("List your minecraft servers") } pub async fn run(command: &CommandInteraction, ctx: &Context) -> String { - 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, @@ -327,20 +326,19 @@ pub(crate) mod server { use serenity::model::id::GuildId; use skynet_discord_bot::common::database::DataBase; use skynet_discord_bot::common::minecraft::Minecraft; - use skynet_discord_bot::is_admin; use sqlx::{Error, Pool, Sqlite}; pub fn register() -> CreateCommand { - CreateCommand::new("minecraft_delete").description("Delete a minecraft server").add_option( - CreateCommandOption::new(CommandOptionType::String, "server_id", "ID of the Minecraft server hosted by the Computer Society").required(true), - ) + CreateCommand::new("minecraft_delete") + .description("Delete a minecraft server") + .default_member_permissions(serenity::model::Permissions::MANAGE_GUILD) + .add_option( + CreateCommandOption::new(CommandOptionType::String, "server_id", "ID of the Minecraft server hosted by the Computer Society") + .required(true), + ) } pub async fn run(command: &CommandInteraction, ctx: &Context) -> String { - // check if user has high enough permisssions - 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, diff --git a/src/commands/role_adder.rs b/src/commands/role_adder.rs index 182eeb9..b0d28e2 100644 --- a/src/commands/role_adder.rs +++ b/src/commands/role_adder.rs @@ -1,7 +1,6 @@ use serenity::client::Context; use skynet_discord_bot::common::database::{DataBase, RoleAdder}; -use skynet_discord_bot::is_admin; use sqlx::{Error, Pool, Sqlite}; pub mod edit { @@ -9,11 +8,6 @@ pub mod edit { use serenity::all::{CommandDataOption, CommandDataOptionValue, CommandInteraction, CommandOptionType, CreateCommand, CreateCommandOption}; pub async fn run(command: &CommandInteraction, ctx: &Context) -> String { - // check if user has high enough permisssions - if let Some(msg) = is_admin(command, ctx).await { - return msg; - } - let role_a = if let Some(CommandDataOption { value: CommandDataOptionValue::Role(role), .. @@ -110,6 +104,7 @@ pub mod edit { pub fn register() -> CreateCommand { CreateCommand::new("roles_adder") .description("Combine roles together to an new one") + .default_member_permissions(serenity::model::Permissions::MANAGE_GUILD) .add_option(CreateCommandOption::new(CommandOptionType::Role, "role_a", "A role you want to add to Role B").required(true)) .add_option(CreateCommandOption::new(CommandOptionType::Role, "role_b", "A role you want to add to Role A").required(true)) .add_option(CreateCommandOption::new(CommandOptionType::Role, "role_c", "Sum of A and B").required(true)) diff --git a/src/lib.rs b/src/lib.rs index 0152205..762dc69 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -3,8 +3,6 @@ pub mod common; use chrono::{Datelike, SecondsFormat, Utc}; use dotenvy::dotenv; use rand::{distr::Alphanumeric, rng, Rng}; -use serenity::all::CommandInteraction; -use serenity::client::Context; use serenity::model::id::{ChannelId, GuildId, RoleId}; use serenity::prelude::TypeMapKey; use std::{env, sync::Arc}; @@ -127,41 +125,3 @@ pub fn get_now_iso(short: bool) -> String { pub fn random_string(len: usize) -> String { rng().sample_iter(&Alphanumeric).take(len).map(char::from).collect() } - -/** - For any time ye need to check if a user who calls a command has admin privlages -*/ -pub async fn is_admin(command: &CommandInteraction, 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 { - Some("Administrator permission required".to_string()) - } else { - None - } -}