Skip to content

Commit

Permalink
Merge pull request #1156 from ai16z/tcm-discord-multiple-bot
Browse files Browse the repository at this point in the history
fix: Enable multiple bots to join Discord voice channels
  • Loading branch information
shakkernerd authored Dec 17, 2024
2 parents 94d374a + 3e736ea commit 68b02e6
Showing 1 changed file with 18 additions and 4 deletions.
22 changes: 18 additions & 4 deletions packages/client-discord/src/voice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import {
VoiceConnectionStatus,
createAudioPlayer,
createAudioResource,
getVoiceConnection,
getVoiceConnections,
joinVoiceChannel,
entersState,
} from "@discordjs/voice";
Expand Down Expand Up @@ -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();
Expand All @@ -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 {
Expand Down Expand Up @@ -328,14 +331,25 @@ 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
) {
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,
Expand Down Expand Up @@ -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.");
Expand Down

0 comments on commit 68b02e6

Please sign in to comment.