From d30662558f9e8f215f69abe215f7894e14250939 Mon Sep 17 00:00:00 2001 From: poslop Date: Mon, 5 Dec 2022 15:03:49 -0600 Subject: [PATCH] config name changes --- build_musl | 2 -- config.toml | 8 ----- src/main.rs | 88 +++++++++++++++++++++++++++++++++++++++++++++------ src/mindus.rs | 38 ++++++++++++---------- 4 files changed, 100 insertions(+), 36 deletions(-) delete mode 100755 build_musl delete mode 100644 config.toml diff --git a/build_musl b/build_musl deleted file mode 100755 index 9533ac1..0000000 --- a/build_musl +++ /dev/null @@ -1,2 +0,0 @@ -alias rust-musl-builder='sudo docker run --rm -it -v "$(pwd)":/home/rust/src ekidd/rust-musl-builder' -rust-musl-builder cargo build --release diff --git a/config.toml b/config.toml deleted file mode 100644 index 802edc0..0000000 --- a/config.toml +++ /dev/null @@ -1,8 +0,0 @@ -discordToken = "" -ip = "localhost" -port = "6859" -trigger = ";" - -[roles] -auth = "" -cons = "" diff --git a/src/main.rs b/src/main.rs index c91bad3..22711bf 100644 --- a/src/main.rs +++ b/src/main.rs @@ -2,33 +2,67 @@ mod mindus; use crate::mindus::*; // use std::{env}; use serenity::async_trait; +use serenity::model::prelude::{RoleId, UserId}; use serenity::prelude::*; use serenity::model::channel::Message; -use serenity::framework::standard::macros::{command, group}; -use serenity::framework::standard::{StandardFramework, CommandResult}; - - +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)] +#[commands(ping, pong, console, git, discord)] struct General; struct Handler; #[async_trait] impl EventHandler for Handler {} +#[help] +async fn my_help( + context: &Context, + msg: &Message, + args: Args, + help_options: &'static HelpOptions, + groups: &[&'static CommandGroup], + owners: HashSet, +) -> CommandResult { + let _ = help_commands::with_embeds(context, msg, args, help_options, groups, owners).await; + Ok(()) +} + +#[hook] +async fn after(_ctx: &Context, _msg: &Message, command_name: &str, command_result: CommandResult) { + match command_result { + Ok(()) => println!("Processed command '{}'", command_name), + Err(why) => println!("Command '{}' returned error {:?}", command_name, why), + } +} + +#[hook] +async fn unknown_command(_ctx: &Context, _msg: &Message, unknown_command_name: &str) { + println!("Could not find command named '{}'", unknown_command_name); +} + +#[hook] +async fn normal_message(_ctx: &Context, msg: &Message) { + println!("Message is not a command '{}'", msg.content); +} + #[tokio::main] async fn main() { let conf = init_conf(); - let sock = TcpSock::new(conf.ip, conf.port).unwrap(); + let sock = TcpSock::new(conf.ip.clone(), conf.port.clone()).unwrap(); let framework = StandardFramework::new() .configure(|c| c - .prefix(conf.trigger) + .prefix(conf.prefix.clone()) .case_insensitivity(true)) - .group(&GENERAL_GROUP); + .help(&MY_HELP) + .group(&GENERAL_GROUP); let intents = GatewayIntents::non_privileged() | GatewayIntents::MESSAGE_CONTENT; let mut client = Client::builder(&conf.discord_token, intents) @@ -40,14 +74,17 @@ async fn main() { { let mut data = client.data.write().await; data.insert::(sock); - } + data.insert::(conf) + } if let Err(why) = client.start().await { println!("An error occurred while running the client: {:?}", why); } } + #[command] +#[help_available(false)] async fn ping(ctx: &Context, msg: &Message) -> CommandResult { msg.reply(ctx, "Pong!").await?; @@ -56,6 +93,7 @@ async fn ping(ctx: &Context, msg: &Message) -> CommandResult { #[command] +#[help_available(false)] async fn pong(ctx: &Context, msg: &Message) -> CommandResult { msg.reply(ctx, "Ping!").await?; @@ -63,8 +101,10 @@ async fn pong(ctx: &Context, msg: &Message) -> CommandResult { } #[command] +#[aliases("c", "cons")] +#[description("Send a command to the mindustry server console")] +#[example("c status")] async fn console(ctx: &Context, msg: &Message) -> CommandResult { - let input = msg.content.strip_prefix(";console ").to_owned(); if input == Option::None { @@ -75,8 +115,36 @@ async fn console(ctx: &Context, msg: &Message) -> CommandResult { let data = ctx.data.read().await; let sock = data.get::().unwrap(); + let conf = data.get::().unwrap(); + if !check_role(ctx, msg, conf).await { + // 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(()); + } + msg.reply(ctx, format!("```\n{}\n```", cons_rw(sock, &input.unwrap()))).await?; Ok(()) } +#[command] +async fn git(ctx: &Context, msg: &Message) -> CommandResult { + msg.reply(ctx, "https://mintyserver.net/git/poslop/Mindustry-Server-Discord-Bot").await?; + Ok(()) +} + +#[command] +async fn discord(ctx: &Context, msg: &Message) -> CommandResult { + msg.reply(ctx, "https://discord.gg/sRKCKQAdU4").await?; + Ok(()) +} + +async fn check_role(ctx: &Context, msg: &Message, conf: &Config) -> bool { + msg.author.has_role(ctx, msg.guild_id.unwrap(), RoleId::from(u64::from_str(&conf.roles.cons).unwrap())).await.unwrap() +} \ No newline at end of file diff --git a/src/mindus.rs b/src/mindus.rs index 7f380db..3d0e986 100644 --- a/src/mindus.rs +++ b/src/mindus.rs @@ -22,6 +22,27 @@ impl TypeMapKey for TcpSock { type Value = TcpSock; } + +#[derive(serde::Deserialize, serde::Serialize)] +pub struct Config { + pub discord_token: String, + pub ip: String, + pub port: String, + pub prefix: String, + pub roles: Roles +} + + +#[derive(serde::Deserialize, serde::Serialize)] +pub struct Roles { + pub auth: String, + pub cons: String +} + +impl TypeMapKey for Config { + type Value = Config; +} + pub fn cons_rw(sock: &TcpSock, input: &str) -> String { let mut output = String::new(); @@ -43,21 +64,6 @@ pub fn cons_rw(sock: &TcpSock, input: &str) -> String { output } -#[derive(serde::Deserialize, serde::Serialize)] -pub struct Config { - pub discord_token: String, - pub ip: String, - pub port: String, - pub trigger: char, - pub roles: Roles -} - - -#[derive(serde::Deserialize, serde::Serialize)] -pub struct Roles { - pub auth: String, - pub cons: String -} pub fn init_conf() -> Config { @@ -92,7 +98,7 @@ let mut toml_file = OpenOptions::new() discord_token: String::from(""), ip: String::from("localhost"), port: String::from("6859"), - trigger: ';', + prefix: String::from(";"), roles: Roles { auth: String::from(""), cons: String::from("")