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
This commit is contained in:
parent
09ce45f70f
commit
7406f0e620
4 changed files with 21 additions and 73 deletions
|
@ -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::database::{get_server_config, DataBase, Servers};
|
||||||
use skynet_discord_bot::common::set_roles::normal::update_server;
|
use skynet_discord_bot::common::set_roles::normal::update_server;
|
||||||
use skynet_discord_bot::common::wolves::cns::get_wolves;
|
use skynet_discord_bot::common::wolves::cns::get_wolves;
|
||||||
use skynet_discord_bot::is_admin;
|
|
||||||
use sqlx::{Error, Pool, Sqlite};
|
use sqlx::{Error, Pool, Sqlite};
|
||||||
|
|
||||||
pub async fn run(command: &CommandInteraction, ctx: &Context) -> String {
|
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 {
|
let wolves_api = if let Some(CommandDataOption {
|
||||||
value: CommandDataOptionValue::String(key),
|
value: CommandDataOptionValue::String(key),
|
||||||
..
|
..
|
||||||
|
@ -83,6 +77,7 @@ pub async fn run(command: &CommandInteraction, ctx: &Context) -> String {
|
||||||
pub fn register() -> CreateCommand {
|
pub fn register() -> CreateCommand {
|
||||||
CreateCommand::new("add")
|
CreateCommand::new("add")
|
||||||
.description("Enable the bot for this discord")
|
.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::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::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))
|
.add_option(CreateCommandOption::new(CommandOptionType::Channel, "bot_channel", "Safe space for folks to use the bot commands.").required(true))
|
||||||
|
|
|
@ -192,19 +192,19 @@ pub(crate) mod server {
|
||||||
use super::*;
|
use super::*;
|
||||||
use skynet_discord_bot::common::minecraft::update_server;
|
use skynet_discord_bot::common::minecraft::update_server;
|
||||||
use skynet_discord_bot::common::minecraft::Minecraft;
|
use skynet_discord_bot::common::minecraft::Minecraft;
|
||||||
use skynet_discord_bot::{is_admin, Config};
|
use skynet_discord_bot::Config;
|
||||||
|
|
||||||
pub fn register() -> CreateCommand {
|
pub fn register() -> CreateCommand {
|
||||||
CreateCommand::new("minecraft_add").description("Add a minecraft server").add_option(
|
CreateCommand::new("minecraft_add")
|
||||||
CreateCommandOption::new(CommandOptionType::String, "server_id", "ID of the Minecraft server hosted by the Computer Society").required(true),
|
.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 {
|
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 {
|
let g_id = match command.guild_id {
|
||||||
None => return "Not in a server".to_string(),
|
None => return "Not in a server".to_string(),
|
||||||
Some(x) => x,
|
Some(x) => x,
|
||||||
|
@ -265,16 +265,15 @@ pub(crate) mod server {
|
||||||
use serenity::client::Context;
|
use serenity::client::Context;
|
||||||
use skynet_discord_bot::common::database::DataBase;
|
use skynet_discord_bot::common::database::DataBase;
|
||||||
use skynet_discord_bot::common::minecraft::{get_minecraft_config_server, server_information};
|
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 {
|
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 {
|
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 {
|
let g_id = match command.guild_id {
|
||||||
None => return "Not in a server".to_string(),
|
None => return "Not in a server".to_string(),
|
||||||
Some(x) => x,
|
Some(x) => x,
|
||||||
|
@ -327,20 +326,19 @@ pub(crate) mod server {
|
||||||
use serenity::model::id::GuildId;
|
use serenity::model::id::GuildId;
|
||||||
use skynet_discord_bot::common::database::DataBase;
|
use skynet_discord_bot::common::database::DataBase;
|
||||||
use skynet_discord_bot::common::minecraft::Minecraft;
|
use skynet_discord_bot::common::minecraft::Minecraft;
|
||||||
use skynet_discord_bot::is_admin;
|
|
||||||
use sqlx::{Error, Pool, Sqlite};
|
use sqlx::{Error, Pool, Sqlite};
|
||||||
|
|
||||||
pub fn register() -> CreateCommand {
|
pub fn register() -> CreateCommand {
|
||||||
CreateCommand::new("minecraft_delete").description("Delete a minecraft server").add_option(
|
CreateCommand::new("minecraft_delete")
|
||||||
CreateCommandOption::new(CommandOptionType::String, "server_id", "ID of the Minecraft server hosted by the Computer Society").required(true),
|
.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 {
|
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 {
|
let g_id = match command.guild_id {
|
||||||
None => return "Not in a server".to_string(),
|
None => return "Not in a server".to_string(),
|
||||||
Some(x) => x,
|
Some(x) => x,
|
||||||
|
|
|
@ -1,7 +1,6 @@
|
||||||
use serenity::client::Context;
|
use serenity::client::Context;
|
||||||
|
|
||||||
use skynet_discord_bot::common::database::{DataBase, RoleAdder};
|
use skynet_discord_bot::common::database::{DataBase, RoleAdder};
|
||||||
use skynet_discord_bot::is_admin;
|
|
||||||
use sqlx::{Error, Pool, Sqlite};
|
use sqlx::{Error, Pool, Sqlite};
|
||||||
|
|
||||||
pub mod edit {
|
pub mod edit {
|
||||||
|
@ -9,11 +8,6 @@ pub mod edit {
|
||||||
use serenity::all::{CommandDataOption, CommandDataOptionValue, CommandInteraction, CommandOptionType, CreateCommand, CreateCommandOption};
|
use serenity::all::{CommandDataOption, CommandDataOptionValue, CommandInteraction, CommandOptionType, CreateCommand, CreateCommandOption};
|
||||||
|
|
||||||
pub async fn run(command: &CommandInteraction, ctx: &Context) -> String {
|
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 {
|
let role_a = if let Some(CommandDataOption {
|
||||||
value: CommandDataOptionValue::Role(role),
|
value: CommandDataOptionValue::Role(role),
|
||||||
..
|
..
|
||||||
|
@ -110,6 +104,7 @@ pub mod edit {
|
||||||
pub fn register() -> CreateCommand {
|
pub fn register() -> CreateCommand {
|
||||||
CreateCommand::new("roles_adder")
|
CreateCommand::new("roles_adder")
|
||||||
.description("Combine roles together to an new one")
|
.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_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_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))
|
.add_option(CreateCommandOption::new(CommandOptionType::Role, "role_c", "Sum of A and B").required(true))
|
||||||
|
|
40
src/lib.rs
40
src/lib.rs
|
@ -3,8 +3,6 @@ pub mod common;
|
||||||
use chrono::{Datelike, SecondsFormat, Utc};
|
use chrono::{Datelike, SecondsFormat, Utc};
|
||||||
use dotenvy::dotenv;
|
use dotenvy::dotenv;
|
||||||
use rand::{distr::Alphanumeric, rng, Rng};
|
use rand::{distr::Alphanumeric, rng, Rng};
|
||||||
use serenity::all::CommandInteraction;
|
|
||||||
use serenity::client::Context;
|
|
||||||
use serenity::model::id::{ChannelId, GuildId, RoleId};
|
use serenity::model::id::{ChannelId, GuildId, RoleId};
|
||||||
use serenity::prelude::TypeMapKey;
|
use serenity::prelude::TypeMapKey;
|
||||||
use std::{env, sync::Arc};
|
use std::{env, sync::Arc};
|
||||||
|
@ -127,41 +125,3 @@ pub fn get_now_iso(short: bool) -> String {
|
||||||
pub fn random_string(len: usize) -> String {
|
pub fn random_string(len: usize) -> String {
|
||||||
rng().sample_iter(&Alphanumeric).take(len).map(char::from).collect()
|
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
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue