diff --git a/src/events/client/VoiceStateUpdate.ts b/src/events/client/VoiceStateUpdate.ts index c2f56fdae..3413ee977 100644 --- a/src/events/client/VoiceStateUpdate.ts +++ b/src/events/client/VoiceStateUpdate.ts @@ -8,7 +8,7 @@ export default class VoiceStateUpdate extends Event { }); } - public async run(_oldState: VoiceState, newState: VoiceState): Promise { + public async run(oldState: VoiceState, newState: VoiceState): Promise { const guildId = newState.guild.id; if (!guildId) return; @@ -26,55 +26,110 @@ export default class VoiceStateUpdate extends Event { return player.destroy(); } - if ( - newState.id === this.client.user?.id && - newState.channelId && - newState.channel?.type === ChannelType.GuildStageVoice && - newState.guild.members.me?.voice.suppress - ) { + let type: 'join' | 'leave' | 'move' | null = null; + + if (!oldState.channelId && newState.channelId) { + type = 'join'; + } else if (oldState.channelId && !newState.channelId) { + type = 'leave'; + } else if (oldState.channelId && newState.channelId && oldState.channelId !== newState.channelId) { + type = 'move'; + } + + if (type === 'join') { + this.handale.join(newState, this.client); + } else if (type === 'leave') { + this.handale.leave(newState, this.client); + } else if (type === 'move') { + this.handale.move(newState, this.client); + } + } + + handale = { + async join(newState: VoiceState, client: Lavamusic) { + await new Promise((resolve) => setTimeout(resolve, 3000)); + const bot = newState.guild.voiceStates.cache.get(client.user!.id); + if (!bot) return; + if ( - newState.guild.members.me.permissions.has(['Connect', 'Speak']) || - newState.channel.permissionsFor(newState.guild.members.me).has('MuteMembers') + bot.id === client.user?.id && + bot.channelId && + bot.channel?.type === ChannelType.GuildStageVoice && + bot.suppress ) { - await newState.guild.members.me.voice.setSuppressed(false).catch(() => { - null; - }); + if (bot.channel && bot.member && bot.channel.permissionsFor(bot.member!).has('MuteMembers')) { + await bot.setSuppressed(false) + } } - } - if (newState.id === this.client.user?.id && !newState.serverDeaf) { - const permissions = vc.permissionsFor(newState.guild.members.me!); - if (permissions?.has('DeafenMembers')) { - await newState.setDeaf(true); + const player = client.manager.getPlayer(newState.guild.id); + if (!player) return; + + if (!player?.voiceChannelId) return; + + const vc = newState.guild.channels.cache.get(player.voiceChannelId); + if (!(vc && vc.members instanceof Map)) return; + if (newState.id === client.user?.id && !newState.serverDeaf) { + const permissions = vc.permissionsFor(newState.guild.members.me!); + if (permissions?.has('DeafenMembers')) { + await newState.setDeaf(true); + } } - } - if (newState.id === this.client.user?.id) { - if (newState.serverMute && !player.paused) { - player.pause(); - } else if (!newState.serverMute && player.paused) { - player.resume(); + if (newState.id === client.user?.id) { + if (newState.serverMute && !player.paused) { + player.pause(); + } else if (!newState.serverMute && player.paused) { + player.resume(); + } } - } + }, + + async leave(newState: VoiceState, client: Lavamusic) { + const player = client.manager.getPlayer(newState.guild.id); + if (!player) return; + if (!player?.voiceChannelId) return; + const is247 = await client.db.get_247(newState.guild.id); + const vc = newState.guild.channels.cache.get(player.voiceChannelId); + if (!(vc && vc.members instanceof Map)) return; - if (vc.members instanceof Map && [...vc.members.values()].filter((x: GuildMember) => !x.user.bot).length <= 0) { - setTimeout(async () => { - if (!player?.voiceChannelId) return; - - const playerVoiceChannel = newState.guild.channels.cache.get(player?.voiceChannelId); - if ( - player && - playerVoiceChannel && - playerVoiceChannel.members instanceof Map && - [...playerVoiceChannel.members.values()].filter((x: GuildMember) => !x.user.bot).length <= 0 - ) { - if (!is247) { - player.destroy(); + if (vc.members instanceof Map && [...vc.members.values()].filter((x: GuildMember) => !x.user.bot).length <= 0) { + setTimeout(async () => { + if (!player?.voiceChannelId) return; + + const playerVoiceChannel = newState.guild.channels.cache.get(player?.voiceChannelId); + if ( + player && + playerVoiceChannel && + playerVoiceChannel.members instanceof Map && + [...playerVoiceChannel.members.values()].filter((x: GuildMember) => !x.user.bot).length <= 0 + ) { + if (!is247) { + player.destroy(); + } } + }, 5000); + } + }, + + async move(newState: VoiceState, client: Lavamusic) { + // delay for 3 seconds + await new Promise((resolve) => setTimeout(resolve, 3000)); + const bot = newState.guild.voiceStates.cache.get(client.user!.id); + if (!bot) return; + + if ( + bot.id === client.user?.id && + bot.channelId && + bot.channel?.type === ChannelType.GuildStageVoice && + bot.suppress + ) { + if (bot.channel && bot.member && bot.channel.permissionsFor(bot.member!).has('MuteMembers')) { + await bot.setSuppressed(false) } - }, 5000); - } - } + } + }, + }; } /**