Skip to content

Commit

Permalink
fixed Not getting a voice on the stage channel appujet#738
Browse files Browse the repository at this point in the history
  • Loading branch information
appujet committed Oct 6, 2024
1 parent 639b020 commit cfba720
Showing 1 changed file with 95 additions and 40 deletions.
135 changes: 95 additions & 40 deletions src/events/client/VoiceStateUpdate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ export default class VoiceStateUpdate extends Event {
});
}

public async run(_oldState: VoiceState, newState: VoiceState): Promise<any> {
public async run(oldState: VoiceState, newState: VoiceState): Promise<any> {
const guildId = newState.guild.id;
if (!guildId) return;

Expand All @@ -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);
}
}
}
},
};
}

/**
Expand Down

0 comments on commit cfba720

Please sign in to comment.