From c3e171e9176896e1a2e069e205b031cb5f67a4ec Mon Sep 17 00:00:00 2001 From: Tom Date: Fri, 19 Aug 2022 20:48:26 +0200 Subject: [PATCH] Add new commands to use a json dict for more custom options --- Cargo.lock | 1 + Cargo.toml | 1 + src/commands/coms.rs | 70 ++++++++++++++++++++++++++++++++++++++++---- 3 files changed, 66 insertions(+), 6 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 04be2e8..cd5200a 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -876,6 +876,7 @@ dependencies = [ "lazy_static", "log", "poise", + "rand", "regex", "reqwest", "serde", diff --git a/Cargo.toml b/Cargo.toml index dc449a1..92b50d1 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -15,6 +15,7 @@ lazy_static = "1" log = "0.4" simplelog = "0.11.1" poise = { version = "0.3", features = ["collector"] } +rand = "0.8" regex = {version="1"} reqwest = "0.11" # sqlx = { version = "0.5", features = ["runtime-tokio-rustls", "sqlite", "offline"] } diff --git a/src/commands/coms.rs b/src/commands/coms.rs index 878ce6b..eb873d8 100644 --- a/src/commands/coms.rs +++ b/src/commands/coms.rs @@ -1,14 +1,12 @@ -use crate::{commands::utils, Context, Error}; +use crate::{Context, Error}; use cached::proc_macro::cached; -use chrono::{DateTime, Utc}; -use log::warn; -use reqwest::header::AUTHORIZATION; -use serde::Deserialize; use serde_json::{ Map, Value::{self, Object}, }; use std::fs; +use rand::seq::IteratorRandom; + async fn autocomplete_command<'a>( _ctx: Context<'_>, @@ -22,6 +20,7 @@ async fn autocomplete_command<'a>( res.into_iter() } +#[cached(time = 600)] fn get_coms() -> Map { let path = "./data/commands.json"; let data = fs::read_to_string(path).expect("Unable to read file"); @@ -46,9 +45,68 @@ pub async fn coms( ctx.say(format!("Command `{}` not found", command)).await?; } Some(com) => { - ctx.say(com.as_str().unwrap()).await?; + ctx.say(sky(com.as_str().unwrap())).await?; } } Ok(()) } + + +fn sky(input: &str) -> String { + let result: String = input.to_string(); + let mut rng = rand::thread_rng(); + loop { + match sky_open(&result) { + None => return result, + Some((left, end)) => { + match sky_closed(&end) { + None => return result, + Some((mid, right)) => return sky(&format!("{}{}{}", left, mid.split('|').choose(&mut rng).unwrap(), right)) + }; + } + } + } +} + +fn sky_open(input: &str) -> Option<(String, String)> { + match input.rsplit_once('{') { + None => (return None), + Some((left, end)) => { + if left.ends_with('\\') { + match sky_open(left) { + None => (return None), + Some((left, right)) => { + let mut end: String = end.to_string(); + end.push('{'); + end.push_str(right.as_str()); + return Some((left.to_owned(), end)) + } + }; + } else { + return Some((left.to_owned(), end.to_owned())); + } + } + } +} + +fn sky_closed(input: &str) -> Option<(String, String)> { + match input.split_once('}') { + None => (return None), + Some((left, end)) => { + if left.ends_with('\\') { + match sky_closed(end) { + None => (return None), + Some((mid, right)) => { + let mut start: String = left.to_string(); + start.push('}'); + start.push_str(mid.as_str()); + return Some((start, right.to_owned())) + } + }; + } else { + return Some((left.to_owned(), end.to_owned())); + } + } + } +} \ No newline at end of file