Send error message if command fails

This commit is contained in:
Xiretza 2023-07-18 20:15:01 +00:00
parent 17d076ed45
commit 27214dbc34

View file

@ -179,6 +179,7 @@ impl Bot {
Ok(id) Ok(id)
} }
/// Collects the configured announcement rooms and joins them if necessary.
#[instrument(skip(self))] #[instrument(skip(self))]
async fn join_announce_rooms(&mut self) -> Result<()> { async fn join_announce_rooms(&mut self) -> Result<()> {
for room in &self.config.space.announce_rooms { for room in &self.config.space.announce_rooms {
@ -205,28 +206,13 @@ impl Bot {
Ok(()) Ok(())
} }
#[instrument(skip(self))] #[instrument(skip(self, ev, room))]
async fn handle_message( async fn handle_command(
self: &Arc<Self>, self: &Arc<Self>,
ev: &OriginalRoomMessageEvent, ev: &OriginalRoomMessageEvent,
room: &Joined, room: &Joined,
command: &str,
) -> Result<()> { ) -> 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| { let reply = |msg: &str| {
// workaround for broken IRC bridge // workaround for broken IRC bridge
// https://github.com/matrix-org/matrix-appservice-irc/issues/683#issuecomment-1312688727 // https://github.com/matrix-org/matrix-appservice-irc/issues/683#issuecomment-1312688727
@ -254,8 +240,43 @@ impl Bot {
_ => { _ => {
reply("Unknown command").await?; reply("Unknown command").await?;
} }
};
Ok(())
} }
#[instrument(skip(self))]
async fn handle_message(
self: &Arc<Self>,
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(()) Ok(())
} }