aboutsummaryrefslogtreecommitdiff
path: root/src/commands/roll.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/commands/roll.rs')
-rw-r--r--src/commands/roll.rs107
1 files changed, 47 insertions, 60 deletions
diff --git a/src/commands/roll.rs b/src/commands/roll.rs
index 45e3ba8..6cd084c 100644
--- a/src/commands/roll.rs
+++ b/src/commands/roll.rs
@@ -1,31 +1,18 @@
use std::result::Result as StdResult;
+use lazy_static::lazy_static;
use log::{
debug,
error,
};
use rand::prelude::*;
-use serenity::{
- framework::standard::{
- macros::command,
- Args,
- },
- model::channel::Message,
- prelude::*,
-};
use thiserror::Error;
-use lazy_static::lazy_static;
-use serenity::{
- framework::standard::{
- CommandError,
- CommandResult,
- },
- futures::TryFutureExt,
+use crate::{
+ util,
+ PoiseContext,
};
-use crate::util;
-
#[derive(pest_derive::Parser)]
#[grammar = "commands/calc.pest"]
struct Calc;
@@ -49,27 +36,26 @@ impl Calc {
Pair,
Pairs,
},
- prec_climber::PrecClimber,
+ pratt_parser::PrattParser,
Parser,
};
use self::Rule::*;
lazy_static! {
- static ref CLIMBER: PrecClimber<Rule> = {
- use pest::prec_climber::{
+ static ref CLIMBER: PrattParser<Rule> = {
+ use pest::pratt_parser::{
Assoc::*,
- Operator,
+ Op as Operator,
};
- PrecClimber::new(vec![
- Operator::new(add, Left)
- | Operator::new(sub, Left)
- | Operator::new(modulo, Left),
- Operator::new(mul, Left) | Operator::new(div, Left),
- Operator::new(dice, Left),
- Operator::new(pow, Right),
- ])
+ PrattParser::new()
+ .op(Operator::infix(add, Left)
+ | Operator::infix(sub, Left)
+ | Operator::infix(modulo, Left))
+ .op(Operator::infix(mul, Left) | Operator::infix(div, Left))
+ .op(Operator::infix(dice, Left))
+ .op(Operator::infix(pow, Right))
};
}
@@ -160,29 +146,33 @@ impl Calc {
}
fn eval_expr(p: Pairs<Rule>) -> StdResult<f64, CalcError> {
- CLIMBER.climb(p, eval_single_pair, |lhs, op, rhs| {
- let lhs = lhs?;
- let rhs = rhs?;
+ CLIMBER
+ .map_primary(eval_single_pair)
+ .map_infix(|lhs, op, rhs| {
+ let lhs = lhs?;
+ let rhs = rhs?;
- let result = match op.as_rule() {
- add => lhs + rhs,
- sub => lhs - rhs,
- mul => lhs * rhs,
- div => lhs / rhs,
- pow => lhs.powf(rhs),
- dice => {
- let dice_count = lhs as usize;
- let dice_faces = rhs as usize;
+ let result = match op.as_rule() {
+ add => lhs + rhs,
+ sub => lhs - rhs,
+ mul => lhs * rhs,
+ div => lhs / rhs,
+ pow => lhs.powf(rhs),
+ dice => {
+ let dice_count = lhs as usize;
+ let dice_faces = rhs as usize;
- let mut rng = thread_rng();
- (0..dice_count).map(|_| rng.gen_range(1..(dice_faces + 1))).sum::<usize>()
- as f64
- },
- _ => unreachable!(),
- };
+ let mut rng = thread_rng();
+ (0..dice_count)
+ .map(|_| rng.gen_range(1..(dice_faces + 1)))
+ .sum::<usize>() as f64
+ },
+ _ => unreachable!(),
+ };
- Ok(result)
- })
+ Ok(result)
+ })
+ .parse(p)
}
eval_expr(result)
@@ -212,21 +202,18 @@ mod test {
}
}
-#[command]
-#[aliases("calc", "calculate")]
-pub async fn roll(ctx: &Context, msg: &Message, args: Args) -> CommandResult {
- match Calc::eval(args.rest()) {
+#[poise::command(slash_command, prefix_command, guild_only, aliases("calc", "calculate"))]
+pub async fn roll(ctx: PoiseContext<'_>, #[rest] rest: String) -> anyhow::Result<()> {
+ match Calc::eval(&rest) {
Ok(result) => {
debug!("got calc result '{}'", result);
- util::send(ctx, msg.channel_id, &format!("{}", result), msg.tts)
- .map_err(CommandError::from)
- .await
+ util::reply(ctx, result.to_string()).await?;
},
Err(e) => {
- error!("error encountered reading calc '{}': {}", args.rest(), e);
- util::send(ctx, msg.channel_id, "I COULDN'T READ THAT YOU FUCK", msg.tts)
- .map_err(CommandError::from)
- .await
+ error!("error encountered reading calc '{}': {}", rest, e);
+ util::reply(ctx, "I COULDN'T READ THAT YOU FUCK").await?;
},
}
+
+ Ok(())
}