config support

This commit is contained in:
poslop
2022-12-04 15:39:12 -06:00
parent 6acf1b2119
commit ccb6350519
3 changed files with 95 additions and 36 deletions

View File

@@ -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<TcpStream>,
writer: std::io::BufWriter<TcpStream>
}
impl TypeMapKey for TcpSock {
type Value = TcpSock;
}
impl TcpSock {
pub fn new() -> std::io::Result<Self> {
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?;

79
src/mindus.rs Normal file
View File

@@ -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<Self> {
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<u8> = 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
}