-
-
Notifications
You must be signed in to change notification settings - Fork 94
Handling Discord.js Events
github-actions[bot] edited this page Dec 22, 2025
·
4 revisions
DisTube provides utilities to help you handle Discord.js events for better user experience.
import { isVoiceChannelEmpty } from "distube";
client.on("voiceStateUpdate", oldState => {
if (!oldState?.channel) return;
const voice = distube.voices.get(oldState);
if (voice && isVoiceChannelEmpty(oldState)) {
voice.leave();
}
});import { isVoiceChannelEmpty } from "distube";
client.on("voiceStateUpdate", oldState => {
if (!oldState?.channel) return;
const queue = distube.getQueue(oldState);
if (!queue) return;
if (isVoiceChannelEmpty(oldState)) {
queue.pause();
} else if (queue.paused) {
queue.resume();
}
});import { Events } from "distube";
const inactivityTimers = new Map();
// Start timer when queue finishes
distube.on(Events.FINISH, queue => {
const timer = setTimeout(() => {
queue.voice.leave();
inactivityTimers.delete(queue.id);
}, 5 * 60 * 1000); // 5 minutes
inactivityTimers.set(queue.id, timer);
});
// Clear timer when new song plays
distube.on(Events.PLAY_SONG, queue => {
const timer = inactivityTimers.get(queue.id);
if (timer) {
clearTimeout(timer);
inactivityTimers.delete(queue.id);
}
});
// Clear timer when queue is deleted
distube.on(Events.DELETE_QUEUE, queue => {
const timer = inactivityTimers.get(queue.id);
if (timer) {
clearTimeout(timer);
inactivityTimers.delete(queue.id);
}
});client.on("voiceStateUpdate", (oldState, newState) => {
// Check if it's the bot
if (oldState.member?.id !== client.user?.id) return;
// Bot was moved to a different channel
if (oldState.channelId && newState.channelId && oldState.channelId !== newState.channelId) {
const queue = distube.getQueue(oldState.guild.id);
if (queue) {
queue.textChannel?.send(`Moved to <#${newState.channelId}>`);
}
}
});Home - Installation - [Guide](DisTube Guide) - API Documentation - Discord Support