Skip to content
Closed
Show file tree
Hide file tree
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
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ This file records the notable changes in each dsh-im release. Its format follows

### Fixed / 修复

- Discord 独占 Consumer 把对本应用托管身份组(`tags.bot_id` 等于本应用用户 ID,且本应用持有该身份组)的提及视为提及本应用,不需要开启普通采集;其他身份组、`@everyone` 仍不唤醒([BotHarness #1615](https://github.com/BotHarness/DeepSeekBot/issues/1615))。
Discord exclusive Consumers treat a mention of the app's own managed role (`tags.bot_id` equals the app's user ID and the app holds the role) as a mention of the app, without ordinary collection; other roles and `@everyone` still do not address it ([BotHarness #1615](https://github.com/BotHarness/DeepSeekBot/issues/1615)).

- QQ 独占 Consumer 保留原生提及的名称、ID 和精确正文 token,供现有外部提及块展示;仅接收应用的严格 `is_you` 提及可使用已认证应用名称补全,歧义标识不猜测,不改变正文、收件或 Echo 资格([BotHarness #1156](https://github.com/BotHarness/DeepSeekBot/issues/1156))。
QQ exclusive Consumers retain native mention names, IDs and exact body tokens for the existing external mention chips. Only a strict receiving-App `is_you` mention can use its authenticated application name as fallback; ambiguous identifiers are not guessed, and source text, intake and Echo qualification remain unchanged ([BotHarness #1156](https://github.com/BotHarness/DeepSeekBot/issues/1156)).

Expand Down
520 changes: 260 additions & 260 deletions lib/index.js

Large diffs are not rendered by default.

14 changes: 10 additions & 4 deletions src/channels/discord/discord-runtime.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { DiscordApi } from './discord-api.mjs';
import { createDiscordReachable } from './reachable-conversations.mjs';
import { createDiscordHistoryReader } from './history-reader.mjs';
import { createDiscordBridgeStatus, DiscordHarnessBridge } from './discord-bridge.mjs';
import { discordRefusal, verifiedDiscordAccount, discordMessageContentAllowed, inspectDiscordSourceChannel, normalizeDiscordExternalText,
import { discordRefusal, verifiedDiscordAccount, discordMessageContentAllowed, inspectDiscordSourceChannel, normalizeDiscordExternalText, discordOwnRoleMention,
qualifyDiscordReply, sendDiscordReply, sendDiscordCheckedText } from './external-consumer.mjs';

const DISCORD_GATEWAY_INTENTS = (1 << 0) | (1 << 9) | (1 << 12) | (1 << 15);
Expand Down Expand Up @@ -939,18 +939,24 @@ export class DiscordRuntime {
if (!this.#account || !this.#status.ready || generation !== this.#generation
|| !message?.guild_id || message.author?.bot === true || message.webhook_id
|| !Array.isArray(message.mentions)) return;
const mentioned = message.mentions.some(user => user.id === this.#account.userId);
if (!mentioned && (!this.#ordinaryAuthorized || this.#externalOrdinaryText() !== true
const userMentioned = message.mentions.some(user => user.id === this.#account.userId);
const roleCandidate = !userMentioned && Array.isArray(message.mention_roles) && message.mention_roles.length > 0;
if (!userMentioned && !roleCandidate && (!this.#ordinaryAuthorized || this.#externalOrdinaryText() !== true
|| typeof message.content !== 'string' || !message.content.trim())) return;
const checked = this.#checkedLifetime();
let channel;
if (roleCandidate) channel = await inspectDiscordSourceChannel(checked.api, message.channel_id, checked.account, checked.signal);
const mentioned = userMentioned || discordOwnRoleMention(message, channel) !== undefined;
if (!mentioned && (!this.#ordinaryAuthorized || this.#externalOrdinaryText() !== true
|| typeof message.content !== 'string' || !message.content.trim())) return;
if (!mentioned) {
const application = await checked.api.getCurrentApplication({ signal: checked.signal });
if (verifiedDiscordAccount({ id: checked.account.userId, bot: true }, application).fingerprint !== checked.account.fingerprint)
throw discordRefusal('account-changed');
checked.assertCurrent();
if (!discordMessageContentAllowed(application) || this.#externalOrdinaryText() !== true) return;
}
const channel = await inspectDiscordSourceChannel(checked.api, message.channel_id, checked.account, checked.signal);
channel ??= await inspectDiscordSourceChannel(checked.api, message.channel_id, checked.account, checked.signal);
let event = normalizeDiscordExternalText(message, { botId: this.#config.botId, account: checked.account,
channel, eventId: `gateway:${this.#sessionId}:${sequence}`, ordinaryText: !mentioned });
checked.assertCurrent();
Expand Down
16 changes: 14 additions & 2 deletions src/channels/discord/external-consumer.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,12 @@ export async function inspectDiscordSourceChannel(api, channelId, account, signa
signal?.throwIfAborted();
const name = [guild.name, parent.name].every(value => typeof value === 'string' && value.trim())
? `${guild.name.trim()} #${parent.name.trim()}`.slice(0, 512) : undefined;
const ownRoleIds = (Array.isArray(guild.roles) ? guild.roles : [])
.filter(role => discordSnowflake(role?.id) && role.managed === true && role.tags?.bot_id === account.userId
&& Array.isArray(member?.roles) && member.roles.includes(role.id))
.map(role => role.id);
return { guildId: guild.id, conversationId: parent.id, channelId: channel.id, ...(name ? { name } : {}),
...(isThread ? { threadId: channel.id } : {}) };
...(isThread ? { threadId: channel.id } : {}), ...(ownRoleIds.length ? { ownRoleIds } : {}) };
} catch (error) { throw nativeFailure(error, 'source-not-found'); }
}

Expand All @@ -106,6 +110,11 @@ export function discordMessageContentAllowed(application) {
return (flags & ((1n << 18n) | (1n << 19n))) !== 0n;
}

export function discordOwnRoleMention(message, channel) {
if (!Array.isArray(message?.mention_roles) || !Array.isArray(channel?.ownRoleIds)) return undefined;
return message.mention_roles.find(id => discordSnowflake(id) && channel.ownRoleIds.includes(id));
}

export function normalizeDiscordExternalText(message, { botId, account, channel, eventId, ordinaryText = false }) {
return normalizeDiscordHumanText(message, { botId, account, channel, eventId }, ordinaryText !== true);
}
Expand All @@ -122,7 +131,8 @@ function normalizeDiscordHumanText(message, { botId, account, channel, eventId }
|| message.content.length > 16000 || !Array.isArray(message.mentions)) return null;
if (message.guild_id !== channel.guildId || message.channel_id !== channel.channelId)
throw discordRefusal('stale-route');
const mentionedAccount = message.mentions.some(user => user.id === account.userId);
const ownRole = discordOwnRoleMention(message, channel);
const mentionedAccount = message.mentions.some(user => user.id === account.userId) || ownRole !== undefined;
if (requireMention && !mentionedAccount) return null;
const timestamp = Date.parse(message.timestamp);
if (!Number.isFinite(timestamp) || typeof eventId !== 'string' || !eventId || eventId.length > 512)
Expand All @@ -132,6 +142,8 @@ function normalizeDiscordHumanText(message, { botId, account, channel, eventId }
const mentions = message.mentions.filter(user => discordSnowflake(user.id)).slice(0, 100)
.map(user => ({ id: user.id, key: `<@${user.id}>`,
...(user.id === account.userId ? { name: account.name } : {}) }));
if (ownRole !== undefined && !mentions.some(mention => mention.id === account.userId) && mentions.length < 100)
mentions.push({ id: account.userId, key: `<@&${ownRole}>`, ...(account.name ? { name: account.name } : {}) });
return { version: 1, channel: 'discord', botId, fingerprint: account.fingerprint,
eventId, messageId: message.id, actor: { kind: 'user', id: message.author.id, ...(name ? { name: name.slice(0, 512) } : {}) },
conversation: { kind: 'group', id: channel.conversationId, ...(channel.name ? { name: channel.name } : {}) }, mentions, mentionedAccount,
Expand Down
28 changes: 28 additions & 0 deletions test/channels/discord/external-consumer.test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,34 @@ test('channel mention is native direct addressing, preserves IDs and creates no
f.message.author.bot = false; f.message.webhook_id = ids.actor; assert.equal(await event(f), null);
});

test("a mention of the app's own managed role addresses the app; other roles and @everyone do not", async () => {
const f = fixture(); const other = '888888888888888889';
f.guild.roles.push({ id: ids.role, permissions: '0', managed: true, tags: { bot_id: ids.bot } },
{ id: other, permissions: '0', managed: true, tags: { bot_id: ids.actor } });
f.member.roles = [ids.role];
f.message.mentions = []; f.message.content = `<@&${ids.role}> QA`; f.message.mention_roles = [ids.role];
const e = await event(f);
assert.equal(e.mentionedAccount, true);
assert.deepEqual(e.mentions, [{ id: ids.bot, key: `<@&${ids.role}>`, name: account.name }]);
f.message.mention_roles = [other]; assert.equal(await event(f), null);
f.message.mention_roles = []; f.message.mention_everyone = true; assert.equal(await event(f), null);
f.message.mention_roles = [ids.role]; f.member.roles = []; assert.equal(await event(f), null);
});

test('Gateway admits an own-role mention without ordinary opt-in and ignores other role mentions', async () => {
const f = ordinaryRuntimeFixture({ enabled: false, optedIn: false, thread: false });
f.guild.roles.push({ id: ids.role, permissions: '0', managed: true, tags: { bot_id: ids.bot } });
f.member.roles = [ids.role];
try {
await f.runtime.start();
await f.dispatch({ ...f.message, mentions: [], mention_roles: ['888888888888888889'], content: 'role ping' });
assert.equal(f.received.length, 0);
await f.dispatch({ ...f.message, mentions: [], mention_roles: [ids.role], content: `<@&${ids.role}> QA` });
assert.equal(f.received.length, 1);
assert.equal(f.received[0].mentionedAccount, true);
} finally { await f.runtime.stop(); }
});

test('existing public thread retains native child and parent channel without fabricating message ancestry', async () => {
const f = fixture(true); const e = await event(f);
assert.equal(e.conversation.id, ids.channel); assert.equal(e.reply.threadId, ids.thread);
Expand Down