From ccb635051951dbea37fad79ff02d9c1056bbe299 Mon Sep 17 00:00:00 2001 From: poslop Date: Sun, 4 Dec 2022 15:39:12 -0600 Subject: [PATCH] config support --- config.toml | 4 +++ src/main.rs | 48 ++++++++----------------------- src/mindus.rs | 79 +++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 95 insertions(+), 36 deletions(-) create mode 100644 config.toml create mode 100644 src/mindus.rs diff --git a/config.toml b/config.toml new file mode 100644 index 0000000..acfefb8 --- /dev/null +++ b/config.toml @@ -0,0 +1,4 @@ +ip = "localhost" +port = "6859" +trigger = ";" +roles = "" diff --git a/src/main.rs b/src/main.rs index f1fa689..4bac934 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,42 +1,16 @@ +mod mindus; +use crate::mindus::*; use std::io::{Write, BufRead}; -use std::time::Duration; -use std::{env, io, clone}; +use std::{env}; use serenity::async_trait; -use serenity::futures::io::{BufReader, BufWriter}; use serenity::prelude::*; use serenity::model::channel::Message; use serenity::framework::standard::macros::{command, group}; -use serenity::framework::standard::{StandardFramework, CommandResult, Args}; +use serenity::framework::standard::{StandardFramework, CommandResult}; use dotenv::dotenv; -use std::net::TcpStream; - - - -struct TcpSock { - stream: TcpStream, - reader: std::io::BufReader, - writer: std::io::BufWriter -} - - -impl TypeMapKey for TcpSock { - type Value = TcpSock; -} - -impl TcpSock { - pub fn new() -> std::io::Result { - let stream = TcpStream::connect("localhost:6859").expect("Tcp connection fail"); - stream.set_read_timeout(Some(Duration::from_millis(200)))?; - let mut reader = std::io::BufReader::new(stream.try_clone()?); - let mut writer = std::io::BufWriter::new(stream.try_clone()?); - - Ok(TcpSock { stream, reader, writer }) - } -} - #[group] -#[commands(ping, pong, send)] +#[commands(ping, pong, console)] struct General; struct Handler; @@ -46,14 +20,16 @@ impl EventHandler for Handler {} #[tokio::main] async fn main() { - let sock = TcpSock::new().unwrap(); + let conf = init_conf(); + + let sock = TcpSock::new(conf.ip, conf.port).unwrap(); + dotenv().ok(); let framework = StandardFramework::new() - .configure(|c| c.prefix(";")) // set the bot's prefix to "~" + .configure(|c| c.prefix(conf.trigger)) .group(&GENERAL_GROUP); - // Login with a bot token from the environment let token = env::var("DISCORD_TOKEN").expect("token"); let intents = GatewayIntents::non_privileged() | GatewayIntents::MESSAGE_CONTENT; let mut client = Client::builder(&token, intents) @@ -88,9 +64,9 @@ async fn pong(ctx: &Context, msg: &Message) -> CommandResult { } #[command] -async fn send(ctx: &Context, msg: &Message) -> CommandResult { +async fn console(ctx: &Context, msg: &Message) -> CommandResult { - let input = msg.content.strip_prefix(";send ").to_owned(); + 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?; diff --git a/src/mindus.rs b/src/mindus.rs new file mode 100644 index 0000000..b928895 --- /dev/null +++ b/src/mindus.rs @@ -0,0 +1,79 @@ +use std::fs::{File, OpenOptions}; +use std::io::{Read, Write, Seek}; +use std::time::Duration; +use std::net::TcpStream; +use serenity::prelude::TypeMapKey; +use std::str; + +pub struct TcpSock { + pub stream: TcpStream, +} + +impl TcpSock { + pub fn new(ip: String, port: String) -> std::io::Result { + let stream = TcpStream::connect(format!("{}:{}", ip, port)).expect("Tcp connection fail"); + stream.set_read_timeout(Some(Duration::from_millis(200)))?; + + Ok(TcpSock { stream }) + } +} + +impl TypeMapKey for TcpSock { + type Value = TcpSock; +} + +#[derive(serde::Deserialize, serde::Serialize)] +pub struct Config { + pub ip: String, + pub port: String, + pub trigger: char, + pub roles: String +} + +pub 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(); + + toml_file.read_to_string(&mut toml_str).unwrap(); + + println!("{}", toml_str); + + let config: Config = toml::from_str(&toml_str).expect("unable to fill Config struct"); + + // let mut conf_vec: Vec = vec![]; + // toml_file.read_to_end(&mut conf_vec).expect("unable to read toml to string"); + // let toml_str = str::from_utf8(&conf_vec).expect("unable to convert to string"); + // println!("{}", toml_str); + // let config: Config = toml::from_str(toml_str).expect("unable to fill Config struct"); + + config +} + +fn toml_make() -> File { +println!("initializing config"); +let mut toml_file = OpenOptions::new() + .read(true) + .write(true) + .create(true) + .open("config.toml") + .unwrap(); + + let fill_conf = Config { + ip: String::from("localhost"), + port: String::from("6859"), + trigger: ';', + roles: String::new() + }; + + toml_file.write(toml::to_string(&fill_conf).unwrap().as_bytes()).expect("Unable to write to new file"); + toml_file.flush().unwrap(); + toml_file.rewind().unwrap(); + toml_file +} \ No newline at end of file