This commit is contained in:
poslop
2022-12-06 14:09:36 -06:00
parent 3f3882dd46
commit 5226ac34c2
2 changed files with 79 additions and 30 deletions

View File

@@ -2,12 +2,14 @@ mod mindus;
use crate::mindus::*; use crate::mindus::*;
// use std::{env}; // use std::{env};
use serenity::async_trait; use serenity::async_trait;
use serenity::model::prelude::{UserId}; use serenity::model::prelude::{UserId, RoleId};
use serenity::prelude::*; use serenity::prelude::*;
use serenity::model::channel::Message; use serenity::model::channel::Message;
use serenity::framework::standard::macros::{command, group, help, hook}; use serenity::framework::standard::macros::{command, group, help, hook};
use serenity::framework::standard::{StandardFramework, CommandResult, Args, HelpOptions, CommandGroup, help_commands}; use serenity::framework::standard::{StandardFramework, CommandResult, Args, HelpOptions, CommandGroup, help_commands};
use serenity::utils::Color;
use std::collections::HashSet; use std::collections::HashSet;
use std::str::FromStr;
#[group] #[group]
#[commands(ping, pong, console, git, discord)] #[commands(ping, pong, console, git, discord)]
@@ -51,7 +53,7 @@ async fn main() {
.configure(|c| c .configure(|c| c
.prefix(conf.prefix.clone()) .prefix(conf.prefix.clone())
.case_insensitivity(true)) .case_insensitivity(true))
.unrecognised_command(unknown_command) .unrecognised_command(unknown_command)
.help(&MY_HELP) .help(&MY_HELP)
.group(&GENERAL_GROUP); .group(&GENERAL_GROUP);
@@ -98,29 +100,22 @@ async fn pong(ctx: &Context, msg: &Message) -> CommandResult {
#[min_args(1)] #[min_args(1)]
async fn console(ctx: &Context, msg: &Message, args: Args) -> CommandResult { 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 data = ctx.data.read().await;
let sock = data.get::<TcpSock>().unwrap(); let sock = data.get::<TcpSock>().unwrap();
// let conf = data.get::<Config>().unwrap(); let conf = data.get::<Config>().unwrap();
// if !check_role(ctx, msg, conf).await.unwrap_or_else(|e| false) { if !check_role(ctx, msg, &conf.roles.cons).await.unwrap_or_else(|e| true) {
// // msg.channel_id.say(ctx, "You do not have permission to use this command").await?; msg.channel_id.send_message(ctx, |m| {
// msg.channel_id.send_message(ctx, |m| { m.content("")
// m.content("test") .embed(|e| e
// .embed(|e| e .title("No Permissions")
// .title("No Permissions") .description("You do not have permission to use this command")
// .description("You do not have permission to use this command") .color(Color::RED))
// .color(Color::RED)) }).await?;
// }).await?; return Ok(());
// return Ok(()); }
// }
msg.channel_id.send_message(ctx, |m| { msg.channel_id.send_message(ctx, |m| {
m.content("") m.content("")
@@ -133,6 +128,41 @@ async fn console(ctx: &Context, msg: &Message, args: Args) -> CommandResult {
Ok(()) 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::<TcpSock>().unwrap();
let conf = data.get::<Config>().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] #[command]
async fn git(ctx: &Context, msg: &Message) -> CommandResult { async fn git(ctx: &Context, msg: &Message) -> CommandResult {
msg.reply(ctx, "https://mintyserver.net/git/poslop/Mindustry-Server-Discord-Bot").await?; 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(()) Ok(())
} }
// async fn check_role(ctx: &Context, msg: &Message, conf: &Config) -> Result<bool, SerenityError> { async fn check_role(ctx: &Context, msg: &Message, conf: &Vec<String>) -> Result<bool, SerenityError> {
// let id = RoleId::from(u64::from_str(&conf.roles.cons))?; let mut invalid_roles = 0;
// let check = msg.author.has_role(ctx, msg.guild_id.unwrap(), id).await?; for v_id in conf {
// Ok(check) 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)
}

View File

@@ -35,8 +35,8 @@ pub struct Config {
#[derive(serde::Deserialize, serde::Serialize)] #[derive(serde::Deserialize, serde::Serialize)]
pub struct Roles { pub struct Roles {
pub auth: String, pub auth: Vec<String>,
pub cons: String pub cons: Vec<String>
} }
impl TypeMapKey for Config { impl TypeMapKey for Config {
@@ -100,8 +100,8 @@ let mut toml_file = OpenOptions::new()
port: String::from("6859"), port: String::from("6859"),
prefix: String::from(";"), prefix: String::from(";"),
roles: Roles { roles: Roles {
auth: String::from(""), auth: vec![String::from(""), String::from("")],
cons: String::from("") cons: vec![String::from("")]
} }
}; };