moved read function to mindus.rs and removed ansi encoding
This commit is contained in:
@@ -7,5 +7,8 @@ edition = "2021"
|
||||
|
||||
[dependencies]
|
||||
dotenv = "0.15.0"
|
||||
serde = "1.0.148"
|
||||
serenity = "0.11"
|
||||
strip-ansi-escapes = "0.1.1"
|
||||
tokio = { version = "1.21.2", features = ["macros", "rt-multi-thread"] }
|
||||
toml = "0.5.9"
|
||||
|
||||
20
src/main.rs
20
src/main.rs
@@ -1,6 +1,5 @@
|
||||
mod mindus;
|
||||
use crate::mindus::*;
|
||||
use std::io::{Write, BufRead};
|
||||
use std::{env};
|
||||
use serenity::async_trait;
|
||||
use serenity::prelude::*;
|
||||
@@ -67,30 +66,17 @@ async fn pong(ctx: &Context, msg: &Message) -> CommandResult {
|
||||
async fn console(ctx: &Context, msg: &Message) -> CommandResult {
|
||||
|
||||
let input = msg.content.strip_prefix(";console ").to_owned();
|
||||
let output = &mut String::new();
|
||||
|
||||
if input == Option::None {
|
||||
msg.reply(ctx, "Not enough Parameters").await?;
|
||||
return Ok(());
|
||||
}
|
||||
|
||||
let data = ctx.data.read().await;
|
||||
|
||||
let sock = data.get::<TcpSock>().unwrap();
|
||||
|
||||
|
||||
let mut writer = std::io::BufWriter::new(sock.stream.try_clone()?);
|
||||
let mut reader = std::io::BufReader::new(sock.stream.try_clone()?);
|
||||
|
||||
writer.write((input.unwrap().to_owned() + "\n").as_bytes())?;
|
||||
writer.flush().expect("flush failed");
|
||||
|
||||
loop {
|
||||
match reader.read_line(output) {
|
||||
Ok(t) => t,
|
||||
Err(_) => break(),
|
||||
};
|
||||
}
|
||||
println!("{}", output);
|
||||
msg.reply(ctx, format!("```ansi\n{}\n```", output)).await?;
|
||||
msg.reply(ctx, format!("```ansi\n{}\n```", cons_rw(sock, &input.unwrap()))).await?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
use std::fs::{File, OpenOptions};
|
||||
use std::io::{Read, Write, Seek};
|
||||
use std::io::{Read, Write, Seek, BufRead};
|
||||
use std::time::Duration;
|
||||
use std::net::TcpStream;
|
||||
use serenity::prelude::TypeMapKey;
|
||||
@@ -22,6 +22,27 @@ impl TypeMapKey for TcpSock {
|
||||
type Value = TcpSock;
|
||||
}
|
||||
|
||||
pub fn cons_rw(sock: &TcpSock, input: &str) -> String {
|
||||
|
||||
let mut output = String::new();
|
||||
|
||||
let mut writer = std::io::BufWriter::new(sock.stream.try_clone().unwrap());
|
||||
let mut reader = std::io::BufReader::new(sock.stream.try_clone().unwrap());
|
||||
|
||||
writer.write((input.to_owned() + "\n").as_bytes()).unwrap();
|
||||
writer.flush().expect("flush failed");
|
||||
|
||||
loop {
|
||||
match reader.read_line(&mut output) {
|
||||
Ok(t) => t,
|
||||
Err(_) => break(),
|
||||
};
|
||||
}
|
||||
println!("{}", output);
|
||||
output = String::from_utf8(strip_ansi_escapes::strip(&output).unwrap()).unwrap();
|
||||
output
|
||||
}
|
||||
|
||||
#[derive(serde::Deserialize, serde::Serialize)]
|
||||
pub struct Config {
|
||||
pub ip: String,
|
||||
|
||||
Reference in New Issue
Block a user