2020-10-15 16:52:08 +00:00
|
|
|
use crate::context::Context;
|
2020-10-31 21:03:17 +00:00
|
|
|
use crate::error::{BotError, BotError::CommandParsingError};
|
2020-10-17 15:47:17 +00:00
|
|
|
use async_trait::async_trait;
|
2020-10-31 21:03:17 +00:00
|
|
|
use parser::CommandParsingError::UnrecognizedCommand;
|
2020-10-16 12:40:25 +00:00
|
|
|
use thiserror::Error;
|
2020-04-21 06:09:43 +00:00
|
|
|
|
2020-10-31 12:40:44 +00:00
|
|
|
pub mod basic_rolling;
|
|
|
|
pub mod cofd;
|
2020-10-31 13:19:38 +00:00
|
|
|
pub mod cthulhu;
|
2020-10-31 12:40:44 +00:00
|
|
|
pub mod misc;
|
2020-04-21 03:15:13 +00:00
|
|
|
pub mod parser;
|
2020-10-31 12:40:44 +00:00
|
|
|
pub mod variables;
|
2020-04-21 03:15:13 +00:00
|
|
|
|
2020-10-16 12:40:25 +00:00
|
|
|
#[derive(Error, Debug)]
|
|
|
|
pub enum CommandError {
|
|
|
|
#[error("invalid command: {0}")]
|
|
|
|
InvalidCommand(String),
|
|
|
|
|
|
|
|
#[error("ignored command")]
|
|
|
|
IgnoredCommand,
|
|
|
|
}
|
|
|
|
|
2020-04-21 06:07:03 +00:00
|
|
|
pub struct Execution {
|
|
|
|
plain: String,
|
|
|
|
html: String,
|
2020-04-21 03:15:13 +00:00
|
|
|
}
|
|
|
|
|
2020-04-21 06:07:03 +00:00
|
|
|
impl Execution {
|
|
|
|
pub fn plain(&self) -> &str {
|
|
|
|
&self.plain
|
2020-04-21 03:15:13 +00:00
|
|
|
}
|
|
|
|
|
2020-04-21 06:07:03 +00:00
|
|
|
pub fn html(&self) -> &str {
|
|
|
|
&self.html
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-17 15:47:17 +00:00
|
|
|
#[async_trait]
|
|
|
|
pub trait Command: Send + Sync {
|
|
|
|
async fn execute(&self, ctx: &Context) -> Execution;
|
2020-08-21 21:49:22 +00:00
|
|
|
fn name(&self) -> &'static str;
|
2020-04-21 06:07:03 +00:00
|
|
|
}
|
|
|
|
|
2020-10-17 15:47:17 +00:00
|
|
|
/// Parse a command string into a dynamic command execution trait
|
|
|
|
/// object. Returns an error if a command was recognized but not
|
|
|
|
/// parsed correctly. Returns Ok(None) if no command was recognized.
|
|
|
|
pub fn parse(s: &str) -> Result<Box<dyn Command>, BotError> {
|
|
|
|
match parser::parse_command(s) {
|
2020-10-31 21:03:17 +00:00
|
|
|
Ok(command) => Ok(command),
|
|
|
|
Err(CommandParsingError(UnrecognizedCommand(_))) => {
|
|
|
|
Err(CommandError::IgnoredCommand.into())
|
|
|
|
}
|
2020-10-17 15:47:17 +00:00
|
|
|
Err(e) => Err(e),
|
2020-10-15 16:52:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub struct CommandResult {
|
|
|
|
pub plain: String,
|
|
|
|
pub html: String,
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Attempt to execute a command, and return the content that should
|
|
|
|
/// go back to Matrix, if the command was executed (successfully or
|
|
|
|
/// not). If a command is determined to be ignored, this function will
|
|
|
|
/// return None, signifying that we should not send a response.
|
2020-10-17 15:47:17 +00:00
|
|
|
pub async fn execute_command(ctx: &Context) -> Option<CommandResult> {
|
|
|
|
let res = parse(&ctx.message_body);
|
2020-10-15 16:52:08 +00:00
|
|
|
|
|
|
|
let (plain, html) = match res {
|
2020-10-17 15:47:17 +00:00
|
|
|
Ok(cmd) => {
|
|
|
|
let execution = cmd.execute(ctx).await;
|
|
|
|
(execution.plain().into(), execution.html().into())
|
|
|
|
}
|
2020-10-15 16:52:08 +00:00
|
|
|
Err(BotError::CommandError(CommandError::IgnoredCommand)) => return None,
|
|
|
|
Err(e) => {
|
|
|
|
let message = format!("Error parsing command: {}", e);
|
|
|
|
let html_message = format!("<p><strong>{}</strong></p>", message);
|
|
|
|
(message, html_message)
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
let plain = format!("{}\n{}", ctx.username, plain);
|
|
|
|
let html = format!("<p>{}</p>\n{}", ctx.username, html);
|
|
|
|
|
|
|
|
Some(CommandResult {
|
|
|
|
plain: plain,
|
|
|
|
html: html,
|
|
|
|
})
|
2020-04-21 03:15:13 +00:00
|
|
|
}
|
2020-08-21 21:49:22 +00:00
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use super::*;
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn chance_die_is_not_malformed() {
|
2020-10-17 20:24:24 +00:00
|
|
|
assert!(parse("!chance").is_ok());
|
2020-08-21 21:49:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn roll_malformed_expression_test() {
|
2020-10-17 20:24:24 +00:00
|
|
|
assert!(parse("!roll 1d20asdlfkj").is_err());
|
|
|
|
assert!(parse("!roll 1d20asdlfkj ").is_err());
|
2020-08-21 21:49:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2020-08-30 20:48:52 +00:00
|
|
|
fn roll_dice_pool_malformed_expression_test() {
|
2020-10-17 20:24:24 +00:00
|
|
|
assert!(parse("!pool 8abc").is_err());
|
|
|
|
assert!(parse("!pool 8abc ").is_err());
|
2020-08-30 20:48:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn pool_whitespace_test() {
|
2020-10-17 20:24:24 +00:00
|
|
|
parse("!pool ns3:8 ").expect("was error");
|
|
|
|
parse(" !pool ns3:8").expect("was error");
|
|
|
|
parse(" !pool ns3:8 ").expect("was error");
|
2020-08-30 20:48:52 +00:00
|
|
|
}
|
|
|
|
|
2020-08-28 22:02:41 +00:00
|
|
|
#[test]
|
|
|
|
fn help_whitespace_test() {
|
2020-10-17 20:24:24 +00:00
|
|
|
parse("!help stuff ").expect("was error");
|
|
|
|
parse(" !help stuff").expect("was error");
|
|
|
|
parse(" !help stuff ").expect("was error");
|
2020-08-28 22:02:41 +00:00
|
|
|
}
|
|
|
|
|
2020-08-30 20:48:52 +00:00
|
|
|
#[test]
|
|
|
|
fn roll_whitespace_test() {
|
2020-10-17 20:24:24 +00:00
|
|
|
parse("!roll 1d4 + 5d6 -3 ").expect("was error");
|
|
|
|
parse("!roll 1d4 + 5d6 -3 ").expect("was error");
|
|
|
|
parse(" !roll 1d4 + 5d6 -3 ").expect("was error");
|
2020-08-21 21:49:22 +00:00
|
|
|
}
|
|
|
|
}
|