@@ -1,29 +1,38 @@
|
|||||||
use crate::common::{Context, Error};
|
use crate::common::{self, Context, Error};
|
||||||
use std::io::Cursor;
|
use std::io::Cursor;
|
||||||
|
|
||||||
/// Evaluates a Lamm program
|
/// Evaluates a Lamm program
|
||||||
#[poise::command(prefix_command, aliases("lamm"))]
|
#[poise::command(slash_command, prefix_command, aliases("lamm"))]
|
||||||
pub async fn eval(ctx: Context<'_>, expr: poise::CodeBlock) -> Result<(), Error> {
|
pub async fn eval(ctx: Context<'_>,
|
||||||
let runtime = lamm::Runtime::new(Cursor::new(expr.code), "<eval>");
|
#[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| {
|
let runtime = lamm::Runtime::new(Cursor::new(expr), "<eval>");
|
||||||
if acc.is_err() {
|
|
||||||
return acc;
|
|
||||||
};
|
|
||||||
|
|
||||||
let x = acc.unwrap();
|
let values = runtime.values()
|
||||||
|
.map(|res| res.map(|value| value.to_string()))
|
||||||
match v {
|
.collect::<Result<Vec<_>, _>>();
|
||||||
Ok(lamm::Value::Nil) => Ok(x),
|
|
||||||
Ok(v) => Ok(format!("{x}\n{v}")),
|
|
||||||
Err(e) => Err(e),
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
match values {
|
match values {
|
||||||
Ok(values) => ctx.reply(format!("{values}")).await,
|
Ok(values) => common::no_ping_reply(&ctx, format!("{}", values.join("\n"))).await?,
|
||||||
Err(e) => ctx.reply(format!("```ansi\n\x1b[31;1merror\x1b[0m: {e}\n```")).await,
|
Err(e) => common::no_ping_reply(&ctx, format!("```ansi\n\x1b[31;1merror\x1b[0m: {e}\n```")).await?,
|
||||||
}?;
|
};
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
@@ -8,10 +8,21 @@ mod eval;
|
|||||||
mod self_roles;
|
mod self_roles;
|
||||||
mod settings;
|
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<String>) -> Result<(), Error>
|
||||||
|
{
|
||||||
|
poise::builtins::help(ctx, command.as_deref(), poise::builtins::HelpConfiguration::default()).await?;
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
pub fn commands() -> Vec<Command<Data, Error>> {
|
pub fn commands() -> Vec<Command<Data, Error>> {
|
||||||
vec![
|
vec![
|
||||||
|
help(),
|
||||||
ping::ping(),
|
ping::ping(),
|
||||||
dox::dox(),
|
dox::dox(),
|
||||||
yeehaw::yeehaw(),
|
yeehaw::yeehaw(),
|
||||||
|
|||||||
Reference in New Issue
Block a user