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

0% Statements 0/26
0% Branches 0/4
0% Functions 0/7
0% Lines 0/25

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                                                                                                                                                     
import { Timer, WAIT } from "@/utils/Tools";
import { LOCALIZATION_ADAPTER, NestedLocalizationAdapter } from "@necord/localization";
import { Inject, Injectable, Logger } from "@nestjs/common";
import { OnEvent } from "@nestjs/event-emitter";
import { Client, GuildChannel, GuildMember, Message, TextChannel } from "discord.js";
import { Player, Track, TrackEndEvent, TrackExceptionEvent, TrackStartEvent } from "lavalink-client";
import { Music } from "../";
import { MessageTools } from "../../commands/Message";
import type { IMusicEmbeds, IMusicService } from "../interfaces";
 
@Injectable()
export class TrackEvents {
	public constructor(
		@Inject(LOCALIZATION_ADAPTER) private readonly translate: NestedLocalizationAdapter,
		@Inject(Music.Embeds) private readonly embeds: IMusicEmbeds,
		@Inject(Music.Service) private readonly service: IMusicService,
		private readonly client: Client,
	) {}
 
	private readonly logger = new Logger(TrackEvents.name);
 
	@OnEvent("track.start")
	public async onTrackStart(player: Player, track: Track, payload: TrackStartEvent): Promise<void> {
		const textChannel = this.client.channels.cache.get(player.textChannelId) as TextChannel;
 
		const Requester = (
			(await (await this.client.guilds.fetch(player.guildId)).members.fetch(track.requester as string)) as GuildMember
		).user;
 
		await WAIT(500);
 
		MessageTools.send(textChannel, {
			embeds: [
				await this.embeds.TrackStart(
					textChannel,
					track,
					Requester,
					await Timer(this.translate, "normal", track.info.duration, textChannel.guild.preferredLocale),
					await this.service.URLChecker(false, track.info.uri),
					this.service.formatSourceName(track.info.sourceName),
				),
			],
		}).then((msg) => {
			try {
				const CURRENT_SONG_MSG = textChannel.messages.cache.get(player.songMessage) as Message;
				if (CURRENT_SONG_MSG && msg.id !== CURRENT_SONG_MSG.id) {
					CURRENT_SONG_MSG.delete().catch((error: Error) => {
						this.logger.warn('Não consegui deletar a "CURRENT_SONG_MSG"', error);
					});
				}
			} catch {}
			player.songMessage = msg.id;
		});
	}
 
	@OnEvent("track.end")
	public async onTrackEnd(player: Player, track: Track, payload: TrackEndEvent) {
		player.lastSong = track;
	}
 
	@OnEvent("track.stuck")
	public async onTrackStuck(player: Player, track: Track, payload: TrackStartEvent) {
		const textChannel = this.client.channels.cache.get(player.textChannelId) as TextChannel;
		MessageTools.send(textChannel, { embeds: [await this.embeds.TrackStuck(textChannel, track)] });
	}
 
	@OnEvent("track.error")
	public async onTrackError(player: Player, track: Track, payload: TrackExceptionEvent) {
		const textChannel = this.client.channels.cache.get(player.textChannelId) as TextChannel;
		MessageTools.send(textChannel, {
			embeds: [await this.embeds.TrackError(textChannel, track, payload.exception.message)],
		});
	}
}