reconneeeeeeect

This commit is contained in:
poslop
2023-02-17 16:22:34 -06:00
parent db541c1e55
commit 702d77c4e8
4 changed files with 63 additions and 38 deletions

View File

@@ -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::framework::standard::{StandardFramework, CommandResult, Args, HelpOptions, CommandGroup, help_commands};
use serenity::utils::Color; use serenity::utils::Color;
use std::collections::HashSet; use std::collections::HashSet;
use std::process::exit;
#[group] #[group]
#[commands(ping, pong, console, git, discord, auth)] #[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); println!("Message is not a command '{}'", msg.content);
} }
#[tokio::main] #[tokio::main]
async fn main() { async fn main() {
let conf = init_conf().await; 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 { async fn console(ctx: &Context, msg: &Message, mut args: Args) -> CommandResult {
let data = ctx.data.read().await; let data = ctx.data.read().await;
let sock = data.get::<TcpSock>().unwrap(); let sock = data.get::<TcpSock>().unwrap();
let conf = data.get::<Config>().unwrap(); let conf = data.get::<Config>().unwrap();
@@ -149,41 +148,24 @@ async fn console(ctx: &Context, msg: &Message, mut args: Args) -> CommandResult
.color(Color::ROSEWATER) .color(Color::ROSEWATER)
) )
}).await?; }).await?;
return Ok(());
} }
Err(_e) => { Err(_e) => {
msg.channel_id.send_message(ctx, |m| { msg.channel_id.send_message(ctx, |m| {
m.content("") m.content("")
.embed(|e| e .embed(|e| e
.title("Error") .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) .color(Color::RED)
) )
}).await?; }).await?;
exit(1);
// let mut w_data = ctx.data.write().await; drop(data);
recon(ctx, msg).await;
// match TcpSock::new(conf.discord_settings.ip.clone(), conf.discord_settings.port.clone()) { // exit(1);
// Ok(n) => { }
// w_data.insert::<TcpSock>(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);
// }
// }
}
} }
Ok(()) Ok(())
} }
@@ -208,20 +190,24 @@ async fn auth(ctx: &Context, msg: &Message, args: Args) -> CommandResult {
.color(Color::ROSEWATER) .color(Color::ROSEWATER)
) )
}).await?; }).await?;
return Ok(());
} }
Err(_e) => { Err(_e) => {
msg.channel_id.send_message(ctx, |m| { msg.channel_id.send_message(ctx, |m| {
m.content("") m.content("")
.embed(|e| e .embed(|e| e
.title("Error") .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) .color(Color::RED)
) )
}).await?; }).await?;
exit(1);
drop(data);
recon(ctx, msg).await;
// exit(1);
} }
} }
Ok(()) Ok(())
} }

View File

@@ -1,9 +1,11 @@
use crate::structs::*; use crate::structs::*;
use std::process::exit;
use std::str::FromStr; use std::str::FromStr;
use std::fs::{File, OpenOptions}; use std::fs::{File, OpenOptions};
use std::io::{Read, Write, Seek, BufRead}; use std::io::{Read, Write, Seek, BufRead};
use serenity::model::prelude::{Message, RoleId}; use serenity::model::prelude::{Message, RoleId};
use serenity::prelude::{Context}; use serenity::prelude::{Context};
use serenity::utils::Color;
use std::{str}; use std::{str};
use serenity::prelude::SerenityError; use serenity::prelude::SerenityError;
use indoc::indoc; use indoc::indoc;
@@ -44,26 +46,26 @@ pub fn cons_rw(sock: &TcpSock, input: &str) -> std::io::Result<String> {
println!("connection to socket lost\nis the server stopped or restarted?"); 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")) return Err(std::io::Error::new(std::io::ErrorKind::Other, "unable to connect to server"))
} }
Err(e) => { Err(_e) => {
println!("error peeking {}", e); println!("queue empty");
} }
} }
println!("clearing output variable"); // println!("clearing output variable");
output.clear(); output.clear();
println!("writing"); // println!("writing");
writer.write((input.to_owned() + "\n").as_bytes()).expect("could not write to cons"); 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"); writer.flush().expect("flush failed");
let mut line = 0; let mut _line = 0;
loop { loop {
line += 1; _line += 1;
println!("reading line number {}", line); // println!("reading line number {}", line);
match reader.read_line(&mut output) { match reader.read_line(&mut output) {
Ok(t) => t, Ok(t) => t,
Err(_) => break(), Err(_) => break(),
@@ -191,3 +193,40 @@ pub fn is_command(command: String, command_vec: &Vec<String>) -> bool {
} }
false false
} }
pub async fn recon(ctx: &Context, msg: &Message) {
let data = ctx.data.read().await;
let conf = data.get::<Config>().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::<TcpSock>(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);
}
}
}

View File

@@ -14,7 +14,7 @@ impl TcpSock {
Err(e) => return Err(e), 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!!"); println!("Socket Connected!!");
Ok(TcpSock { stream }) Ok(TcpSock { stream })
} }