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;
#[group]
#[commands(ping, pong, console, git, discord)]
#[commands(ping, pong, console, git, discord, auth)]
struct General;
struct Handler;
@@ -45,7 +45,8 @@ async fn normal_message(_ctx: &Context, msg: &Message) {
#[tokio::main]
async fn main() {
let conf = init_conf();
let conf = init_conf().await;
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 {
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();
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| {
m.content("")
.embed(|e| e
@@ -175,9 +176,9 @@ async fn discord(ctx: &Context, msg: &Message) -> CommandResult {
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;
for v_id in conf {
for v_id in roles {
let u_id = match u64::from_str(&v_id) {
Ok(n) => n,
Err(e) =>
@@ -186,14 +187,14 @@ async fn check_role(ctx: &Context, msg: &Message, conf: &Vec<String>) -> Result<
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 {
return Ok(check)
}
}
if invalid_roles == conf.len() {
if invalid_roles == roles.len() {
return Ok(true)
}

View File

@@ -4,6 +4,7 @@ use std::time::Duration;
use std::net::TcpStream;
use serenity::prelude::TypeMapKey;
use std::str;
use indoc::indoc;
pub struct TcpSock {
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()
.read(true)
.write(true)
.open("config.toml")
.unwrap_or_else(|_e| toml_make());
// .unwrap_or(toml_make());
let mut toml_str = String::new();
@@ -94,18 +95,44 @@ let mut toml_file = OpenOptions::new()
.open("config.toml")
.unwrap();
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("")]
}
};
let fill_conf =
indoc! {r#"
# Discord bot token
discord_token = ""
# Ip of the mindustry server
ip = "localhost"
# 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.rewind().unwrap();
toml_file