config name changes

This commit is contained in:
poslop
2022-12-05 15:03:49 -06:00
parent 6360f329a6
commit d30662558f
4 changed files with 100 additions and 36 deletions

View File

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

View File

@@ -1,8 +0,0 @@
discordToken = ""
ip = "localhost"
port = "6859"
trigger = ";"
[roles]
auth = ""
cons = ""

View File

@@ -2,32 +2,66 @@ 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<UserId>,
) -> 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))
.help(&MY_HELP)
.group(&GENERAL_GROUP);
let intents = GatewayIntents::non_privileged() | GatewayIntents::MESSAGE_CONTENT;
@@ -40,6 +74,7 @@ async fn main() {
{
let mut data = client.data.write().await;
data.insert::<TcpSock>(sock);
data.insert::<Config>(conf)
}
if let Err(why) = client.start().await {
@@ -47,7 +82,9 @@ async fn main() {
}
}
#[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::<TcpSock>().unwrap();
let conf = data.get::<Config>().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()
}

View File

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