All files / src/modules/bot/music/events Voice.ts

0% Statements 0/48
0% Branches 0/81
0% Functions 0/8
0% Lines 0/45

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139                                                                                                                                                                                                                                                                                     
import { Config } from "@/modules/shared/config/types";
import type { IDatabaseService } from "@/modules/shared/database/interfaces/IDatabaseService";
import { Services } from "@/types/Constants";
import { LOCALIZATION_ADAPTER, NestedLocalizationAdapter } from "@necord/localization";
import { Inject, Logger } from "@nestjs/common";
import { ConfigService } from "@nestjs/config";
import { ChannelType, Client, EmbedBuilder, TextChannel, VoiceChannel } from "discord.js";
import { Context, ContextOf, On } from "necord";
import { Music } from "..";
import type { IMusicService } from "../interfaces";
 
export class VoiceEvents {
	public constructor(
		@Inject(Services.Database) private readonly database: IDatabaseService,
		@Inject(Music.Service) private readonly MusicService: IMusicService,
		@Inject(LOCALIZATION_ADAPTER) private readonly translate: NestedLocalizationAdapter,
		private readonly client: Client,
		private readonly config: ConfigService<Config>,
	) {}
	private readonly logger = new Logger(VoiceEvents.name);
 
	@On("voiceStateUpdate")
	async onVoiceStateUpdate(@Context() [oldState, newState]: ContextOf<"voiceStateUpdate">) {
		const guildData = await this.database.GuildRepo().get(oldState.guild.id);
		if (!guildData) return;
		const { Premium } = guildData.Settings!;
		const player = await this.MusicService.getPlayerEvent(newState.guild.id, Premium);
 
		// Auto Leave Client from Channels if is EMPTY or Everyone is MUTED
		if (oldState && (!newState.channelId || newState.channelId)) {
			if (player && oldState.channelId === player.voiceChannelId) {
				if (
					!(
						(!oldState.streaming && newState.streaming) ||
						(oldState.streaming && !newState.streaming) ||
						(!oldState.serverMute && newState.serverMute && !newState.serverDeaf && !newState.selfDeaf) ||
						(oldState.serverMute && !newState.serverMute && !newState.serverDeaf && !newState.selfDeaf) ||
						(!oldState.selfMute && newState.selfMute && !newState.serverDeaf && !newState.selfDeaf) ||
						(oldState.selfMute && !newState.selfMute && !newState.serverDeaf && !newState.selfDeaf) ||
						(!oldState.selfVideo && newState.selfVideo) ||
						(oldState.selfVideo && !newState.selfVideo)
					)
				) {
					if (
						this.config.getOrThrow<Config["Music"]>("Music").Player.AutoLeaveEmpty.Channel.Enable &&
						player &&
						(!oldState.channel.members ||
							oldState.channel.members.size === 0 ||
							oldState.channel.members.filter((mem) => !mem.user.bot && !mem.voice.deaf && !mem.voice.selfDeaf).size <
								1)
					) {
						setTimeout(async () => {
							try {
								let voiceChannel: VoiceChannel;
								voiceChannel = newState.guild.channels.cache.get(player.voiceChannelId) as VoiceChannel;
								if (voiceChannel) {
									voiceChannel = (await voiceChannel.fetch()) as VoiceChannel;
								}
								if (!voiceChannel) {
									voiceChannel = (await newState.guild.channels.fetch(player.voiceChannelId).catch((error) => {
										this.logger.error(error);
									})) as VoiceChannel;
								}
								if (!voiceChannel) return player.destroy();
								if (
									!voiceChannel.members ||
									voiceChannel.members.size === 0 ||
									voiceChannel.members.filter(
										(member) => !member.user.bot && !member.voice.deaf && !member.voice.selfDeaf,
									).size < 1
								) {
									player.destroy("Auto Leave Client from Channels if is EMPTY or Everyone is MUTED");
								}
							} catch (error) {
								this.logger.error(String(error));
							}
						}, this.config.getOrThrow<Config["Music"]>("Music").Player.AutoLeaveEmpty.Channel.Delay || 60000);
					}
				}
			}
		}
	}
 
	@On("voiceChannelJoin")
	public async onVoiceChannelJoin(@Context() [member, channel]: ContextOf<"voiceChannelJoin">) {
		if (member.id === this.client.user.id) {
			// Auto set Client as Speaker in Stage Channels
			if (channel?.type === ChannelType.GuildStageVoice && channel.guild.members.me.voice.suppress) {
				if (
					channel.guild.members.me.permissions.has("Speak") ||
					channel.permissionsFor(channel.guild.members.me).has("Speak")
				) {
					channel.guild.members.me.voice.setSuppressed(false);
				}
			}
 
			// Mute Client when join
			if (!channel?.guild.members.me.voice.deaf) {
				if (
					channel.guild.members.me.permissions.has("DeafenMembers") ||
					channel.permissionsFor(channel.guild.members.me).has("DeafenMembers")
				) {
					member.voice.setDeaf(true);
				}
			}
		}
	}
 
	@On("voiceChannelUndeaf")
	public async onVoiceChannelUndeaf(@Context() [member, type]: ContextOf<"voiceChannelUndeaf">) {
		const { Premium } = (await this.database.GuildRepo().get(member.guild.id)).Settings;
 
		const player = await this.MusicService.getPlayerEvent(member.guild.id, Premium);
		// Anti Unmute Client
		if (member.id === this.client.user.id) {
			member.voice.setDeaf(true);
			const textChannel = member.guild.channels.cache.get(player.textChannelId) as TextChannel;
 
			textChannel.send({
				embeds: [
					new EmbedBuilder()
						.setColor("#c20e00")
						.setAuthor({
							name: this.client.user.tag,
							iconURL: this.client.user.displayAvatarURL(),
						})
						.setDescription(
							this.translate.getTranslation("Events.VoiceStateUpdate.UNMute", member.guild.preferredLocale),
						)
						.setTimestamp()
						.setFooter({
							text: this.translate.getTranslation("Events.VoiceStateUpdate.Embed.Footer", member.guild.preferredLocale),
						}),
				],
			});
		}
	}
}