wait for bot to play audio

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);


});
4 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!
duck
duck8mo ago
you can use entersState for both waiting for the connection to be Ready and waiting for the player to be Idle
d.js docs
d.js docs8mo ago
:guide: Library: Life cycles read more
Ahsanfr.
Ahsanfr.OP8mo ago
i see, but the docs say the player goes to idle on error as well, how can i check if the bot has played the audio then went into idle state? can ya look at the code and see if it's fine. Also the error handling? of stpping the player and destroying connecion.
for (const num of arr) {
const connection = joinVoiceChannel({
channelId: channel,
guildId: guild.id,
adapterCreator: guild.voiceAdapterCreator,
});

await playAudio(connection);
}
});

async function playAudio(connection) {
const player = createAudioPlayer();

connection.on(VoiceConnectionStatus.Disconnected, async () => {
try {
await Promise.race([
entersState(connection, VoiceConnectionStatus.Signalling, 5_000),
entersState(connection, VoiceConnectionStatus.Connecting, 5_000),
]);
} catch (error) {
player.stop();
return connection.destroy();
}
});

player.on("error", (error) => {
player.stop();
return connection.destroy();
});

const resource = createAudioResource("./te");
player.play(resource);
connection.subscribe(player);

await new Promise((res, re) => {
player.on(AudioPlayerStatus.Idle, async () => {
console.log('PLAYED THE AUDIO')

player.stop();
connection.destroy();
res();
});
});
}
for (const num of arr) {
const connection = joinVoiceChannel({
channelId: channel,
guildId: guild.id,
adapterCreator: guild.voiceAdapterCreator,
});

await playAudio(connection);
}
});

async function playAudio(connection) {
const player = createAudioPlayer();

connection.on(VoiceConnectionStatus.Disconnected, async () => {
try {
await Promise.race([
entersState(connection, VoiceConnectionStatus.Signalling, 5_000),
entersState(connection, VoiceConnectionStatus.Connecting, 5_000),
]);
} catch (error) {
player.stop();
return connection.destroy();
}
});

player.on("error", (error) => {
player.stop();
return connection.destroy();
});

const resource = createAudioResource("./te");
player.play(resource);
connection.subscribe(player);

await new Promise((res, re) => {
player.on(AudioPlayerStatus.Idle, async () => {
console.log('PLAYED THE AUDIO')

player.stop();
connection.destroy();
res();
});
});
}
i am really unsure about those destruction of connection
Want results from more Discord servers?
Add your server