Voice State Management
Learn how to handle voice state updates and manage voice connections in Yukino.
Understanding Voice State Flow
// Voice state flow:
// 1. Discord.js emits voice state/server updates
// 2. Connector receives and processes updates
// 3. Node handles voice connection
// 4. Player manages voice state
Core Voice Events
// Basic voice state handling
yukino.node.on('voiceStateUpdate', (state) => {
console.log('Voice state:', {
guildId: state.guild_id,
channelId: state.channel_id
});
});
// Handle WebSocket closure
yukino.node.on('wsClosed', (player, code, reason, byRemote) => {
console.log(`WebSocket closed for guild ${player.guildId}`, {
code, reason, byRemote
});
if (code === 4006) {
// Session invalid - attempt reconnect
setTimeout(() => {
if (player && player.voiceChannelId) {
player.connect().catch(console.error);
}
}, 2000);
}
});
Discord.js Integration
Handle bot movements and disconnections:
client.on("voiceStateUpdate", async (oldState, newState) => {
// Only handle bot events
if (oldState.member?.user.id !== client.user?.id) return;
const player = client.yukino.getPlayer(oldState.guild.id);
if (!player) return;
// Bot disconnected
if (oldState.channelId && !newState.channelId) {
if (player.playing) {
await player.connect().catch(() => player.destroy());
} else {
await player.destroy();
}
}
// Bot moved
else if (oldState.channelId !== newState.channelId) {
player.voiceChannelId = newState.channelId;
}
});
Auto-Management
Example auto-disconnect implementation when channel is empty:
class VoiceManager {
private timeouts = new Map<string, NodeJS.Timeout>();
constructor(private yukino: YukinoClient) {
this.setupEvents();
}
private setupEvents() {
yukino.connector.client.on('voiceStateUpdate', (oldState, newState) => {
const player = yukino.getPlayer(oldState.guild.id);
if (!player) return;
const channel = yukino.connector.client.channels.cache.get(
player.voiceChannelId
);
if (!channel) return;
// Check if channel is empty (excluding bot)
const members = channel.members.size - 1;
if (members === 0) {
// Disconnect after 5 minutes if empty
this.timeouts.set(player.guildId, setTimeout(() => {
player.destroy();
}, 5 * 60 * 1000));
} else {
// Clear timeout if someone joins
const timeout = this.timeouts.get(player.guildId);
if (timeout) {
clearTimeout(timeout);
this.timeouts.delete(player.guildId);
}
}
});
}
destroy() {
this.timeouts.forEach(clearTimeout);
this.timeouts.clear();
}
}
Best Practices
- Enable auto-reconnect when creating players:
const player = yukino.createPlayer({
guildId: 'guild-id',
voiceChannelId: 'channel-id',
autoReconnect: true // Handles disconnections automatically
});
- Implement graceful cleanup:
async function disconnect(player: Player) {
await player.stop();
await player.destroy();
}
- Monitor connection state using the debug event:
yukino.node.on('debug', (message) => {
if (message.includes('voice')) {
console.debug('[Voice]', message);
}
});