diff --git a/src/main.rs b/src/main.rs index d957c90..0657093 100644 --- a/src/main.rs +++ b/src/main.rs @@ -10,7 +10,6 @@ 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::process::exit; #[group] #[commands(ping, pong, console, git, discord, auth)] @@ -46,6 +45,7 @@ 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().await; @@ -107,7 +107,6 @@ async fn pong(ctx: &Context, msg: &Message) -> CommandResult { async fn console(ctx: &Context, msg: &Message, mut args: Args) -> CommandResult { let data = ctx.data.read().await; - let sock = data.get::().unwrap(); let conf = data.get::().unwrap(); @@ -149,41 +148,24 @@ async fn console(ctx: &Context, msg: &Message, mut args: Args) -> CommandResult .color(Color::ROSEWATER) ) }).await?; + return Ok(()); } + Err(_e) => { msg.channel_id.send_message(ctx, |m| { m.content("") .embed(|e| e .title("Error") - .description("Unable to connect to the mindustry server\nCheck if server has restarted\nBot shutting down") + .description("Unable to connect to the mindustry server\nCheck if server has restarted\nAttempting Reconnect") .color(Color::RED) ) }).await?; - exit(1); - // let mut w_data = ctx.data.write().await; - - // match TcpSock::new(conf.discord_settings.ip.clone(), conf.discord_settings.port.clone()) { - // Ok(n) => { - // w_data.insert::(n); - // println!("reconnected"); - // } - // Err(_e) => { - // msg.channel_id.send_message(ctx, |m| { - // m.content("") - // .embed(|e| e - // .title("Error") - // .description("Reconnection unsuccessful\nStopping bot") - // .color(Color::RED) - // ) - // }).await?; - // println!("unable to reconnect"); - // exit(1); - // } - // } - } + drop(data); + recon(ctx, msg).await; + // exit(1); + } } - Ok(()) } @@ -208,20 +190,24 @@ async fn auth(ctx: &Context, msg: &Message, args: Args) -> CommandResult { .color(Color::ROSEWATER) ) }).await?; + return Ok(()); } + Err(_e) => { msg.channel_id.send_message(ctx, |m| { m.content("") .embed(|e| e .title("Error") - .description("Unable to connect to the mindustry server\nCheck if server has restarted\nBot shutting down") + .description("Unable to connect to the mindustry server\nCheck if server has restarted\nAttempting Reconnect") .color(Color::RED) ) }).await?; - exit(1); + + drop(data); + recon(ctx, msg).await; + // exit(1); } } - Ok(()) } diff --git a/src/mindus.rs b/src/mindus.rs index d29cdba..f3403f5 100644 --- a/src/mindus.rs +++ b/src/mindus.rs @@ -1,9 +1,11 @@ use crate::structs::*; +use std::process::exit; use std::str::FromStr; use std::fs::{File, OpenOptions}; use std::io::{Read, Write, Seek, BufRead}; use serenity::model::prelude::{Message, RoleId}; use serenity::prelude::{Context}; +use serenity::utils::Color; use std::{str}; use serenity::prelude::SerenityError; use indoc::indoc; @@ -44,26 +46,26 @@ pub fn cons_rw(sock: &TcpSock, input: &str) -> std::io::Result { println!("connection to socket lost\nis the server stopped or restarted?"); return Err(std::io::Error::new(std::io::ErrorKind::Other, "unable to connect to server")) } - Err(e) => { - println!("error peeking {}", e); + Err(_e) => { + println!("queue empty"); } } - println!("clearing output variable"); + // println!("clearing output variable"); output.clear(); - println!("writing"); + // println!("writing"); writer.write((input.to_owned() + "\n").as_bytes()).expect("could not write to cons"); - println!("flushing writer"); + // println!("flushing writer"); writer.flush().expect("flush failed"); - let mut line = 0; + let mut _line = 0; loop { - line += 1; - println!("reading line number {}", line); + _line += 1; + // println!("reading line number {}", line); match reader.read_line(&mut output) { Ok(t) => t, Err(_) => break(), @@ -191,3 +193,40 @@ pub fn is_command(command: String, command_vec: &Vec) -> bool { } false } + +pub async fn recon(ctx: &Context, msg: &Message) { + + let data = ctx.data.read().await; + let conf = data.get::().unwrap(); + + match TcpSock::new(conf.discord_settings.ip.clone(), conf.discord_settings.port.clone()) { + Ok(n) => { + drop(data); + let mut w_data = ctx.data.try_write().expect("unable to create write"); + w_data.insert::(n); + println!("reconnected"); + + msg.channel_id.send_message(ctx, |m| { + m.content("") + .embed(|e| e + .title("Success") + .description("Reconnection Succeeded\nRetry your command :)") + .color(Color::ROSEWATER) + ) + }).await.unwrap(); + } + Err(_e) => { + msg.channel_id.send_message(ctx, |m| { + m.content("") + .embed(|e| e + .title("Error") + .description("Reconnection unsuccessful\nStopping bot") + .color(Color::RED) + ) + }).await.unwrap(); + println!("unable to reconnect"); + exit(1); + } + } + +} \ No newline at end of file diff --git a/src/structs.rs b/src/structs.rs index 96c13fa..27cc8ba 100644 --- a/src/structs.rs +++ b/src/structs.rs @@ -14,7 +14,7 @@ impl TcpSock { Err(e) => return Err(e), }; - stream.set_read_timeout(Some(Duration::from_millis(100)))?; + stream.set_read_timeout(Some(Duration::from_millis(200)))?; println!("Socket Connected!!"); Ok(TcpSock { stream }) } diff --git a/target/x86_64-unknown-linux-musl/release/msdb b/target/x86_64-unknown-linux-musl/release/msdb index b162c8c..86ea7e7 100755 Binary files a/target/x86_64-unknown-linux-musl/release/msdb and b/target/x86_64-unknown-linux-musl/release/msdb differ