Reputation:
I am currently struggling with a problem of my discord.js bot that plays a music stream via an url. Everything works fine, but after about 12h all players disconnect and I get the following error message: [VOICE_CONNECTION_TIMEOUT]: Connection not established within 15 seconds.
This is my code to join/leave a channel:
function join(voiceChannel, volume = 20) {
return new Promise((resolve, reject) => {
voiceChannel.join().then(vc => {
vc.voice.setSelfDeaf(true);
const dispatcher = vc.play(streamURL);
dispatcher.once('start', () => {
dispatcher.setBitrate(64);
dispatcher.setVolumeLogarithmic(volume / 100);
setTimeout(() => {
if (voiceChannel.members.filter(m => !m.user.bot).size === 0) dispatcher.pause(true);
}, 1500);
resolve();
});
// Tried to reconnect on error but does not seem to work
dispatcher.on('error', () => {
leave(voiceChannel.guild);
setTimeout(() => join(voiceChannel, volume), 5000);
});
}).catch(err => reject(err));
});
}
function leave(guild) {
guild.me?.voice?.connection?.disconnect();
}
I also have this event set up to pause the stream if nobody is listening:
client.on('voiceStateUpdate', (oldState, newState) => {
const state = oldState || newState;
if (state?.member.user.bot) return;
const guild = state?.guild;
if (!guild?.me?.voice?.connection) return;
if (newState.channel === oldState.channel) return;
Guild.findOne({guildId: guild.id}, (err, dbGuild) => {
if (err || !dbGuild) return;
const channel = guild.channels.cache.get(dbGuild.musicChannelId);
if (!channel || !guild.me.voice.connection.dispatcher) return;
const dispatcher = guild.me.voice.connection.dispatcher;
if (channel.members.filter(m => !m.user.bot).size > 0 && dispatcher.paused) {
dispatcher.resume();
return;
}
if (!dispatcher.paused && channel.members.filter(m => !m.user.bot).size === 0) {
dispatcher.pause(true);
}
});
});
Maybe somebody can help me with this. I don't know how to fix this error.
Upvotes: 0
Views: 5624
Reputation: 711
This error is familiar in the Discord.JS package. The problem is that Discord doesn't fully support voice connections for bots, so this has nothing to do with your code. Discord.JS recommends you tho to use their voice package for playing audio in a voice channel. Here are their documentations and here are some of their examples.
Upvotes: 1