Add coms command with the BOTtas custom command json source
This commit is contained in:
parent
4e5bfb1d23
commit
2ed80b6cf2
54
src/commands/coms.rs
Normal file
54
src/commands/coms.rs
Normal file
|
@ -0,0 +1,54 @@
|
|||
use crate::{commands::utils, 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;
|
||||
|
||||
async fn autocomplete_command<'a>(
|
||||
_ctx: Context<'_>,
|
||||
partial: &'a str,
|
||||
) -> impl Iterator<Item = String> + 'a {
|
||||
let res: Vec<String> = get_coms()
|
||||
.keys()
|
||||
.filter(move |e| e.contains(&partial))
|
||||
.map(|e| e.to_string())
|
||||
.collect();
|
||||
res.into_iter()
|
||||
}
|
||||
|
||||
fn get_coms() -> Map<String, Value> {
|
||||
let path = "./data/commands.json";
|
||||
let data = fs::read_to_string(path).expect("Unable to read file");
|
||||
let res: serde_json::Value = serde_json::from_str(&data).expect("Unable to parse");
|
||||
if let Object(things) = res {
|
||||
return things;
|
||||
} else {
|
||||
return Map::<String, Value>::new();
|
||||
}
|
||||
}
|
||||
|
||||
// F1TV links throughout the seasons
|
||||
#[poise::command(slash_command)]
|
||||
pub async fn coms(
|
||||
ctx: Context<'_>,
|
||||
#[description = "Which command to execute?"]
|
||||
#[autocomplete = "autocomplete_command"]
|
||||
command: String,
|
||||
) -> Result<(), Error> {
|
||||
match get_coms().get(&command) {
|
||||
None => {
|
||||
ctx.say(format!("Command `{}` not found", command)).await?;
|
||||
}
|
||||
Some(com) => {
|
||||
ctx.say(com.as_str().unwrap()).await?;
|
||||
}
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
|
@ -4,6 +4,7 @@ use poise::serenity_prelude as serenity;
|
|||
pub mod invites;
|
||||
mod links;
|
||||
// pub mod planning;
|
||||
pub mod coms;
|
||||
pub mod schedule;
|
||||
pub mod utils;
|
||||
|
||||
|
|
|
@ -185,6 +185,7 @@ async fn app() -> Result<(), Error> {
|
|||
commands::fix(),
|
||||
// commands::planning::get_command(),
|
||||
commands::get_links(),
|
||||
commands::coms::coms(),
|
||||
];
|
||||
|
||||
let discord = SETTINGS.read().unwrap().get_table("discord").unwrap();
|
||||
|
|
Loading…
Reference in a new issue