audit follow-up: burn down lint hotspots and dedupe channel gating flows
This commit is contained in:
@@ -17,7 +17,14 @@ import type {
|
||||
ChannelAdapter,
|
||||
ChannelStatus,
|
||||
} from '../types.js';
|
||||
import { buildResetInboundMessage, normalizeResetCommandText, splitMessage } from '../utils.js';
|
||||
import {
|
||||
allowTrustedOrPairedSender,
|
||||
buildResetInboundMessage,
|
||||
isAllowedByAllowlist,
|
||||
normalizeResetCommandText,
|
||||
shouldIgnoreForMissingMention,
|
||||
splitMessage,
|
||||
} from '../utils.js';
|
||||
import type { PairingManager } from '../pairing.js';
|
||||
|
||||
/** Configuration for the Discord channel adapter. */
|
||||
@@ -98,7 +105,7 @@ export class DiscordAdapter implements ChannelAdapter {
|
||||
|
||||
// ── Message handler — route inbound messages ──
|
||||
this.client.on(Events.MessageCreate, (message: DiscordMessage) => {
|
||||
this.handleMessage(message);
|
||||
void this.handleMessage(message);
|
||||
});
|
||||
|
||||
// Log in and wait for the ready event
|
||||
@@ -162,7 +169,7 @@ export class DiscordAdapter implements ChannelAdapter {
|
||||
}
|
||||
|
||||
/** Internal: process an inbound Discord message. */
|
||||
private handleMessage(message: DiscordMessage): void {
|
||||
private async handleMessage(message: DiscordMessage): Promise<void> {
|
||||
if (!this.messageHandler) {return;}
|
||||
|
||||
// Ignore bot messages
|
||||
@@ -174,42 +181,42 @@ export class DiscordAdapter implements ChannelAdapter {
|
||||
if (!isDM) {
|
||||
// Check allowed guild IDs
|
||||
if (
|
||||
this.config.allowedGuildIds &&
|
||||
this.config.allowedGuildIds.length > 0 &&
|
||||
!this.config.allowedGuildIds.includes(message.guild!.id)
|
||||
!isAllowedByAllowlist(message.guild!.id, this.config.allowedGuildIds)
|
||||
) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Check allowed channel IDs
|
||||
if (
|
||||
this.config.allowedChannelIds &&
|
||||
this.config.allowedChannelIds.length > 0 &&
|
||||
!this.config.allowedChannelIds.includes(message.channelId)
|
||||
) {
|
||||
if (!isAllowedByAllowlist(message.channelId, this.config.allowedChannelIds)) {
|
||||
return;
|
||||
}
|
||||
|
||||
// ── Mention requirement in guild channels ──
|
||||
const requireMention = this.config.requireMention ?? true;
|
||||
if (requireMention && this.client?.user) {
|
||||
if (!message.mentions.has(this.client.user)) {
|
||||
return;
|
||||
}
|
||||
if (this.client?.user && shouldIgnoreForMissingMention({
|
||||
requireMention: this.config.requireMention,
|
||||
defaultRequireMention: true,
|
||||
mentionsBot: message.mentions.has(this.client.user),
|
||||
})) {
|
||||
return;
|
||||
}
|
||||
} else {
|
||||
// DM pairing check — if pairing is enabled, require approval
|
||||
const pm = this.config.pairingManager;
|
||||
if (pm?.enabled && !pm.isApproved('discord', message.channelId)) {
|
||||
const text = message.content.trim();
|
||||
if (text && pm.validateCode('discord', message.channelId, text)) {
|
||||
try {
|
||||
if (this.config.pairingManager?.enabled) {
|
||||
if (!await allowTrustedOrPairedSender({
|
||||
pairingManager: this.config.pairingManager,
|
||||
channel: 'discord',
|
||||
senderId: message.channelId,
|
||||
text: message.content ?? '',
|
||||
isTrusted: false,
|
||||
onPaired: async () => {
|
||||
if ('send' in message.channel) {
|
||||
(message.channel as any).send('Pairing successful! You can now chat with Flynn.');
|
||||
await (message.channel as { send: (content: string) => Promise<unknown> })
|
||||
.send('Pairing successful! You can now chat with Flynn.');
|
||||
}
|
||||
} catch { /* ignore send errors */ }
|
||||
},
|
||||
})) {
|
||||
return;
|
||||
}
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user