Execute commands even when surrounded by weird whitespace.

This commit is contained in:
projectmoon 2020-11-05 23:03:22 +00:00
parent 3154f36dca
commit 472f02d153
3 changed files with 22 additions and 12 deletions

View File

@ -6,7 +6,7 @@ use crate::error::BotError;
use crate::state::DiceBotState;
use async_trait::async_trait;
use dirs;
use log::{debug, error, info, trace, warn};
use log::{debug, error, info, warn};
use matrix_sdk::Error as MatrixError;
use matrix_sdk::{
self,
@ -136,10 +136,11 @@ impl DiceBot {
let mut results = Vec::with_capacity(msg_body.lines().count());
for command in msg_body.lines() {
let ctx = Context::new(&self.db, &room_id.as_str(), &sender_username, &command);
if let Some(cmd_result) = execute_command(&ctx).await {
results.push(cmd_result);
if !command.is_empty() {
let ctx = Context::new(&self.db, &room_id.as_str(), &sender_username, &command);
if let Some(cmd_result) = execute_command(&ctx).await {
results.push(cmd_result);
}
}
}
@ -230,13 +231,6 @@ async fn should_process<'a>(
(String::new(), String::new())
};
//Command parser can handle non-commands, but faster to
//not parse them.
if !msg_body.starts_with("!") {
trace!("Ignoring non-command: {}", msg_body);
return Err(BotError::ShouldNotProcessError);
}
Ok((msg_body, sender_username))
}

View File

@ -55,6 +55,7 @@ pub fn parse(s: &str) -> Result<Box<dyn Command>, BotError> {
}
}
#[derive(Debug)]
pub struct CommandResult {
pub plain: String,
pub html: String,

View File

@ -163,6 +163,21 @@ mod tests {
assert!(result.is_err());
}
#[test]
fn newline_test() {
assert!(parse_command("\n!roll 1d4").is_ok());
}
#[test]
fn whitespace_and_newline_test() {
assert!(parse_command(" \n!roll 1d4").is_ok());
}
#[test]
fn newline_and_whitespace_test() {
assert!(parse_command("\n !cthroll 50").is_ok());
}
#[test]
fn word_with_exclamation_mark_test() {
let result1 = parse_command("hello !notacommand");