Monke
Monke
DIAdiscord.js - Imagine an app
Created by Monke on 10/31/2023 in #djs-voice
Player Auto-Pause
I have this music bot and I want it to pause the playback if it's alone in the voice channel. I've succeeded in doing that, but rn it pauses instantly after detecting that it's alone and I want it to wait for 3 seconds before doing that, so that if anybody joins during that time interval the bot doesn't need to pause. here's the code currently:
client.on('voiceStateUpdate', (oldState, newState) => {
const guildId = newState.guild.id;

const connection = getVoiceConnection(guildId);
const player = audioPlayers.get(guildId);

try {
if (player && connection) {
const members = newState.channel?.members;

if (!members || (members.size === 1 && player.state.status === AudioPlayerStatus.Playing)) {
// Pause audio when there are no members in the voice channel
player.pause();
console.log("!!Pausing!!");
} else if (members && members.size > 1 && player.state.status === AudioPlayerStatus.Paused) {
// Resume audio when someone joins the voice channel
player.unpause();
console.log("!!Unpausing!!");
}
}
} catch (err) {
console.error('Error: ', err);
}
});
client.on('voiceStateUpdate', (oldState, newState) => {
const guildId = newState.guild.id;

const connection = getVoiceConnection(guildId);
const player = audioPlayers.get(guildId);

try {
if (player && connection) {
const members = newState.channel?.members;

if (!members || (members.size === 1 && player.state.status === AudioPlayerStatus.Playing)) {
// Pause audio when there are no members in the voice channel
player.pause();
console.log("!!Pausing!!");
} else if (members && members.size > 1 && player.state.status === AudioPlayerStatus.Paused) {
// Resume audio when someone joins the voice channel
player.unpause();
console.log("!!Unpausing!!");
}
}
} catch (err) {
console.error('Error: ', err);
}
});
11 replies