diff --git a/src/commands.rs b/src/commands.rs index 9d400cb..c0db926 100644 --- a/src/commands.rs +++ b/src/commands.rs @@ -1,15 +1,31 @@ +use std::{collections::HashMap, sync::Mutex}; + use rand::seq::SliceRandom; /// this is where we keep data for the bot -/// we can probably eventually serialize this to disk for cheap persistence +// can we eventually serialize this to disk for cheap persistence? #[derive(Default)] -pub struct Data {} +pub struct Data { + facts: Mutex>, +} type Error = Box; type Context<'a> = poise::Context<'a, Data, Error>; /// all of the commands supported by `buscemi` pub fn commands() -> Vec> { - vec![ping(), eight_ball()] + vec![help(), ping(), eight_ball(), know()] +} + +/// print helpful information +#[poise::command(prefix_command, slash_command)] +async fn help( + ctx: Context<'_>, + #[description = "optional command to show help about"] + #[autocomplete = "poise::builtins::autocomplete_command"] + command: Option, +) -> Result<(), Error> { + poise::builtins::help(ctx, command.as_deref(), Default::default()).await?; + Ok(()) } /// check that the bot is working @@ -62,3 +78,45 @@ async fn eight_ball( ctx.say(answer).await?; Ok(()) } + +/// remember some important knowledge +/// +/// usage: +/// ``` +/// @buscemi (remember|know) [that] [is] +/// ``` +/// examples: +/// ``` +/// @buscemi remember that x is y +/// @buscemi know foo is bar +/// ``` +/// technically works but confusing: +/// ``` +/// @buscemi remember 1 2 +/// ``` +#[poise::command(prefix_command, slash_command, aliases("remember"))] +async fn know( + ctx: Context<'_>, + #[description = "the thing you want to define"] that: String, + #[rest] + #[description = "the definition of the thing"] + is: String, +) -> Result<(), Error> { + let (that, is) = if that.as_str() == "that" { + is.split_once(' ').unwrap() + } else { + (that.as_str(), is.as_str()) + }; + let is = is.strip_prefix("is ").unwrap_or(is); + let old = { + let mut facts = ctx.data().facts.lock().unwrap(); + facts.insert(that.to_string(), is.to_string()) + }; + let message = if let Some(old) = old { + format!("ok, {that} is {is} (changed from {old})") + } else { + format!("ok, {that} is {is}") + }; + ctx.say(message).await?; + Ok(()) +}