Add new commands to use a json dict for more custom options

This commit is contained in:
Tom 2022-08-19 20:48:26 +02:00
parent 2ed80b6cf2
commit c3e171e917
3 changed files with 66 additions and 6 deletions

1
Cargo.lock generated
View file

@ -876,6 +876,7 @@ dependencies = [
"lazy_static",
"log",
"poise",
"rand",
"regex",
"reqwest",
"serde",

View file

@ -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"] }

View file

@ -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<String, Value> {
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()));
}
}
}
}