Skip to content

Commit

Permalink
fix: don't reply to self
Browse files Browse the repository at this point in the history
The refactor on `should_send` was broken. Simplifying return types to a
simple bool to make it more intuitive.

Follow-up to #84
  • Loading branch information
conorsch committed Nov 21, 2023
1 parent 1d84fa4 commit f7ae5cd
Showing 1 changed file with 13 additions and 7 deletions.
20 changes: 13 additions & 7 deletions src/handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,15 @@ impl Handler {
/// Check whether the bot can proceed with honoring this request,
/// performing preflight checks on server and channel configuration,
/// avoiding self-sends, and honoring ratelimits.
async fn should_send(&self, ctx: Context, message: Message) -> anyhow::Result<bool> {
async fn should_send(&self, ctx: Context, message: Message) -> bool {
tracing::trace!("parsing message: {:#?}", message);
let message_info = MessageInfo::new(message.clone(), ctx.clone())?;
let message_info = match MessageInfo::new(message.clone(), ctx.clone()) {
Ok(m) => m,
Err(e) => {
tracing::error!(%e, "failed to parse message");
return false;
}
};
let self_id = ctx.cache.current_user().id;

// Stop if we're not allowed to respond in this channel
Expand All @@ -63,22 +69,22 @@ impl Handler {
?message_info.guild_channel,
"not allowed to send messages in this channel"
);
return Ok(false);
return false;
}
} else {
return Ok(false);
return false;
};

// TODO: add check for channel id, bailing out if channel doesn't match

// Don't trigger on messages we ourselves send
if message_info.user_id == self_id {
tracing::trace!("detected message from ourselves");
return Ok(false);
return false;
}

// If we made it this far, it's OK to send.
return Ok(true);
return true;
}
}

Expand Down Expand Up @@ -248,7 +254,7 @@ impl EventHandler for Handler {
/// window, then Galileo will respond instructing the user to wait longer.
async fn message(&self, ctx: Context, message: Message) {
// Check whether we should proceed.
if let Err(_e) = self.should_send(ctx.clone(), message.clone()).await {
if !self.should_send(ctx.clone(), message.clone()).await {
return;
}

Expand Down

0 comments on commit f7ae5cd

Please sign in to comment.