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

0% Statements 0/51
0% Branches 0/10
0% Functions 0/14
0% Lines 0/48

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                                                                                                                                                                                                       
import { Config } from "@/modules/shared/config/types";
import { Inject, Injectable, Logger } from "@nestjs/common";
import { ConfigService } from "@nestjs/config";
import { OnEvent } from "@nestjs/event-emitter";
import { Client, Guild, TextChannel, VoiceChannel } from "discord.js";
import { Player } from "lavalink-client";
import { Music } from "../";
import { MessageTools } from "../../commands/Message";
import type { IMusicEmbeds } from "../interfaces";
 
@Injectable()
export class PlayerEvents {
	public constructor(
		@Inject(Music.Embeds) private readonly embeds: IMusicEmbeds,
		private readonly config: ConfigService<Config>,
		private readonly client: Client,
	) {}
 
	private readonly logger = new Logger(PlayerEvents.name);
 
	@OnEvent("player.create")
	public async onPlayerCreate(player: Player): Promise<void> {
		const guild = (await this.client.guilds.fetch(player.guildId)) as Guild;
		this.logger.log(`Player iniciando no servidor: ${guild.name}(${guild.id})`);
		const textChannel = this.client.channels.cache.get(player.textChannelId) as TextChannel;
		const voiceChannel = this.client.channels.cache.get(player.voiceChannelId) as VoiceChannel;
 
		player.playerMessage = (
			await MessageTools.send(textChannel, {
				embeds: [await this.embeds.PlayerCreate(guild, textChannel, voiceChannel, player)],
			})
		).id;
 
		textChannel.messages.fetch(player.playerMessage).then((msg) => {
			if (msg?.deletable) {
				setTimeout(async () => {
					msg.delete().catch((error: Error) => {
						this.logger.warn('Não consegui deletar o "Player_MESSAGE"', error);
					});
				}, 5 * 1000);
			}
		});
 
		if (this.config.getOrThrow<Config["Music"]>("Music").Client.serverDeaf) {
			for (let i = 0; i <= 5; i++) {
				await new Promise((resolve) => {
					setTimeout(() => {
						resolve(2);
						guild.members.me.voice.setDeaf(true);
						i = 10;
					}, 1000);
				});
			}
		}
	}
 
	@OnEvent("player.move")
	public async onPlayerMove(player: Player, oldChannel: string, newChannel: string): Promise<void> {
		if (!newChannel) {
			const textChannel = this.client.channels.cache.get(player.textChannelId) as TextChannel;
			const voiceChannel = this.client.channels.cache.get(player.voiceChannelId) as VoiceChannel;
			MessageTools.send(textChannel, { embeds: [await this.embeds.PlayerMove(textChannel, voiceChannel)] });
			try {
				textChannel.messages.fetch(player.get("MESSAGE")).then((msg) => {
					if (msg?.deletable) {
						setTimeout(() => {
							msg.delete();
						}, 2000);
					}
				});
			} catch (error) {
				this.logger.error(String(error));
			}
			player.destroy();
		} else {
			player.voiceChannelId = newChannel;
			if (player.paused) return;
			setTimeout(() => {
				player.pause();
				setTimeout(() => {
					player.resume();
				}, this.client.ws.ping * 2);
			}, this.client.ws.ping * 2);
		}
	}
 
	@OnEvent("player.disconnect")
	public async onPlayerDisconnect(player: Player, oldChannel: string): Promise<void> {
		const guild = this.client.guilds.cache.get(player.guildId) as Guild;
		this.logger.log(`Player desconectado no servidor: ${guild.name}(${guild.id})\n canal: ${oldChannel}`);
		player.destroy();
	}
 
	@OnEvent("player.destroy")
	public async onPlayerDestroy(player: Player, destroyReason: string): Promise<void> {
		const guild = this.client.guilds.cache.get(player.guildId) as Guild;
		this.logger.log(`Player destruído no servidor: ${guild.name}(${guild.id}) | Motivo: ${destroyReason}`);
	}
}