From 3ccd60c173416c7f7a44cc3e267c38dfa2e22bd2 Mon Sep 17 00:00:00 2001 From: projectmoon Date: Sun, 1 Nov 2020 12:20:45 +0000 Subject: [PATCH] Do not respond to or log ignored commands. --- src/bot.rs | 49 +++++++++++++++++++++++++++---------------------- src/commands.rs | 3 ++- 2 files changed, 29 insertions(+), 23 deletions(-) diff --git a/src/bot.rs b/src/bot.rs index 86fadb9..2cd526b 100644 --- a/src/bot.rs +++ b/src/bot.rs @@ -143,31 +143,36 @@ impl DiceBot { } } - if results.len() == 1 { - let cmd_result = &results[0]; - let response = AnyMessageEventContent::RoomMessage(MessageEventContent::Notice( - NoticeMessageEventContent::html(cmd_result.plain.clone(), cmd_result.html.clone()), - )); + if results.len() >= 1 { + if results.len() == 1 { + let cmd_result = &results[0]; + let response = AnyMessageEventContent::RoomMessage(MessageEventContent::Notice( + NoticeMessageEventContent::html( + cmd_result.plain.clone(), + cmd_result.html.clone(), + ), + )); - let result = self.client.room_send(&room_id, response, None).await; - if let Err(e) = result { - let message = extract_error_message(e); - error!("Error sending message: {}", message); - }; - } else { - let message = format!("{}: Executed {} commands", sender_username, results.len()); - let response = AnyMessageEventContent::RoomMessage(MessageEventContent::Notice( - NoticeMessageEventContent::html(&message, &message), - )); + let result = self.client.room_send(&room_id, response, None).await; + if let Err(e) = result { + let message = extract_error_message(e); + error!("Error sending message: {}", message); + }; + } else if results.len() > 1 { + let message = format!("{}: Executed {} commands", sender_username, results.len()); + let response = AnyMessageEventContent::RoomMessage(MessageEventContent::Notice( + NoticeMessageEventContent::html(&message, &message), + )); - let result = self.client.room_send(&room_id, response, None).await; - if let Err(e) = result { - let message = extract_error_message(e); - error!("Error sending message: {}", message); - }; + let result = self.client.room_send(&room_id, response, None).await; + if let Err(e) = result { + let message = extract_error_message(e); + error!("Error sending message: {}", message); + }; + } + + info!("[{}] {} executed: {}", room_name, sender_username, msg_body); } - - info!("[{}] {} executed: {}", room_name, sender_username, msg_body); } } diff --git a/src/commands.rs b/src/commands.rs index 1e66ca5..cdc1821 100644 --- a/src/commands.rs +++ b/src/commands.rs @@ -43,7 +43,8 @@ pub trait Command: Send + Sync { /// 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. +/// parsed correctly. Returns IgnoredCommand error if no command was +/// recognized. pub fn parse(s: &str) -> Result, BotError> { match parser::parse_command(s) { Ok(command) => Ok(command),