AutoLeave not working

static async #play(client, voiceChannel, guild, trackUrl) {
const player = client.moonlink.createPlayer({
guildId: guild.id,
voiceChannelId: voiceChannel.id,
textChannelId: voiceChannel.id,
volume: 50,
loop: 'track',
autoLeave: true
});

player.connect();

const result = await client.moonlink.search({
query: trackUrl,
})

const track = result.tracks[0];

player.queue.add(track);

if (!player.playing) await player.play().catch(error => {
Logger.error(LogModule.AUDIO, null, error?.message, error?.stack);
});
}
static async #play(client, voiceChannel, guild, trackUrl) {
const player = client.moonlink.createPlayer({
guildId: guild.id,
voiceChannelId: voiceChannel.id,
textChannelId: voiceChannel.id,
volume: 50,
loop: 'track',
autoLeave: true
});

player.connect();

const result = await client.moonlink.search({
query: trackUrl,
})

const track = result.tracks[0];

player.queue.add(track);

if (!player.playing) await player.play().catch(error => {
Logger.error(LogModule.AUDIO, null, error?.message, error?.stack);
});
}
I Create a player like this. But after leaving the channel the bot stays
Solution:
I have now done it this way and would then install the function in our voice state logic
Jump to solution
13 Replies
1Lucas1.apk
1Lucas1.apk•22h ago
Hello, I will check and get back to you shortly with the answer. Thank you very much for reporting the bug.
Mika
MikaOP•22h ago
Does the auto leave refer to the queue or the channel (members)?
1Lucas1.apk
1Lucas1.apk•22h ago
autoLeave it will disconnect when the queue is empty Members will have to do it client Discord
BastiGameツ
BastiGameツ•21h ago
Oh, we need that the bot leaves when there are no more members in the channel Would you add this as a feature or do we have to code it ourselves?
1Lucas1.apk
1Lucas1.apk•21h ago
You use Discord.js's voiceStateUpdate event
BastiGameツ
BastiGameツ•21h ago
yes
1Lucas1.apk
1Lucas1.apk•21h ago
There's no way I can do it within the Lavalink client package, luckily the logic of making the bot leave the call is very easy.
BastiGameツ
BastiGameツ•21h ago
Oh okay, thanks for the help then.
1Lucas1.apk
1Lucas1.apk•21h ago
client.on('voiceStateUpdate', (oldState, newState) => {
const player = client.manager.getPlayer(oldState.guild.id);
if (!player) return;

const channel = oldState.channel || newState.channel;
if (!channel || player.voiceChannel !== channel.id) return;

const humanCount = channel.members.filter(m => !m.user.bot).size;
if (humanCount === 0) player.destroy();
});
client.on('voiceStateUpdate', (oldState, newState) => {
const player = client.manager.getPlayer(oldState.guild.id);
if (!player) return;

const channel = oldState.channel || newState.channel;
if (!channel || player.voiceChannel !== channel.id) return;

const humanCount = channel.members.filter(m => !m.user.bot).size;
if (humanCount === 0) player.destroy();
});
Mika
MikaOP•21h ago
static async leaveAndStop(voiceChannel, client, guild) {
if (voiceChannel.members > 0) return;

const player = client.moonlink.getPlayer(guild.id);
if (!player && !player.connected) return;
player.destroy();
}
static async leaveAndStop(voiceChannel, client, guild) {
if (voiceChannel.members > 0) return;

const player = client.moonlink.getPlayer(guild.id);
if (!player && !player.connected) return;
player.destroy();
}
MEE6
MEE6•21h ago
GG @Mika, you just advanced to level 1!
Solution
Mika
Mika•21h ago
I have now done it this way and would then install the function in our voice state logic
1Lucas1.apk
1Lucas1.apk•21h ago
Nice

Did you find this page helpful?