Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 28 additions & 6 deletions src/discord.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ use std::sync::LazyLock;
use serenity::async_trait;
use serenity::model::channel::{Message, ReactionType};
use serenity::model::gateway::Ready;
use serenity::model::id::{ChannelId, MessageId};
use serenity::model::id::{ChannelId, MessageId, UserId};
use serenity::model::user::User;
use serenity::prelude::*;
use std::collections::HashSet;
use std::sync::Arc;
Expand Down Expand Up @@ -167,7 +168,7 @@ impl EventHandler for Handler {
}

let prompt = if is_mentioned {
strip_mention(&msg.content)
resolve_mentions(&msg.content, bot_id, &msg.mentions)
} else {
msg.content.trim().to_string()
};
Expand Down Expand Up @@ -790,12 +791,33 @@ fn compose_display(tool_lines: &[ToolEntry], text: &str, streaming: bool) -> Str
out
}

static MENTION_RE: LazyLock<regex::Regex> = LazyLock::new(|| {
regex::Regex::new(r"<@[!&]?\d+>").unwrap()
static ROLE_MENTION_RE: LazyLock<regex::Regex> = LazyLock::new(|| {
regex::Regex::new(r"<@&\d+>").unwrap()
});
static USER_MENTION_RE: LazyLock<regex::Regex> = LazyLock::new(|| {
regex::Regex::new(r"<@!?\d+>").unwrap()
});

fn strip_mention(content: &str) -> String {
MENTION_RE.replace_all(content, "").trim().to_string()
fn resolve_mentions(content: &str, bot_id: UserId, mentions: &[User]) -> String {
// 1. Strip the bot's own trigger mention
let mut out = content
.replace(&format!("<@{}>", bot_id), "")
.replace(&format!("<@!{}>", bot_id), "");
// 2. Resolve known user mentions to @DisplayName
for user in mentions {
if user.id == bot_id {
continue;
}
let label = user.global_name.as_deref().unwrap_or(&user.name);
let display = format!("@{}", label);
out = out
.replace(&format!("<@{}>", user.id), &display)
.replace(&format!("<@!{}>", user.id), &display);
}
// 3. Fallback: replace any remaining unresolved mentions
let out = ROLE_MENTION_RE.replace_all(&out, "@(role)");
let out = USER_MENTION_RE.replace_all(&out, "@(user)").to_string();
out.trim().to_string()
}

fn shorten_thread_name(prompt: &str) -> String {
Expand Down
Loading