Files
Yumi/src/discord/MusicPlayer/NowPlaying.ts
T

69 lines
2.8 KiB
TypeScript
Raw Normal View History

2022-06-07 03:08:27 +07:00
import { Message, CommandInteraction, MessageActionRow, MessageButton } from "discord.js";
import { I18n } from "i18n";
2022-03-03 20:56:49 +07:00
2022-02-05 00:32:02 +07:00
import DiscordProvider from "../../providers/Discord";
2022-02-28 11:50:47 +07:00
import DiscordMusicPlayer, { ValidTracks } from "../../providers/DiscordMusicPlayer";
2022-06-07 03:08:27 +07:00
import Locale from "../../services/Locale";
import { makeErrorEmbed, makeInfoEmbed, sendHybridInteractionMessageResponse } from "../../utils/DiscordMessage";
import DiscordModule, { HybridInteractionMessage } from "../../utils/DiscordModule";
2022-02-05 00:32:02 +07:00
const EMBEDS = {
2022-06-07 03:08:27 +07:00
NOW_PLAYING: (data: HybridInteractionMessage, locale: I18n, track: ValidTracks) => {
2022-02-05 00:32:02 +07:00
const embed = makeInfoEmbed({
2022-06-07 03:08:27 +07:00
title: locale.__('musicplayer.now_playing'),
icon: '🎵 ',
description: track.title,
2022-02-05 00:32:02 +07:00
user: DiscordProvider.client.user
});
2022-03-04 00:29:50 +07:00
const highestResolutionThumbnail = track.thumbnails.reduce((prev, current) => (prev.height * prev.width > current.height * current.width) ? prev : current)
if(highestResolutionThumbnail)
embed.setImage(highestResolutionThumbnail.url);
2022-02-05 00:32:02 +07:00
return embed;
},
2022-06-07 03:08:27 +07:00
NO_MUSIC_PLAYING: (data: HybridInteractionMessage, locale: I18n) => {
2022-02-05 00:32:02 +07:00
return makeErrorEmbed({
2022-06-07 03:08:27 +07:00
title: locale.__('musicplayer.no_music_playing'),
user: data.getUser()
2022-02-05 00:32:02 +07:00
});
}
}
2022-03-03 20:56:49 +07:00
export default class NowPlaying extends DiscordModule {
public id = "Discord_MusicPlayer_NowPlaying";
public commands = ["nowplaying", "np"];
public commandInteractionName = "nowplaying";
2022-02-05 00:32:02 +07:00
2022-03-03 20:56:49 +07:00
async GuildOnModuleCommand(args: any, message: Message) {
await this.run(new HybridInteractionMessage(message), args);
2022-02-05 00:32:02 +07:00
}
2022-03-03 20:56:49 +07:00
async GuildModuleCommandInteractionCreate(interaction: CommandInteraction) {
await this.run(new HybridInteractionMessage(interaction), interaction.options);
2022-02-05 00:32:02 +07:00
}
2022-03-03 20:56:49 +07:00
async run(data: HybridInteractionMessage, args: any) {
2022-02-05 00:32:02 +07:00
2022-03-03 20:56:49 +07:00
const guild = data.getGuild();
if(!guild) return;
2022-02-05 00:32:02 +07:00
2022-06-07 03:08:27 +07:00
const locale = await Locale.getGuildLocale(guild.id);
2022-03-03 20:56:49 +07:00
const instance = DiscordMusicPlayer.getGuildInstance(guild.id);
2022-06-07 03:08:27 +07:00
if(!instance || !instance.queue.track[0]) return await sendHybridInteractionMessageResponse(data, { embeds: [EMBEDS.NO_MUSIC_PLAYING(data, locale)] });
2022-02-05 00:32:02 +07:00
const row = new MessageActionRow()
.addComponents(
new MessageButton()
.setEmoji('▶️')
.setLabel('Open on YouTube')
.setURL(encodeURI(`https://www.youtube.com/watch?v=${instance.queue.track[0].id}`))
.setStyle('LINK'),
)
2022-06-07 03:08:27 +07:00
return await sendHybridInteractionMessageResponse(data, { embeds: [EMBEDS.NOW_PLAYING(data, locale, instance.queue.track[0])], components: [row] });
2022-02-05 00:32:02 +07:00
}
}