diff --git a/app/test/bot-gateway.test.ts b/app/test/bot-gateway.test.ts index 6086ca3..d371886 100644 --- a/app/test/bot-gateway.test.ts +++ b/app/test/bot-gateway.test.ts @@ -32,6 +32,7 @@ class FakeSource extends EventEmitter implements BotSource { sent: { guildId: string; payload: any }[] = []; handlers = new Map void; state: (d: any) => void }>(); commands: string[] = []; + wants?: (guildId: string) => boolean; me() { return ME; } @@ -274,9 +275,13 @@ test('봇 중계: 슬래시 명령은 그 서버를 지켜보는 앱으로 가 new Promise((resolve) => s.source.emit('command', { name, guildId: G, user: m(user, user), voiceChannel: { id: VC1, name: '일반' } } satisfies BotCommand, resolve)); try { assert.equal(await cmd('녹음시작', SUPER), '지금 이 서버에서 켜져 있는 겜녹이 앱이 없습니다.'); + // 지켜보는 앱이 없는 서버의 명령은 서버가 받지 않는다 (봇 토큰을 직접 쓰는 v1.1.0 PC 가 응답하게) + assert.equal(s.source.wants?.(G), false); await a.gw.start(); await b.gw.start(); await a.gw.watch(G, { commands: false }); + assert.equal(s.source.wants?.(G), true); + assert.equal(s.source.wants?.(OTHER_G), false); await b.gw.watch(G, { commands: false }); const seenBy: string[] = []; a.gw.on('command', (c, respond) => (seenBy.push(`a:${c.name}`), respond(`A 가 ${c.name}`))); diff --git a/site/server/src/bot-broker.ts b/site/server/src/bot-broker.ts index 6af9072..3cf831c 100644 --- a/site/server/src/bot-broker.ts +++ b/site/server/src/bot-broker.ts @@ -29,6 +29,8 @@ export interface BotCommand { * 이벤트: 'ready', 'guilds', 'voiceState' (BotVoiceEvent), 'command' (BotCommand, respond(content)) */ export interface BotSource extends EventEmitter { + /** 이 서버의 슬래시 명령을 받을지 (지켜보는 앱이 없으면 응답하지 않는다: 아직 봇 토큰을 직접 쓰는 v1.1.0 PC 의 응답을 가로채지 않게) */ + wants?: (guildId: string) => boolean; me(): BotGwMember | null; guilds(): BotGwGuild[]; /** 음성채널 목록과 음성채널에 있는 사람 */ @@ -76,6 +78,7 @@ export class BotBroker { source.on('voiceState', (e: BotVoiceEvent) => { for (const c of this.conns) if (c.guildId === e.guildId) this.send(c, { t: 'voiceState', ...e }); }); + source.wants = (guildId) => [...this.conns].some((c) => c.guildId === guildId); source.on('command', (cmd: BotCommand, respond: (content: string) => void) => void this.onCommand(cmd).then(respond)); // 등록 해제 · 로그아웃된 연결은 30초 안에 끊는다 (해제 · 로그아웃 API 는 kick 으로 바로 끊는다) this.timer = setInterval(() => { diff --git a/site/server/src/discord-source.ts b/site/server/src/discord-source.ts index 192355b..8bdcd2c 100644 --- a/site/server/src/discord-source.ts +++ b/site/server/src/discord-source.ts @@ -18,6 +18,7 @@ const isVoice = (c: { type: ChannelType } | null | undefined): c is VoiceBasedCh export class DiscordSource extends EventEmitter implements BotSource { readonly client: Client; + wants?: (guildId: string) => boolean; constructor(private log: { info: (o: any, m?: string) => void; error: (o: any, m?: string) => void }) { super(); @@ -39,6 +40,7 @@ export class DiscordSource extends EventEmitter implements BotSource { }); c.on(Events.InteractionCreate, async (i) => { if (!i.isChatInputCommand() || !i.guildId || !COMMANDS.some((x) => x.name === i.commandName)) return; + if (this.wants && !this.wants(i.guildId)) return; try { await i.deferReply({ flags: MessageFlags.Ephemeral }); } catch (e) {