forked from projectmoon/tenebrous-dicebot
Execute commands even when surrounded by weird whitespace.
This commit is contained in:
parent
3154f36dca
commit
472f02d153
12
src/bot.rs
12
src/bot.rs
|
@ -6,7 +6,7 @@ use crate::error::BotError;
|
||||||
use crate::state::DiceBotState;
|
use crate::state::DiceBotState;
|
||||||
use async_trait::async_trait;
|
use async_trait::async_trait;
|
||||||
use dirs;
|
use dirs;
|
||||||
use log::{debug, error, info, trace, warn};
|
use log::{debug, error, info, warn};
|
||||||
use matrix_sdk::Error as MatrixError;
|
use matrix_sdk::Error as MatrixError;
|
||||||
use matrix_sdk::{
|
use matrix_sdk::{
|
||||||
self,
|
self,
|
||||||
|
@ -136,12 +136,13 @@ impl DiceBot {
|
||||||
let mut results = Vec::with_capacity(msg_body.lines().count());
|
let mut results = Vec::with_capacity(msg_body.lines().count());
|
||||||
|
|
||||||
for command in msg_body.lines() {
|
for command in msg_body.lines() {
|
||||||
|
if !command.is_empty() {
|
||||||
let ctx = Context::new(&self.db, &room_id.as_str(), &sender_username, &command);
|
let ctx = Context::new(&self.db, &room_id.as_str(), &sender_username, &command);
|
||||||
|
|
||||||
if let Some(cmd_result) = execute_command(&ctx).await {
|
if let Some(cmd_result) = execute_command(&ctx).await {
|
||||||
results.push(cmd_result);
|
results.push(cmd_result);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if results.len() >= 1 {
|
if results.len() >= 1 {
|
||||||
if results.len() == 1 {
|
if results.len() == 1 {
|
||||||
|
@ -230,13 +231,6 @@ async fn should_process<'a>(
|
||||||
(String::new(), String::new())
|
(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))
|
Ok((msg_body, sender_username))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -55,6 +55,7 @@ pub fn parse(s: &str) -> Result<Box<dyn Command>, BotError> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Debug)]
|
||||||
pub struct CommandResult {
|
pub struct CommandResult {
|
||||||
pub plain: String,
|
pub plain: String,
|
||||||
pub html: String,
|
pub html: String,
|
||||||
|
|
|
@ -163,6 +163,21 @@ mod tests {
|
||||||
assert!(result.is_err());
|
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]
|
#[test]
|
||||||
fn word_with_exclamation_mark_test() {
|
fn word_with_exclamation_mark_test() {
|
||||||
let result1 = parse_command("hello !notacommand");
|
let result1 = parse_command("hello !notacommand");
|
||||||
|
|
Loading…
Reference in New Issue