Skip to content

Commit 9eabb29

Browse files
tcm390shakkernerd
authored andcommitted
allow multiple bots to join the voice channel
1 parent 81d0273 commit 9eabb29

File tree

1 file changed

+18
-4
lines changed

1 file changed

+18
-4
lines changed

packages/client-discord/src/voice.ts

+18-4
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ import {
2525
VoiceConnectionStatus,
2626
createAudioPlayer,
2727
createAudioResource,
28-
getVoiceConnection,
28+
getVoiceConnections,
2929
joinVoiceChannel,
3030
entersState,
3131
} from "@discordjs/voice";
@@ -194,7 +194,9 @@ export class VoiceManager extends EventEmitter {
194194
}
195195

196196
async joinChannel(channel: BaseGuildVoiceChannel) {
197-
const oldConnection = getVoiceConnection(channel.guildId as string);
197+
const oldConnection = this.getVoiceConnection(
198+
channel.guildId as string
199+
);
198200
if (oldConnection) {
199201
try {
200202
oldConnection.destroy();
@@ -212,6 +214,7 @@ export class VoiceManager extends EventEmitter {
212214
adapterCreator: channel.guild.voiceAdapterCreator as any,
213215
selfDeaf: false,
214216
selfMute: false,
217+
group: this.client.user.id,
215218
});
216219

217220
try {
@@ -328,14 +331,25 @@ export class VoiceManager extends EventEmitter {
328331
}
329332
}
330333

334+
private getVoiceConnection(guildId: string) {
335+
const connections = getVoiceConnections(this.client.user.id);
336+
if (!connections) {
337+
return;
338+
}
339+
const connection = [...connections.values()].find(
340+
(connection) => connection.joinConfig.guildId === guildId
341+
);
342+
return connection;
343+
}
344+
331345
private async monitorMember(
332346
member: GuildMember,
333347
channel: BaseGuildVoiceChannel
334348
) {
335349
const userId = member?.id;
336350
const userName = member?.user?.username;
337351
const name = member?.user?.displayName;
338-
const connection = getVoiceConnection(member?.guild?.id);
352+
const connection = this.getVoiceConnection(member?.guild?.id);
339353
const receiveStream = connection?.receiver.subscribe(userId, {
340354
autoDestroy: true,
341355
emitClose: true,
@@ -1069,7 +1083,7 @@ export class VoiceManager extends EventEmitter {
10691083
}
10701084

10711085
async handleLeaveChannelCommand(interaction: any) {
1072-
const connection = getVoiceConnection(interaction.guildId as any);
1086+
const connection = this.getVoiceConnection(interaction.guildId as any);
10731087

10741088
if (!connection) {
10751089
await interaction.reply("Not currently in a voice channel.");

0 commit comments

Comments
 (0)