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);
}
});
2 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! - Marked as resolved by OP
Monke
MonkeOP13mo ago
my discord.js version: [email protected] my node version: v20.1.0 can anyone help? I tried this
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)) {
// If there's a scheduled pause, clear it
if (player.pauseTimeout) {
clearTimeout(player.pauseTimeout);
player.pauseTimeout = null;
}
// Schedule a pause after 3 seconds
player.pauseTimeout = setTimeout(() => {
player.pause();
console.log("!!Pausing!!");
}, 3000); // 3 seconds delay
} else if (members && members.size > 1 && player.state.status === AudioPlayerStatus.Paused) {
// Clear the scheduled pause if someone joins the voice channel
if (player.pauseTimeout) {
clearTimeout(player.pauseTimeout);
player.pauseTimeout = null;
}
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)) {
// If there's a scheduled pause, clear it
if (player.pauseTimeout) {
clearTimeout(player.pauseTimeout);
player.pauseTimeout = null;
}
// Schedule a pause after 3 seconds
player.pauseTimeout = setTimeout(() => {
player.pause();
console.log("!!Pausing!!");
}, 3000); // 3 seconds delay
} else if (members && members.size > 1 && player.state.status === AudioPlayerStatus.Paused) {
// Clear the scheduled pause if someone joins the voice channel
if (player.pauseTimeout) {
clearTimeout(player.pauseTimeout);
player.pauseTimeout = null;
}
player.unpause();
console.log("!!Unpausing!!");
}
}
} catch (err) {
console.error('Error: ', err);
}
});
and it does pause after 3 seconds but if someone joins in that 3 second interval bot still proceeds to pause and then doesn't resume playback until a user leaves and rejoins so do I use connection.joinConfig.channelId for that check? can you help out? I'm out of ideas actually nvm thanks for the advices I did the check with connection.joinConfig.channelId to see if the event is tied to bot's VC and then made flags for player timeouts and used setTimeout/clearTimeout logic it's working now
Want results from more Discord servers?
Add your server