From 5226ac34c22406abb64e944cd45a520985d75c81 Mon Sep 17 00:00:00 2001 From: poslop Date: Tue, 6 Dec 2022 14:09:36 -0600 Subject: [PATCH] src --- src/main.rs | 101 +++++++++++++++++++++++++++++++++++++------------- src/mindus.rs | 8 ++-- 2 files changed, 79 insertions(+), 30 deletions(-) diff --git a/src/main.rs b/src/main.rs index cd8f1f3..14e1691 100644 --- a/src/main.rs +++ b/src/main.rs @@ -2,12 +2,14 @@ mod mindus; use crate::mindus::*; // use std::{env}; use serenity::async_trait; -use serenity::model::prelude::{UserId}; +use serenity::model::prelude::{UserId, RoleId}; use serenity::prelude::*; use serenity::model::channel::Message; use serenity::framework::standard::macros::{command, group, help, hook}; use serenity::framework::standard::{StandardFramework, CommandResult, Args, HelpOptions, CommandGroup, help_commands}; +use serenity::utils::Color; use std::collections::HashSet; +use std::str::FromStr; #[group] #[commands(ping, pong, console, git, discord)] @@ -51,7 +53,7 @@ async fn main() { .configure(|c| c .prefix(conf.prefix.clone()) .case_insensitivity(true)) - .unrecognised_command(unknown_command) + .unrecognised_command(unknown_command) .help(&MY_HELP) .group(&GENERAL_GROUP); @@ -98,29 +100,22 @@ async fn pong(ctx: &Context, msg: &Message) -> CommandResult { #[min_args(1)] async fn console(ctx: &Context, msg: &Message, args: Args) -> CommandResult { - // let input = msg.content.strip_prefix(";console ").to_owned(); - - // if input == Option::None { - // msg.reply(ctx, "Not enough Parameters").await?; - // return Ok(()); - // } - let data = ctx.data.read().await; let sock = data.get::().unwrap(); - // let conf = data.get::().unwrap(); + let conf = data.get::().unwrap(); + - // if !check_role(ctx, msg, conf).await.unwrap_or_else(|e| false) { - // // msg.channel_id.say(ctx, "You do not have permission to use this command").await?; - // msg.channel_id.send_message(ctx, |m| { - // m.content("test") - // .embed(|e| e - // .title("No Permissions") - // .description("You do not have permission to use this command") - // .color(Color::RED)) - // }).await?; - // return Ok(()); - // } + if !check_role(ctx, msg, &conf.roles.cons).await.unwrap_or_else(|e| true) { + msg.channel_id.send_message(ctx, |m| { + m.content("") + .embed(|e| e + .title("No Permissions") + .description("You do not have permission to use this command") + .color(Color::RED)) + }).await?; + return Ok(()); + } msg.channel_id.send_message(ctx, |m| { m.content("") @@ -133,6 +128,41 @@ async fn console(ctx: &Context, msg: &Message, args: Args) -> CommandResult { Ok(()) } +#[command] +#[num_args(1)] +#[aliases("a")] +#[description("Give youself permissions to build in the mindustry server")] +#[example("a ErkDog")] +async fn auth(ctx: &Context, msg: &Message, args: Args) -> CommandResult { + + let data = ctx.data.read().await; + + let sock = data.get::().unwrap(); + let conf = data.get::().unwrap(); + + + if !check_role(ctx, msg, &conf.roles.auth).await.unwrap_or_else(|e| true) { + msg.channel_id.send_message(ctx, |m| { + m.content("") + .embed(|e| e + .title("No Permissions") + .description("You do not have permission to use this command") + .color(Color::RED)) + }).await?; + return Ok(()); + } + + msg.channel_id.send_message(ctx, |m| { + m.content("") + .embed(|e| e + .title("Console") + .description(cons_rw(sock, &format!("auth add {}", args.message()))) + .color((255, 219, 221)) + ) + }).await?; + Ok(()) +} + #[command] async fn git(ctx: &Context, msg: &Message) -> CommandResult { msg.reply(ctx, "https://mintyserver.net/git/poslop/Mindustry-Server-Discord-Bot").await?; @@ -145,8 +175,27 @@ async fn discord(ctx: &Context, msg: &Message) -> CommandResult { Ok(()) } -// async fn check_role(ctx: &Context, msg: &Message, conf: &Config) -> Result { -// let id = RoleId::from(u64::from_str(&conf.roles.cons))?; -// let check = msg.author.has_role(ctx, msg.guild_id.unwrap(), id).await?; -// Ok(check) -// } \ No newline at end of file +async fn check_role(ctx: &Context, msg: &Message, conf: &Vec) -> Result { + let mut invalid_roles = 0; + for v_id in conf { + let u_id = match u64::from_str(&v_id) { + Ok(n) => n, + Err(e) => + { + invalid_roles += 1; + continue + }, + }; + let id = RoleId::from(u_id); + let check = msg.author.has_role(ctx, msg.guild_id.unwrap(), id).await?; + if check { + return Ok(check) + } + } + + if invalid_roles == conf.len() { + return Ok(true) + } + + Ok(false) +} \ No newline at end of file diff --git a/src/mindus.rs b/src/mindus.rs index 3d0e986..9124377 100644 --- a/src/mindus.rs +++ b/src/mindus.rs @@ -35,8 +35,8 @@ pub struct Config { #[derive(serde::Deserialize, serde::Serialize)] pub struct Roles { - pub auth: String, - pub cons: String + pub auth: Vec, + pub cons: Vec } impl TypeMapKey for Config { @@ -100,8 +100,8 @@ let mut toml_file = OpenOptions::new() port: String::from("6859"), prefix: String::from(";"), roles: Roles { - auth: String::from(""), - cons: String::from("") + auth: vec![String::from(""), String::from("")], + cons: vec![String::from("")] } };