Add help, know

main
mat ess 2023-02-28 20:03:47 -05:00
parent 38a879b9cd
commit 12c58fcec2
1 changed files with 61 additions and 3 deletions

View File

@ -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<HashMap<String, String>>,
}
type Error = Box<dyn std::error::Error + Send + Sync>;
type Context<'a> = poise::Context<'a, Data, Error>;
/// all of the commands supported by `buscemi`
pub fn commands() -> Vec<poise::Command<Data, Error>> {
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<String>,
) -> 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] <OBJECT> [is] <DEFINITION>
/// ```
/// 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(())
}