Is it possible to have 2 bots in voice channels in discord js?

I registered two clients. I attempt to connect each to a voice channel, and after the first bot connects, the second will not connect. Additionally, getVoiceConnection returns a voice connection when the second bot attempts to connect
2 Replies
d.js toolkit
d.js toolkit12mo ago
- What's your exact discord.js npm list discord.js and node node -v version? - Not a discord.js issue? Check out #other-js-ts. - Consider reading #how-to-get-help to improve your question! - Explain what exactly your issue is. - Post the full error stack trace, not just the top part! - Show your code! - Issue solved? Press the button! - Marked as resolved by OP
dimsey
dimsey12mo ago
import { VoiceConnection, VoiceConnectionStatus, getVoiceConnection, joinVoiceChannel } from "@discordjs/voice";
import { ChannelType, Client, Events } from "discord.js";

export default function messageCreateEvent(client: Client, botId: string) {
client.on(Events.MessageCreate, async (message) => {
// Avoid bot replying to itself
if (message.author.bot) {
return;
}

if (client.user === null || client.user === undefined) {
await message.reply("Error occured: code 2001")
return;
}
// User DMs bot, bot has no guild context to join channels so user must do in server
else if (message.channel.type === ChannelType.DM) {
await message.reply('let\'s talk!! @ me in your server or in a specific voice channel');
}
else if (message.mentions.has(client.user)) {
const voiceConnection = getVoiceConnection(message.guildId ?? "")
if (voiceConnection && voiceConnection.state.status === VoiceConnectionStatus.Ready) {
// Bot is already connected to a voice channel
await message.reply("I'm already connected to a voice channel.");
return;
}

// Join voice channel of user if possible
if (message.channel.type === ChannelType.GuildVoice) {
console.log(`Bot ${client.user?.tag} was mentioned in voice channel ${message.channel.name}`);
// Join channel
// Listen and create connections -> out, listen for user
const voiceChannel = message.member?.voice.channel;

if (voiceChannel === undefined || voiceChannel === null) {
await message.reply("Error occured: code 2002")
return;
}

const connection = joinVoiceChannel({
channelId: voiceChannel.id,
guildId: voiceChannel.guild.id,
selfDeaf: false,
selfMute: false,
adapterCreator: voiceChannel.guild.voiceAdapterCreator,
});

// Run voice functions
} else {
// Otherwise join other voice channel
const voiceChannels = message.guild?.channels.cache.filter(c => c.type === ChannelType.GuildVoice);
if (voiceChannels !== undefined && voiceChannels.size > 0) {
const firstVoiceChannel = voiceChannels.first();
if (firstVoiceChannel === undefined) {
return console.log("Error")
}
try {
const connection = joinVoiceChannel({
channelId: firstVoiceChannel.id,
guildId: firstVoiceChannel.guild.id,
selfDeaf: false,
selfMute: false,
adapterCreator: firstVoiceChannel.guild.voiceAdapterCreator,
});
connection.on("stateChange", (oldState, newState) => {
console.log("newstate", newState.status, oldState.status)
})
} catch (e) {
console.log("VOice connection failed: ", e)
}

message.reply(`pls join me in "${firstVoiceChannel.name}" voice channel`)
return;
}

// No voice channels in server.
else {
await message.reply({ content: 'There\'s no voice channels to join.'});
return console.log('No voice channels available to join.');
}
}
}
})
}
import { VoiceConnection, VoiceConnectionStatus, getVoiceConnection, joinVoiceChannel } from "@discordjs/voice";
import { ChannelType, Client, Events } from "discord.js";

export default function messageCreateEvent(client: Client, botId: string) {
client.on(Events.MessageCreate, async (message) => {
// Avoid bot replying to itself
if (message.author.bot) {
return;
}

if (client.user === null || client.user === undefined) {
await message.reply("Error occured: code 2001")
return;
}
// User DMs bot, bot has no guild context to join channels so user must do in server
else if (message.channel.type === ChannelType.DM) {
await message.reply('let\'s talk!! @ me in your server or in a specific voice channel');
}
else if (message.mentions.has(client.user)) {
const voiceConnection = getVoiceConnection(message.guildId ?? "")
if (voiceConnection && voiceConnection.state.status === VoiceConnectionStatus.Ready) {
// Bot is already connected to a voice channel
await message.reply("I'm already connected to a voice channel.");
return;
}

// Join voice channel of user if possible
if (message.channel.type === ChannelType.GuildVoice) {
console.log(`Bot ${client.user?.tag} was mentioned in voice channel ${message.channel.name}`);
// Join channel
// Listen and create connections -> out, listen for user
const voiceChannel = message.member?.voice.channel;

if (voiceChannel === undefined || voiceChannel === null) {
await message.reply("Error occured: code 2002")
return;
}

const connection = joinVoiceChannel({
channelId: voiceChannel.id,
guildId: voiceChannel.guild.id,
selfDeaf: false,
selfMute: false,
adapterCreator: voiceChannel.guild.voiceAdapterCreator,
});

// Run voice functions
} else {
// Otherwise join other voice channel
const voiceChannels = message.guild?.channels.cache.filter(c => c.type === ChannelType.GuildVoice);
if (voiceChannels !== undefined && voiceChannels.size > 0) {
const firstVoiceChannel = voiceChannels.first();
if (firstVoiceChannel === undefined) {
return console.log("Error")
}
try {
const connection = joinVoiceChannel({
channelId: firstVoiceChannel.id,
guildId: firstVoiceChannel.guild.id,
selfDeaf: false,
selfMute: false,
adapterCreator: firstVoiceChannel.guild.voiceAdapterCreator,
});
connection.on("stateChange", (oldState, newState) => {
console.log("newstate", newState.status, oldState.status)
})
} catch (e) {
console.log("VOice connection failed: ", e)
}

message.reply(`pls join me in "${firstVoiceChannel.name}" voice channel`)
return;
}

// No voice channels in server.
else {
await message.reply({ content: 'There\'s no voice channels to join.'});
return console.log('No voice channels available to join.');
}
}
}
})
}
Each bot runs this in its client
export function initClient(token: string, botId: string) {
// If client already exists, destroy it and create a new one
if (botId in clientCache) {
console.warn(`Bot with ID ${botId} already exists.`);
const oldClient = clientCache[botId]
oldClient.destroy();
delete clientCache[botId];
}

try {
// Create client and add to cache to deal with resets, etc
const client = createClient();
clientCache[botId] = client;

// Setup client
errorEvent(client);
clientReadyEvent(client, "@ me to talk!!");
messageCreateEvent(client, botId);
loginClient(client, token);
} catch (e) {
console.log("error init")
}

}
export function initClient(token: string, botId: string) {
// If client already exists, destroy it and create a new one
if (botId in clientCache) {
console.warn(`Bot with ID ${botId} already exists.`);
const oldClient = clientCache[botId]
oldClient.destroy();
delete clientCache[botId];
}

try {
// Create client and add to cache to deal with resets, etc
const client = createClient();
clientCache[botId] = client;

// Setup client
errorEvent(client);
clientReadyEvent(client, "@ me to talk!!");
messageCreateEvent(client, botId);
loginClient(client, token);
} catch (e) {
console.log("error init")
}

}
discord.js@14.13.0 | node@v18.18.0 Any help would be greatly appreciated ty ty! Figrued it out Thanks @Qjuh , just got that figured out! Is there a certain reason for not running more than one bot on a node server? I was thinking it might be a good way to share the underlying resources
Want results from more Discord servers?
Add your server