Do not respond to or log ignored commands.

This commit is contained in:
projectmoon 2020-11-01 12:20:45 +00:00
parent 29ef21087f
commit 3ccd60c173
2 changed files with 29 additions and 23 deletions

View File

@ -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);
}
}

View File

@ -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<Box<dyn Command>, BotError> {
match parser::parse_command(s) {
Ok(command) => Ok(command),