diff --git a/src/commands/eval.rs b/src/commands/eval.rs index ae3e5ae..97bb09e 100644 --- a/src/commands/eval.rs +++ b/src/commands/eval.rs @@ -1,29 +1,38 @@ -use crate::common::{Context, Error}; +use crate::common::{self, Context, Error}; use std::io::Cursor; /// Evaluates a Lamm program -#[poise::command(prefix_command, aliases("lamm"))] -pub async fn eval(ctx: Context<'_>, expr: poise::CodeBlock) -> Result<(), Error> { - let runtime = lamm::Runtime::new(Cursor::new(expr.code), ""); +#[poise::command(slash_command, prefix_command, aliases("lamm"))] +pub async fn eval(ctx: Context<'_>, + #[rest] + expr: String) -> Result<(), Error> +{ + let expr = if expr.starts_with("```\n") { + expr.strip_prefix("```\n") + .and_then(|s| s.strip_suffix("```")) + .unwrap_or(&expr) + } else if expr.starts_with("```") { + expr.strip_prefix("```") + .and_then(|s| s.strip_suffix("```")) + .unwrap_or(&expr) + } else if expr.starts_with('`') { + expr.strip_prefix("`") + .and_then(|s| s.strip_suffix("`")) + .unwrap_or(&expr) + } else { + &expr + }; - let values = runtime.values().fold(Ok(String::new()), |acc, v| { - if acc.is_err() { - return acc; - }; + let runtime = lamm::Runtime::new(Cursor::new(expr), ""); - let x = acc.unwrap(); - - match v { - Ok(lamm::Value::Nil) => Ok(x), - Ok(v) => Ok(format!("{x}\n{v}")), - Err(e) => Err(e), - } - }); + let values = runtime.values() + .map(|res| res.map(|value| value.to_string())) + .collect::, _>>(); match values { - Ok(values) => ctx.reply(format!("{values}")).await, - Err(e) => ctx.reply(format!("```ansi\n\x1b[31;1merror\x1b[0m: {e}\n```")).await, - }?; + Ok(values) => common::no_ping_reply(&ctx, format!("{}", values.join("\n"))).await?, + Err(e) => common::no_ping_reply(&ctx, format!("```ansi\n\x1b[31;1merror\x1b[0m: {e}\n```")).await?, + }; Ok(()) } \ No newline at end of file diff --git a/src/commands/mod.rs b/src/commands/mod.rs index 9792b03..9e126e5 100644 --- a/src/commands/mod.rs +++ b/src/commands/mod.rs @@ -8,10 +8,21 @@ mod eval; mod self_roles; mod settings; -use crate::common::{Data, Error}; +use crate::common::{Data, Error, Context}; + +/// Display a help menu +#[poise::command(prefix_command, slash_command)] +async fn help(ctx: Context<'_>, + #[description = "Specific command to get help with"] + command: Option) -> Result<(), Error> +{ + poise::builtins::help(ctx, command.as_deref(), poise::builtins::HelpConfiguration::default()).await?; + Ok(()) +} pub fn commands() -> Vec> { vec![ + help(), ping::ping(), dox::dox(), yeehaw::yeehaw(),