This commit is contained in:
poslop
2022-12-06 15:46:23 -06:00
parent ad20b2a599
commit 2340a03689
2 changed files with 50 additions and 22 deletions

View File

@@ -12,7 +12,7 @@ use std::collections::HashSet;
use std::str::FromStr; use std::str::FromStr;
#[group] #[group]
#[commands(ping, pong, console, git, discord)] #[commands(ping, pong, console, git, discord, auth)]
struct General; struct General;
struct Handler; struct Handler;
@@ -45,7 +45,8 @@ async fn normal_message(_ctx: &Context, msg: &Message) {
#[tokio::main] #[tokio::main]
async fn main() { async fn main() {
let conf = init_conf();
let conf = init_conf().await;
let sock = TcpSock::new(conf.ip.clone(), conf.port.clone()).unwrap(); let sock = TcpSock::new(conf.ip.clone(), conf.port.clone()).unwrap();
@@ -71,7 +72,7 @@ async fn main() {
} }
if let Err(why) = client.start().await { if let Err(why) = client.start().await {
println!("An error occurred while running the client: {:?}", why); println!("An error occurred while running the client: {:?} \n Check that there is a bot token set in config", why);
} }
} }
@@ -141,7 +142,7 @@ async fn auth(ctx: &Context, msg: &Message, args: Args) -> CommandResult {
let conf = data.get::<Config>().unwrap(); let conf = data.get::<Config>().unwrap();
if !check_role(ctx, msg, &conf.roles.auth).await.unwrap_or_else(|e| true) { if !check_role(ctx, msg, &conf.roles.auth).await.unwrap_or_else(|_e| true) {
msg.channel_id.send_message(ctx, |m| { msg.channel_id.send_message(ctx, |m| {
m.content("") m.content("")
.embed(|e| e .embed(|e| e
@@ -175,9 +176,9 @@ async fn discord(ctx: &Context, msg: &Message) -> CommandResult {
Ok(()) Ok(())
} }
async fn check_role(ctx: &Context, msg: &Message, conf: &Vec<String>) -> Result<bool, SerenityError> { async fn check_role(ctx: &Context, msg: &Message, roles: &Vec<String>) -> Result<bool, SerenityError> {
let mut invalid_roles = 0; let mut invalid_roles = 0;
for v_id in conf { for v_id in roles {
let u_id = match u64::from_str(&v_id) { let u_id = match u64::from_str(&v_id) {
Ok(n) => n, Ok(n) => n,
Err(e) => Err(e) =>
@@ -186,14 +187,14 @@ async fn check_role(ctx: &Context, msg: &Message, conf: &Vec<String>) -> Result<
continue continue
}, },
}; };
let id = RoleId::from(u_id);
let check = msg.author.has_role(ctx, msg.guild_id.unwrap(), id).await?; let check = msg.author.has_role(ctx, msg.guild_id.unwrap(), RoleId::from(u_id)).await?;
if check { if check {
return Ok(check) return Ok(check)
} }
} }
if invalid_roles == conf.len() { if invalid_roles == roles.len() {
return Ok(true) return Ok(true)
} }

View File

@@ -4,6 +4,7 @@ use std::time::Duration;
use std::net::TcpStream; use std::net::TcpStream;
use serenity::prelude::TypeMapKey; use serenity::prelude::TypeMapKey;
use std::str; use std::str;
use indoc::indoc;
pub struct TcpSock { pub struct TcpSock {
pub stream: TcpStream, pub stream: TcpStream,
@@ -65,14 +66,14 @@ pub fn cons_rw(sock: &TcpSock, input: &str) -> String {
} }
pub fn init_conf() -> Config { pub async fn init_conf() -> Config {
let mut toml_file = OpenOptions::new() let mut toml_file = OpenOptions::new()
.read(true) .read(true)
.write(true) .write(true)
.open("config.toml") .open("config.toml")
.unwrap_or_else(|_e| toml_make()); .unwrap_or_else(|_e| toml_make());
// .unwrap_or(toml_make());
let mut toml_str = String::new(); let mut toml_str = String::new();
@@ -94,18 +95,44 @@ let mut toml_file = OpenOptions::new()
.open("config.toml") .open("config.toml")
.unwrap(); .unwrap();
let fill_conf = Config { let fill_conf =
discord_token: String::from(""), indoc! {r#"
ip: String::from("localhost"), # Discord bot token
port: String::from("6859"), discord_token = ""
prefix: String::from(";"),
roles: Roles { # Ip of the mindustry server
auth: vec![String::from(""), String::from("")], ip = "localhost"
cons: vec![String::from("")]
} # Port of the mindustry server socket
}; # Run 'config socketInputPort' in the mindustry console to find this port
port = "6859"
# Prefix used to call commands
# Can be any word letter or symbol
prefix = ";"
# These are the roles needed in order to use the associated command
# If an invalid role is used it will be ignored. If all the roles are invalid or the list is empty then anyone can use the command
[roles]
# Auth command
auth = [""]
# console command
cons = ["738543444322156574", "822523680391037009"]
"#};
toml_file.write(toml::to_string(&fill_conf).unwrap().as_bytes()).expect("Unable to write to new file"); // let fill_conf = Config {
// discord_token: String::from(""),
// ip: String::from("localhost"),
// port: String::from("6859"),
// prefix: String::from(";"),
// roles: Roles {
// auth: vec![String::from(""), String::from("")],
// cons: vec![String::from("")]
// }
// };
toml_file.write(fill_conf.as_bytes()).expect("Unable to write to new file");
toml_file.flush().unwrap(); toml_file.flush().unwrap();
toml_file.rewind().unwrap(); toml_file.rewind().unwrap();
toml_file toml_file