CombustibleToast
CombustibleToast
DIAdiscord.js - Imagine an app
Created by CombustibleToast on 6/3/2023 in #djs-voice
Incorrect client joins channel when using joinVoiceChannel()
[email protected] npm v18.16.0 I have multiple clients logged in and I am trying to connect specific ones to different voice channels. The first time I do this it works fine; the correct bot (Loudspeaker 4) joins the correct channel. The next time I try to do this with a different bot (Loudspeaker 3), Loudspeaker 4 joins the channel instead despite Loudspeaker 3 instantiating the guild and channel. Here's the relevant code:
//In another function, these calls are made
//get the client
const loudspeakerClient = loudspeakerList.get(loudspeakerAssignmentResult.loudspeakerId);
//get the channel object
const voiceChannel = await loudspeakerClient.channels.fetch(interaction.member.voice.channel.id);
initLoudspeaker(loudspeakerClient, voiceChannel);
//...

async function initLoudspeaker(loudspeakerClient, channel) {
//loudspeakerClient is a Client object, channel is a BaseChannel object of a voice channel

const { joinVoiceChannel, createAudioPlayer, NoSubscriberBehavior } = require('@discordjs/voice');
console.log(`initializing "${loudspeakerClient.user.username}"`);
console.log(`Channel "${channel.name}" instantiated by "${channel.client.user.username}"`);
console.log(`Guild instantiated by "${channel.guild.client.user.username}"`);
loudspeakerClient.connection = joinVoiceChannel({
channelId: channel.id,
guildId: channel.guild.id,
adapterCreator: channel.guild.voiceAdapterCreator
});

//function continues...
}
//In another function, these calls are made
//get the client
const loudspeakerClient = loudspeakerList.get(loudspeakerAssignmentResult.loudspeakerId);
//get the channel object
const voiceChannel = await loudspeakerClient.channels.fetch(interaction.member.voice.channel.id);
initLoudspeaker(loudspeakerClient, voiceChannel);
//...

async function initLoudspeaker(loudspeakerClient, channel) {
//loudspeakerClient is a Client object, channel is a BaseChannel object of a voice channel

const { joinVoiceChannel, createAudioPlayer, NoSubscriberBehavior } = require('@discordjs/voice');
console.log(`initializing "${loudspeakerClient.user.username}"`);
console.log(`Channel "${channel.name}" instantiated by "${channel.client.user.username}"`);
console.log(`Guild instantiated by "${channel.guild.client.user.username}"`);
loudspeakerClient.connection = joinVoiceChannel({
channelId: channel.id,
guildId: channel.guild.id,
adapterCreator: channel.guild.voiceAdapterCreator
});

//function continues...
}
For the first connection, this is printed:
initializing "Loudspeaker 4"
Channel "General" instantiated by "Loudspeaker 4"
Guild instantiated by "Loudspeaker 4"
initializing "Loudspeaker 4"
Channel "General" instantiated by "Loudspeaker 4"
Guild instantiated by "Loudspeaker 4"
and the correct bot joins General as expected. When I move to a different channel and attempt the same thing, this is printed:
initializing "Loudspeaker 3"
Channel "Second Channel" instantiated by "Loudspeaker 3"
Guild instantiated by "Loudspeaker 3"
initializing "Loudspeaker 3"
Channel "Second Channel" instantiated by "Loudspeaker 3"
Guild instantiated by "Loudspeaker 3"
This should indicate that Loudspeaker 3 will join, but in reality Loudspeaker 4 just switches channels. According to this: ⁠https://discord.com/channels/222078108977594368/1090014703209693194/1090123144594980934, the guild.voiceAdapterCreator will cause the client who instantiated the guild to connect. As the logs show, L3 is instantiating the guild used to get the adapter. Additionally, Loudspeaker 4's connection updates to reflect the new channel, even though the connection is being assigned to Loudspeaker 3. This is shown by the log of the client, and the connection is assigned to that client. Any ideas on what's going wrong here?
8 replies
DIAdiscord.js - Imagine an app
Created by CombustibleToast on 6/2/2023 in #djs-voice
[SOLVED] Playback ends after ~1 minute without any errors emitted
Title. It sometimes becomes Autopaused, sometimes not. It eventually becomes Idle after a few minutes. No errors are emitted from the player nor the connection. Any ideas on how I can get more information on what's going wrong or any solutions to problems I have overlooked? Thanks. The bot's connection and player are being initialized like this:
const { joinVoiceChannel, createAudioPlayer, NoSubscriberBehavior } = require('@discordjs/voice');
loudspeakerClient.connection = joinVoiceChannel({
channelId: channel.id,
guildId: channel.guild.id,
adapterCreator: channel.guild.voiceAdapterCreator
});

//init loudspeaker with queue
loudspeakerClient.queue = [];

//init player
loudspeakerClient.player = createAudioPlayer({
behaviors: {
//I have tried both .Pause and commenting the line out
noSubscriber: NoSubscriberBehavior.Play,
//idk what this does but I tried turning it on in case it would print errors
debug: true
}
});

//install event handlers (DEBUGGING)
loudspeakerClient.player.on(AudioPlayerStatus.Paused, () => {
console.log("PAUSED!");
});
loudspeakerClient.player.on(AudioPlayerStatus.Playing, () => {
console.log("PLAYING!");
});
loudspeakerClient.player.on(AudioPlayerStatus.Idle, () => {
console.log("IDLE!");
});
loudspeakerClient.player.on(AudioPlayerStatus.Buffering, () => {
console.log("BUFFERING!");
});
loudspeakerClient.player.on(AudioPlayerStatus.AutoPaused, () => {
console.log("AUTOPAUSED!");
});
loudspeakerClient.player.on('error', error => {
console.log(`Error: ${error.message}`);
});
loudspeakerClient.player.on('stateChange', (oldState, newState) => {
console.log(`${oldState.status} -> ${newState.status}`);
});
loudspeakerClient.connection.on('error', error => {
console.log(`Connection Error: ${error.message}`);
});
const { joinVoiceChannel, createAudioPlayer, NoSubscriberBehavior } = require('@discordjs/voice');
loudspeakerClient.connection = joinVoiceChannel({
channelId: channel.id,
guildId: channel.guild.id,
adapterCreator: channel.guild.voiceAdapterCreator
});

//init loudspeaker with queue
loudspeakerClient.queue = [];

//init player
loudspeakerClient.player = createAudioPlayer({
behaviors: {
//I have tried both .Pause and commenting the line out
noSubscriber: NoSubscriberBehavior.Play,
//idk what this does but I tried turning it on in case it would print errors
debug: true
}
});

//install event handlers (DEBUGGING)
loudspeakerClient.player.on(AudioPlayerStatus.Paused, () => {
console.log("PAUSED!");
});
loudspeakerClient.player.on(AudioPlayerStatus.Playing, () => {
console.log("PLAYING!");
});
loudspeakerClient.player.on(AudioPlayerStatus.Idle, () => {
console.log("IDLE!");
});
loudspeakerClient.player.on(AudioPlayerStatus.Buffering, () => {
console.log("BUFFERING!");
});
loudspeakerClient.player.on(AudioPlayerStatus.AutoPaused, () => {
console.log("AUTOPAUSED!");
});
loudspeakerClient.player.on('error', error => {
console.log(`Error: ${error.message}`);
});
loudspeakerClient.player.on('stateChange', (oldState, newState) => {
console.log(`${oldState.status} -> ${newState.status}`);
});
loudspeakerClient.connection.on('error', error => {
console.log(`Connection Error: ${error.message}`);
});
And playing back with:
//create resource from stream
const resource = createAudioResource(loudspeakerClient.songStream);
//play source
loudspeakerClient.player.play(resource);
//subscribe to player
loudspeakerClient.connection.subscribe(loudspeakerClient.player);
//create resource from stream
const resource = createAudioResource(loudspeakerClient.songStream);
//play source
loudspeakerClient.player.play(resource);
//subscribe to player
loudspeakerClient.connection.subscribe(loudspeakerClient.player);
22 replies
DIAdiscord.js - Imagine an app
Created by CombustibleToast on 4/23/2023 in #djs-questions
[Resolved] Cannot find module '.../node_modules/discord-api-types/v10.js'
4 replies
DIAdiscord.js - Imagine an app
Created by CombustibleToast on 3/27/2023 in #djs-voice
Connecting a specific client to a voice channel
Hello, I'm trying to make a distributed music bot service for a single guild. Long story (https://stackoverflow.com/questions/75860115/is-there-a-way-to-connect-a-specific-discord-js-client-to-a-voice-channel) short, I've logged in several other clients I'd like to have connect to a voice channel when a user uses /play. The main bot that receives the command runs
const channel = interaction.member.voice.channel;
const { joinVoiceChannel } = require('@discordjs/voice');
const connection = joinVoiceChannel({
channelId: channel.id,
guildId: channel.guild.id,
adapterCreator: channel.guild.voiceAdapterCreator
});
const channel = interaction.member.voice.channel;
const { joinVoiceChannel } = require('@discordjs/voice');
const connection = joinVoiceChannel({
channelId: channel.id,
guildId: channel.guild.id,
adapterCreator: channel.guild.voiceAdapterCreator
});
and causes the main bot to join. I don't see anything that specifies which client is connecting, so I'm stumped on what to do. Ideally, I'd do something simple like client.voice.join(channel); but that doesn't seem possible from what I've seen.
7 replies