fix: restore regressions from discord-only refactor
This commit is contained in:
@@ -26,6 +26,12 @@ vi.mock('../logger.js', () => ({
|
||||
},
|
||||
}));
|
||||
|
||||
const isPairedRoomJidMock = vi.hoisted(() => vi.fn(() => false));
|
||||
|
||||
vi.mock('../db.js', () => ({
|
||||
isPairedRoomJid: isPairedRoomJidMock,
|
||||
}));
|
||||
|
||||
// --- discord.js mock ---
|
||||
|
||||
type Handler = (...args: any[]) => any;
|
||||
@@ -196,6 +202,7 @@ async function triggerMessage(message: any) {
|
||||
describe('DiscordChannel', () => {
|
||||
beforeEach(() => {
|
||||
vi.clearAllMocks();
|
||||
isPairedRoomJidMock.mockReturnValue(false);
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
@@ -301,18 +308,54 @@ describe('DiscordChannel', () => {
|
||||
expect(opts.onMessage).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('delivers bot messages with is_bot_message flag', async () => {
|
||||
it('ignores its own bot messages', async () => {
|
||||
const opts = createTestOpts();
|
||||
const channel = new DiscordChannel('test-token', opts);
|
||||
await channel.connect();
|
||||
|
||||
const msg = createMessage({ isBot: true, content: 'I am a bot' });
|
||||
const msg = createMessage({
|
||||
authorId: '999888777',
|
||||
isBot: true,
|
||||
content: 'I am the connected bot',
|
||||
});
|
||||
await triggerMessage(msg);
|
||||
|
||||
expect(opts.onMessage).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('ignores other bot messages in normal rooms', async () => {
|
||||
const opts = createTestOpts();
|
||||
const channel = new DiscordChannel('test-token', opts);
|
||||
await channel.connect();
|
||||
|
||||
const msg = createMessage({
|
||||
authorId: '111222333',
|
||||
isBot: true,
|
||||
content: 'I am another bot',
|
||||
});
|
||||
await triggerMessage(msg);
|
||||
|
||||
expect(opts.onMessage).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('delivers other bot messages in paired rooms', async () => {
|
||||
isPairedRoomJidMock.mockReturnValue(true);
|
||||
|
||||
const opts = createTestOpts();
|
||||
const channel = new DiscordChannel('test-token', opts);
|
||||
await channel.connect();
|
||||
|
||||
const msg = createMessage({
|
||||
authorId: '111222333',
|
||||
isBot: true,
|
||||
content: 'I am another bot',
|
||||
});
|
||||
await triggerMessage(msg);
|
||||
|
||||
expect(opts.onMessage).toHaveBeenCalledWith(
|
||||
expect.any(String),
|
||||
'dc:1234567890123456',
|
||||
expect.objectContaining({
|
||||
content: 'I am a bot',
|
||||
content: 'I am another bot',
|
||||
is_bot_message: true,
|
||||
}),
|
||||
);
|
||||
|
||||
@@ -17,6 +17,7 @@ import {
|
||||
DATA_DIR,
|
||||
TRIGGER_PATTERN,
|
||||
} from '../config.js';
|
||||
import { isPairedRoomJid } from '../db.js';
|
||||
import { readEnvFile } from '../env.js';
|
||||
import { logger } from '../logger.js';
|
||||
|
||||
@@ -158,6 +159,7 @@ import {
|
||||
AgentType,
|
||||
Channel,
|
||||
ChannelMeta,
|
||||
NewMessage,
|
||||
OnChatMetadata,
|
||||
OnInboundMessage,
|
||||
RegisteredGroup,
|
||||
@@ -202,11 +204,12 @@ export class DiscordChannel implements Channel {
|
||||
});
|
||||
|
||||
this.client.on(Events.MessageCreate, async (message: Message) => {
|
||||
// Ignore own messages only
|
||||
if (message.author.id === this.client?.user?.id) return;
|
||||
|
||||
const channelId = message.channelId;
|
||||
const chatJid = `dc:${channelId}`;
|
||||
const isOwnBotMessage = message.author.id === this.client?.user?.id;
|
||||
if (isOwnBotMessage) return;
|
||||
if (message.author.bot && !isPairedRoomJid(chatJid)) return;
|
||||
|
||||
let content = message.content;
|
||||
const timestamp = message.createdAt.toISOString();
|
||||
const senderName =
|
||||
@@ -481,6 +484,10 @@ export class DiscordChannel implements Channel {
|
||||
return this.client !== null && this.client.isReady();
|
||||
}
|
||||
|
||||
isOwnMessage(msg: NewMessage): boolean {
|
||||
return !!msg.is_bot_message && msg.sender === this.client?.user?.id;
|
||||
}
|
||||
|
||||
ownsJid(jid: string): boolean {
|
||||
if (!jid.startsWith('dc:')) return false;
|
||||
if (!this.agentTypeFilter) return true;
|
||||
|
||||
Reference in New Issue
Block a user