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

0% Statements 0/23
0% Branches 0/8
0% Functions 0/6
0% Lines 0/22

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                                                                                                                                                                                 
import { Config } from "@/modules/shared/config/types";
import { Extends } from "@/types/Constants";
import { LOCALIZATION_ADAPTER, NestedLocalizationAdapter } from "@necord/localization";
import { Inject, Injectable, Logger } from "@nestjs/common";
import { ConfigService } from "@nestjs/config";
import { OnEvent } from "@nestjs/event-emitter";
import { Client, EmbedBuilder, TextChannel, VoiceChannel } from "discord.js";
import { Player, Track, TrackEndEvent, TrackExceptionEvent, TrackStuckEvent } from "lavalink-client";
import ms from "parse-ms";
import { MessageTools } from "../../commands/Message";
 
@Injectable()
export class QueueEvents {
	public constructor(
		@Inject(LOCALIZATION_ADAPTER) private readonly translate: NestedLocalizationAdapter,
		private readonly client: Client,
		private readonly config: ConfigService<Config>,
	) {}
 
	private readonly logger = new Logger(QueueEvents.name);
 
	@OnEvent("queue.end")
	public async onQueueEnd(
		player: Player,
		track: Track,
		payload: TrackEndEvent | TrackStuckEvent | TrackExceptionEvent,
	) {
		const textChannel = this.client.channels.cache.get(player.textChannelId) as TextChannel;
		const voiceChannel = this.client.channels.cache.get(player.voiceChannelId) as VoiceChannel;
		if (this.config.getOrThrow<Config["Music"]>("Music").Player.AutoLeaveEmpty.Queue.Enable) {
			setTimeout(async () => {
				try {
					if (!player.queue && player.queue.current) {
						const Timer = ms(this.config.getOrThrow<Config["Music"]>("Music").Player.AutoLeaveEmpty.Queue.Delay);
						const embed = new EmbedBuilder()
							.setAuthor({
								name: this.client.user.tag,
								url: this.client.user.displayAvatarURL(),
							})
							.setColor("#00c26f")
							.setTitle(
								this.translate.getTranslation(
									"Events.PlayerEvents.playerMove.queueEnd.Title",
									textChannel.guild.preferredLocale,
								),
							)
							.setDescription(
								this.translate.getTranslation(
									"Events.PlayerEvents.playerMove.queueEnd.Description",
									textChannel.guild.preferredLocale,
									{
										CHANNEL: voiceChannel.name,
										Timer: String(Timer),
									},
								),
							)
							.setFooter({
								text: this.translate.getTranslation(
									"Events.PlayerEvents.playerMove.queueEnd.Footer",
									textChannel.guild.preferredLocale,
									{
										TIMER: String(Timer),
									},
								),
							})
							.setTimestamp();
						const Message = await MessageTools.send(textChannel, {
							embeds: [embed],
						});
 
						textChannel.messages.fetch(Message.id).then((msg) => {
							if (msg?.deletable) {
								setTimeout(async () => {
									msg.delete().catch((error: Error) => {
										this.logger.warn('Não consegui deletar o "Player_MESSAGE"');
									});
								}, 4000);
							}
						});
						player.destroy();
					}
				} catch (error) {
					this.logger.error("Queue End Error: ", String(error));
				}
			}, this.config.getOrThrow<Config["Music"]>("Music").Player.AutoLeaveEmpty.Queue.Delay);
		}
	}
}