Discordjs/voice

First time working with it, I have a cron job running eery 5 minutes. In the callback, the bot fetches an array and loops over it. Each array has a mp3 and a channel id. Now i want to run each mp3 in its own channel. Scenario:Bot runs the join function, a connection is created. It then listens to ready event and create a player to run audio on the connection. Issue: when bot loops on first element, it instead of waiting for connection to be established (ready) and running the audio, just attaches the listener, then goes to next item. For next item, it again runs the join function which overrieds the previous connection according to docs. So how would i make bot wait for connection to be ready, wait for audio to be played, since i cant even await the connection.subscribe ): Code:
for (const num of arr) {

const connection = joinVoiceChannel({
channelId: channel,
guildId: guild.id,
adapterCreator: guild.voiceAdapterCreator,
});

connection.on(VoiceConnectionStatus.Ready, async (f) => {

const player = createAudioPlayer();
player.on("error", (error) => {
console.error(error);
});
const resource = createAudioResource("./test.mp3");
player.play(resource);

// Play "track.mp3" across two voice connections
connection.subscribe(player);


});
for (const num of arr) {

const connection = joinVoiceChannel({
channelId: channel,
guildId: guild.id,
adapterCreator: guild.voiceAdapterCreator,
});

connection.on(VoiceConnectionStatus.Ready, async (f) => {

const player = createAudioPlayer();
player.on("error", (error) => {
console.error(error);
});
const resource = createAudioResource("./test.mp3");
player.play(resource);

// Play "track.mp3" across two voice connections
connection.subscribe(player);


});
2 Replies
d.js toolkit
d.js toolkit8mo 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
NyR
NyR8mo ago
#djs-voice
Want results from more Discord servers?
Add your server