feat: change commands to use the "Manage Server" permission instead of just admin.
All checks were successful
On_Push / lint_clippy (push) Successful in 20s
On_Push / lint_fmt (push) Successful in 34s
On_Push / build (push) Successful in 3m32s
On_Push / deploy (push) Successful in 26s

This measn that the commands only show up if you can manage teh server
This commit is contained in:
silver 2025-02-26 15:55:42 +00:00
parent 09ce45f70f
commit 7406f0e620
Signed by: silver
GPG key ID: 36F93D61BAD3FD7D
4 changed files with 21 additions and 73 deletions

View file

@ -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))

View file

@ -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,

View file

@ -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))

View file

@ -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<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 {
Some("Administrator permission required".to_string())
} else {
None
}
}