Skip to content
74 changes: 51 additions & 23 deletions src/discord.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,10 @@ impl ChatAdapter for DiscordAdapter {
let builder = serenity::builder::CreateMessage::new()
.content(content)
.reference_message((ChannelId::new(ch_id), MessageId::new(msg_id)));
match ChannelId::new(ch_id).send_message(&self.http, builder).await {
match ChannelId::new(ch_id)
.send_message(&self.http, builder)
.await
{
Ok(msg) => Ok(MessageRef {
channel: channel.clone(),
message_id: msg.id.to_string(),
Expand All @@ -105,7 +108,9 @@ impl ChatAdapter for DiscordAdapter {
async fn delete_message(&self, msg: &MessageRef) -> anyhow::Result<()> {
let ch_id: u64 = Self::resolve_channel(&msg.channel).parse()?;
let msg_id: u64 = msg.message_id.parse()?;
self.http.delete_message(ChannelId::new(ch_id), MessageId::new(msg_id), None).await?;
self.http
.delete_message(ChannelId::new(ch_id), MessageId::new(msg_id), None)
.await?;
Ok(())
}

Expand Down Expand Up @@ -415,10 +420,13 @@ impl EventHandler for Handler {
let in_allowed_channel =
self.allow_all_channels || self.allowed_channels.contains(&channel_id);

let is_mentioned =
msg.mentions_user_id(bot_id) || msg.content.contains(&format!("<@{}>", bot_id))
let is_mentioned = msg.mentions_user_id(bot_id)
|| msg.content.contains(&format!("<@{}>", bot_id))
|| (!self.allowed_role_ids.is_empty()
&& msg.mention_roles.iter().any(|r| self.allowed_role_ids.contains(&r.get())));
&& msg
.mention_roles
.iter()
.any(|r| self.allowed_role_ids.contains(&r.get())));

// Bot message gating (from upstream #321)
if msg.author.bot {
Expand Down Expand Up @@ -702,25 +710,43 @@ impl EventHandler for Handler {
debug!(filename = %attachment.filename, "adding text file attachment");
extra_blocks.push(block);
}
} else if let Some(block) = media::download_and_encode_image(
&attachment.url,
attachment.content_type.as_deref(),
&attachment.filename,
u64::from(attachment.size),
None,
)
.await
{
debug!(url = %attachment.url, filename = %attachment.filename, "adding image attachment");
extra_blocks.push(block);
} else if media::is_video_file(&attachment.filename, attachment.content_type.as_deref()) {
debug!(url = %attachment.url, filename = %attachment.filename, "adding video attachment link");
extra_blocks.push(video_attachment_block(
&attachment.filename,
} else {
match media::download_and_encode_image(
&attachment.url,
attachment.content_type.as_deref(),
&attachment.filename,
u64::from(attachment.size),
&attachment.url,
));
None,
)
.await
{
Ok(block) => {
debug!(url = %attachment.url, filename = %attachment.filename, "adding image attachment");
extra_blocks.push(block);
}
Err(media::MediaFetchError::NotAnImage) => {
if media::is_video_file(
&attachment.filename,
attachment.content_type.as_deref(),
) {
debug!(url = %attachment.url, filename = %attachment.filename, "adding video attachment link");
extra_blocks.push(video_attachment_block(
&attachment.filename,
attachment.content_type.as_deref(),
u64::from(attachment.size),
&attachment.url,
));
}
}
Err(e) => {
tracing::warn!(
url = %attachment.url,
filename = %attachment.filename,
error = %e,
"image attachment failed"
);
}
}
}
}

Expand Down Expand Up @@ -1323,7 +1349,9 @@ fn resolve_mentions(content: &str, bot_id: UserId, allowed_role_ids: &HashSet<u6
let out = if allowed_role_ids.is_empty() {
out
} else {
allowed_role_ids.iter().fold(out, |s, id| s.replace(&format!("<@&{}>", id), ""))
allowed_role_ids
.iter()
.fold(out, |s, id| s.replace(&format!("<@&{}>", id), ""))
};
// 3. Other user mentions: keep <@UID> as-is so the LLM can mention back
// 4. Fallback: replace remaining role mentions only (user mentions are preserved)
Expand Down
Loading
Loading