Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Enable multiple bots to join Discord voice channels #1156

Merged
merged 1 commit into from
Dec 17, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading