Discord: gate username lookups

This commit is contained in:
Shadow
2026-01-27 22:47:17 -06:00
parent 14e4b88bf0
commit b01612c262
2 changed files with 31 additions and 13 deletions

View File

@@ -78,6 +78,7 @@ Status: beta.
- Web UI: auto-expand the chat compose textarea while typing (with sensible max height). (#2950) Thanks @shivamraut101.
- Gateway: prevent crashes on transient network errors (fetch failures, timeouts, DNS). Added fatal error detection to only exit on truly critical errors. Fixes #2895, #2879, #2873. (#2980) Thanks @elliotsecops.
- Agents: guard channel tool listActions to avoid plugin crashes. (#2859) Thanks @mbelinky.
- Discord: avoid resolving bare channel names to user DMs when a username matches. Thanks @thewilloftheshadow.
- Providers: update MiniMax API endpoint and compatibility mode. (#3064) Thanks @hlbbbbbbb.
- Telegram: treat more network errors as recoverable in polling. (#3013) Thanks @ryancontent.
- Discord: resolve usernames to user IDs for outbound messages. (#2649) Thanks @nonggialiang.

View File

@@ -81,11 +81,14 @@ export async function resolveDiscordTarget(
const trimmed = raw.trim();
if (!trimmed) return undefined;
// If already a known format, parse directly
const directParse = parseDiscordTarget(trimmed, options);
if (directParse && directParse.kind !== "channel" && !isLikelyUsername(trimmed)) {
const shouldLookup = isExplicitUserLookup(trimmed, options);
const directParse = safeParseDiscordTarget(trimmed, options);
if (directParse && directParse.kind !== "channel") {
return directParse;
}
if (!shouldLookup) {
return directParse ?? parseDiscordTarget(trimmed, options);
}
// Try to resolve as a username via directory lookup
try {
@@ -110,15 +113,29 @@ export async function resolveDiscordTarget(
return parseDiscordTarget(trimmed, options);
}
/**
* Check if a string looks like a Discord username (not a mention, prefix, or ID).
* Usernames typically don't start with special characters except underscore.
*/
function isLikelyUsername(input: string): boolean {
// Skip if it's already a known format
if (/^(user:|channel:|discord:|@|<@!?)|[\d]+$/.test(input)) {
return false;
function safeParseDiscordTarget(
input: string,
options: DiscordTargetParseOptions,
): MessagingTarget | undefined {
try {
return parseDiscordTarget(input, options);
} catch {
return undefined;
}
// Likely a username if it doesn't match known patterns
return true;
}
function isExplicitUserLookup(input: string, options: DiscordTargetParseOptions): boolean {
if (/^<@!?(\d+)>$/.test(input)) {
return true;
}
if (/^(user:|discord:)/.test(input)) {
return true;
}
if (input.startsWith("@")) {
return true;
}
if (/^\d+$/.test(input)) {
return options.defaultKind === "user";
}
return false;
}