Extract Discord inbound message handler

This commit is contained in:
ejclaw
2026-04-28 21:40:08 +09:00
parent 7fb27d238c
commit 55edab6475
2 changed files with 155 additions and 162 deletions

View File

@@ -68,11 +68,6 @@
"maxFunctionLines": 816,
"owner": "discord test hotspot"
},
"src/channels/discord.ts": {
"maxLines": 903,
"maxFunctionLines": 191,
"owner": "discord channel hotspot"
},
"src/codex-warmup.test.ts": {
"maxFunctionLines": 277,
"owner": "codex warmup test hotspot"

View File

@@ -225,6 +225,32 @@ export class DiscordChannel implements Channel {
});
this.client.on(Events.MessageCreate, async (message: Message) => {
await this.handleMessageCreate(message);
});
// Handle errors gracefully
this.client.on(Events.Error, (err) => {
logger.error({ err: err.message }, 'Discord client error');
});
return new Promise<void>((resolve) => {
this.client!.once(Events.ClientReady, (readyClient) => {
logger.info(
{ username: readyClient.user.tag, id: readyClient.user.id },
'Discord bot connected',
);
console.log(`\n Discord bot: ${readyClient.user.tag}`);
console.log(
` Use /chatid command or check channel IDs in Discord settings\n`,
);
resolve();
});
this.client!.login(this.botToken);
});
}
private async handleMessageCreate(message: Message): Promise<void> {
const channelId = message.channelId;
const chatJid = `dc:${channelId}`;
if (!this.receivesInbound) return;
@@ -341,13 +367,7 @@ export class DiscordChannel implements Channel {
// Store chat metadata for discovery
const isGroup = message.guild !== null;
this.opts.onChatMetadata(
chatJid,
timestamp,
chatName,
'discord',
isGroup,
);
this.opts.onChatMetadata(chatJid, timestamp, chatName, 'discord', isGroup);
// Only deliver full message for registered groups. Secondary role bots
// are configured as outbound-only, while the owner bot receives inbound.
@@ -382,28 +402,6 @@ export class DiscordChannel implements Channel {
{ chatJid, chatName, sender: senderName },
'Discord message stored',
);
});
// Handle errors gracefully
this.client.on(Events.Error, (err) => {
logger.error({ err: err.message }, 'Discord client error');
});
return new Promise<void>((resolve) => {
this.client!.once(Events.ClientReady, (readyClient) => {
logger.info(
{ username: readyClient.user.tag, id: readyClient.user.id },
'Discord bot connected',
);
console.log(`\n Discord bot: ${readyClient.user.tag}`);
console.log(
` Use /chatid command or check channel IDs in Discord settings\n`,
);
resolve();
});
this.client!.login(this.botToken);
});
}
async sendMessage(