From 9eabb29851269da1ceff609a9e7c42fb17f37529 Mon Sep 17 00:00:00 2001 From: Ting Chien Meng Date: Mon, 16 Dec 2024 23:14:50 -0500 Subject: [PATCH] allow multiple bots to join the voice channel --- packages/client-discord/src/voice.ts | 22 ++++++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-) diff --git a/packages/client-discord/src/voice.ts b/packages/client-discord/src/voice.ts index ec45b0db94..86bec8bcdf 100644 --- a/packages/client-discord/src/voice.ts +++ b/packages/client-discord/src/voice.ts @@ -25,7 +25,7 @@ import { VoiceConnectionStatus, createAudioPlayer, createAudioResource, - getVoiceConnection, + getVoiceConnections, joinVoiceChannel, entersState, } from "@discordjs/voice"; @@ -194,7 +194,9 @@ export class VoiceManager extends EventEmitter { } async joinChannel(channel: BaseGuildVoiceChannel) { - const oldConnection = getVoiceConnection(channel.guildId as string); + const oldConnection = this.getVoiceConnection( + channel.guildId as string + ); if (oldConnection) { try { oldConnection.destroy(); @@ -212,6 +214,7 @@ export class VoiceManager extends EventEmitter { adapterCreator: channel.guild.voiceAdapterCreator as any, selfDeaf: false, selfMute: false, + group: this.client.user.id, }); try { @@ -328,6 +331,17 @@ export class VoiceManager extends EventEmitter { } } + private getVoiceConnection(guildId: string) { + const connections = getVoiceConnections(this.client.user.id); + if (!connections) { + return; + } + const connection = [...connections.values()].find( + (connection) => connection.joinConfig.guildId === guildId + ); + return connection; + } + private async monitorMember( member: GuildMember, channel: BaseGuildVoiceChannel @@ -335,7 +349,7 @@ export class VoiceManager extends EventEmitter { const userId = member?.id; const userName = member?.user?.username; const name = member?.user?.displayName; - const connection = getVoiceConnection(member?.guild?.id); + const connection = this.getVoiceConnection(member?.guild?.id); const receiveStream = connection?.receiver.subscribe(userId, { autoDestroy: true, emitClose: true, @@ -1069,7 +1083,7 @@ export class VoiceManager extends EventEmitter { } async handleLeaveChannelCommand(interaction: any) { - const connection = getVoiceConnection(interaction.guildId as any); + const connection = this.getVoiceConnection(interaction.guildId as any); if (!connection) { await interaction.reply("Not currently in a voice channel.");