Voice player sometimes doesn't start playing music

Hi, like the title says, the voice player randomly doesn't start playing music The code responsible for it is the boilerplate from https://discordjs.guide/voice/audio-player.html#playing-audio modified only by a 20sec timeout which just disconnects the bot:
const connection = getVoiceConnection(this.guild.id);
connection?.destroy();
const connection = getVoiceConnection(this.guild.id);
connection?.destroy();
Relevant package versions: "@discordjs/opus": "^0.8.0", "@discordjs/voice": "^0.13.0", "ffmpeg-static": "^5.1.0", "libsodium-wrappers": "^0.7.10", "node-opus": "^0.3.3", "opusscript": "^0.0.8", Any leads would be appreciated and in advance I'm sorry for the incomplete info, i can't run it in debug mode right now
6 Replies
d.js toolkit
d.js toolkit13mo 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!
izaroth
izarothOP13mo ago
i thought about that overlap being the case yeah but it doesn't happen often enough as far as i know it would send a message twice within that 20 seconds for example if that timeout was destroying new connections i think yeah, sorry about that are we on the same page on what that would be supposed to be doing? as in if you called all of it twice within 20 seconds, the first timeout would destroy the second okay look through the bot's messages but i doubt that ever happens is there something else that could be bricking it? there's a few opus libraries installed for example, i'm not sure if they interfere with each other I can share it but there's nothing past the boilerplate and what I sent as far as voice goes one sec
async makeVote(channel, playersToVote, inGame = true, time = 20000, showResults = true, customTitle = '') {
playersToVote = this.shuffleArray(playersToVote);
customTitle = 'x';
let roleAlive = this.#bot.guild.roles.cache.find((x) => x.id === config.roles.alive.id);
const playerVoteListIds = playersToVote.map((player) => player.id);

const pool = new Pool(playerVoteListIds);

const voteEmbedContent = this.getVoteEmbedContent(pool);
const voteSelectMenu = new ActionRowBuilder().addComponents(
new SelectMenuBuilder()
.setCustomId('x')
.setPlaceholder('x')
.addOptions(
playersToVote.map((player) => {
return {
label: `${player.friendlyName}#${player.discordTag}`,
value: String(player.id),
};
})
)
);

const voteEmbed = this.#bot.createEmbed('vote', customTitle, voteEmbedContent);

const voteMessage = await channel.send({
content: roleAlive?.toString() ?? '',
embeds: [voteEmbed],
components: [voteSelectMenu],
});
this.#bot.playVotingMusic(config.channels.rozgrywkaVoice.id);
pool.messageId = voteMessage.id;
this.pools.push(pool);
setTimeout(async () => {
this.pools = this.pools.filter((p) => p.messageId !== pool.messageId);
await voteMessage.edit({ embeds: [this.getVoteEmbedResult(pool)], components: [] });
this.#bot.stopVotingMusic();
}, time);
}
async makeVote(channel, playersToVote, inGame = true, time = 20000, showResults = true, customTitle = '') {
playersToVote = this.shuffleArray(playersToVote);
customTitle = 'x';
let roleAlive = this.#bot.guild.roles.cache.find((x) => x.id === config.roles.alive.id);
const playerVoteListIds = playersToVote.map((player) => player.id);

const pool = new Pool(playerVoteListIds);

const voteEmbedContent = this.getVoteEmbedContent(pool);
const voteSelectMenu = new ActionRowBuilder().addComponents(
new SelectMenuBuilder()
.setCustomId('x')
.setPlaceholder('x')
.addOptions(
playersToVote.map((player) => {
return {
label: `${player.friendlyName}#${player.discordTag}`,
value: String(player.id),
};
})
)
);

const voteEmbed = this.#bot.createEmbed('vote', customTitle, voteEmbedContent);

const voteMessage = await channel.send({
content: roleAlive?.toString() ?? '',
embeds: [voteEmbed],
components: [voteSelectMenu],
});
this.#bot.playVotingMusic(config.channels.rozgrywkaVoice.id);
pool.messageId = voteMessage.id;
this.pools.push(pool);
setTimeout(async () => {
this.pools = this.pools.filter((p) => p.messageId !== pool.messageId);
await voteMessage.edit({ embeds: [this.getVoteEmbedResult(pool)], components: [] });
this.#bot.stopVotingMusic();
}, time);
}
shuffleArray(array) {
for (let i = array.length - 1; i > 0; i--) {
let j = Math.floor(Math.random() * (i + 1));
let temp = array[i];
array[i] = array[j];
array[j] = temp;
}

return array;
}
shuffleArray(array) {
for (let i = array.length - 1; i > 0; i--) {
let j = Math.floor(Math.random() * (i + 1));
let temp = array[i];
array[i] = array[j];
array[j] = temp;
}

return array;
}
playVotingMusic(voiceChannelId : string) {

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

const player = createAudioPlayer({
behaviors: {
noSubscriber: NoSubscriberBehavior.Pause
}
});

const resource = createAudioResource('./audio/elevator.mp3', { inlineVolume: true });
resource.volume?.setVolume(0.1);
player.play(resource);

connection.subscribe(player);
}
playVotingMusic(voiceChannelId : string) {

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

const player = createAudioPlayer({
behaviors: {
noSubscriber: NoSubscriberBehavior.Pause
}
});

const resource = createAudioResource('./audio/elevator.mp3', { inlineVolume: true });
resource.volume?.setVolume(0.1);
player.play(resource);

connection.subscribe(player);
}
stopVotingMusic() {
const connection = getVoiceConnection(this.guild.id);
connection?.destroy();
}
stopVotingMusic() {
const connection = getVoiceConnection(this.guild.id);
connection?.destroy();
}
I just looked through its logs and the shortest period between these calls was like 3 minutes yes it's only a part of one server. there just wasn't much point in having the bot there throughout the game it could just sit there and play/pause each time but does that change anything? oh, and it doesn't leave the channel, just joins and doesn't play anything so that rules out the overlap i'm waiting for log access, would be a bit easier 😐 Nope, that's the bug behavior or in other words it's coded to join, play, and then leave in more or less 20 seconds but it sometimes just joins and doesn't play music
izaroth
izarothOP13mo ago
Doesn't <Connection>.destroy() do exactly that? https://discordjs.guide/voice/voice-connections.html#deletion
discord.js Guide
Imagine a guide... that explores the many possibilities for your discord.js bot.
izaroth
izarothOP13mo ago
from what i understand, disconnect is very similar just lets you rejoin without having to instantiate a new connection there's a new player being created too so it's not just the song ending (the mp3 file is an hour long i think) ppFrown probably didn't want to bother with cutting it i'm a co-dev on it that's true, but if something else is already scheduled there, may as well do you know if any errors would be logged without the debug mode enabled for both? rip i've also been told just now that the bot only does that whenever there's more people playing (more people on the voice channel) there wouldn't be any extra intents, verifications or anything like that needed by default right? oh yeah you can't, we have that why though? wait, how come it's only playing in one voice channel and the initial play is triggered from a web panel which doesn't happen often enough it is the bot's code, just called elsewhere and the overlap is a non issue because of this not quite, the bot is just controlled in some part from that panel i.e. the function i sent doesn't get called anywhere within the bot, it's just exposed it's all 1 process as far as i know, the bot being imported to an express server
duck
duck13mo ago
hopefully unrelated, but I noticed some of your deps, including @discordjs/voice, appear to be outdated even if it doesn't fix this particular issue, I'd recommend updating to avoid some other issues
izaroth
izarothOP13mo ago
yep, will do. thanks okay, a bit more context this time
Caught exception: RangeError: offset is out of bounds
at Uint16Array.set (<anonymous>)
at OpusScript.encode (/home/container/node_modules/opusscript/index.js:67:16)
at Encoder._encode (/home/container/node_modules/prism-media/src/opus/Opus.js:60:25)
at Encoder._transform (/home/container/node_modules/prism-media/src/opus/Opus.js:157:24)
at Encoder.Transform._write (node:internal/streams/transform:175:8)
at writeOrBuffer (node:internal/streams/writable:392:12)
at _write (node:internal/streams/writable:333:10)
at Encoder.Writable.write (node:internal/streams/writable:337:10)
at VolumeTransformer.ondata (node:internal/streams/readable:777:22)
at VolumeTransformer.emit (node:events:517:28)
Caught exception: RangeError: offset is out of bounds
at Uint16Array.set (<anonymous>)
at OpusScript.encode (/home/container/node_modules/opusscript/index.js:67:16)
at Encoder._encode (/home/container/node_modules/prism-media/src/opus/Opus.js:60:25)
at Encoder._transform (/home/container/node_modules/prism-media/src/opus/Opus.js:157:24)
at Encoder.Transform._write (node:internal/streams/transform:175:8)
at writeOrBuffer (node:internal/streams/writable:392:12)
at _write (node:internal/streams/writable:333:10)
at Encoder.Writable.write (node:internal/streams/writable:337:10)
at VolumeTransformer.ondata (node:internal/streams/readable:777:22)
at VolumeTransformer.emit (node:events:517:28)
this is what it logged at least twice
Want results from more Discord servers?
Add your server