From 27214dbc34ffe4e7cb7f2958fcc175a7afd400e8 Mon Sep 17 00:00:00 2001 From: Xiretza Date: Tue, 18 Jul 2023 20:15:01 +0000 Subject: [PATCH] Send error message if command fails --- src/bot.rs | 57 +++++++++++++++++++++++++++++++++++++----------------- 1 file changed, 39 insertions(+), 18 deletions(-) diff --git a/src/bot.rs b/src/bot.rs index 0687ce3..d092787 100644 --- a/src/bot.rs +++ b/src/bot.rs @@ -179,6 +179,7 @@ impl Bot { Ok(id) } + /// Collects the configured announcement rooms and joins them if necessary. #[instrument(skip(self))] async fn join_announce_rooms(&mut self) -> Result<()> { for room in &self.config.space.announce_rooms { @@ -205,28 +206,13 @@ impl Bot { Ok(()) } - #[instrument(skip(self))] - async fn handle_message( + #[instrument(skip(self, ev, room))] + async fn handle_command( self: &Arc, ev: &OriginalRoomMessageEvent, room: &Joined, + command: &str, ) -> Result<()> { - event!(Level::TRACE, ?ev, "handling message"); - if Some(&*ev.sender) == self.client.user_id() { - event!(Level::TRACE, "message from ourselves, ignoring"); - return Ok(()); - } - - let MessageType::Text(TextMessageEventContent { body, .. }) = &ev.content.msgtype else { - event!(Level::TRACE, "non-plaintext message, ignoring"); - return Ok(()); - }; - - let Some(command) = body.strip_prefix(&self.config.app.command_prefix) else { - event!(Level::TRACE, "non-command message, ignoring"); - return Ok(()); - }; - let reply = |msg: &str| { // workaround for broken IRC bridge // https://github.com/matrix-org/matrix-appservice-irc/issues/683#issuecomment-1312688727 @@ -254,8 +240,43 @@ impl Bot { _ => { reply("Unknown command").await?; } + }; + + Ok(()) + } + + #[instrument(skip(self))] + async fn handle_message( + self: &Arc, + ev: &OriginalRoomMessageEvent, + room: &Joined, + ) -> Result<()> { + event!(Level::TRACE, ?ev, "handling message"); + if Some(&*ev.sender) == self.client.user_id() { + event!(Level::TRACE, "message from ourselves, ignoring"); + return Ok(()); } + let MessageType::Text(TextMessageEventContent { body, .. }) = &ev.content.msgtype else { + event!(Level::TRACE, "non-plaintext message, ignoring"); + return Ok(()); + }; + + let Some(command) = body.strip_prefix(&self.config.app.command_prefix) else { + event!(Level::TRACE, "non-command message, ignoring"); + return Ok(()); + }; + + if let Err(error) = self.handle_command(ev, room, command).await { + event!(Level::WARN, ?error, "handling command failed"); + let _ignore = room + .send( + RoomMessageEventContent::text_plain("error handling command: {error}"), + None, + ) + .await; + }; + Ok(()) }