Add new commands to use a json dict for more custom options
This commit is contained in:
parent
2ed80b6cf2
commit
c3e171e917
1
Cargo.lock
generated
1
Cargo.lock
generated
|
@ -876,6 +876,7 @@ dependencies = [
|
|||
"lazy_static",
|
||||
"log",
|
||||
"poise",
|
||||
"rand",
|
||||
"regex",
|
||||
"reqwest",
|
||||
"serde",
|
||||
|
|
|
@ -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"] }
|
||||
|
|
|
@ -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()));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue