src/commands/queue.ts (view raw)
1import { SlashCommandBuilder, ChatInputCommandInteraction } from 'discord.js';
2import { formatTitle, getChannel, getQueue } from '../functions/music';
3import { YouTubeVideo } from 'play-dl';
4
5const CHARACTER_LIMIT_API = 2000;
6
7function getReply(result: YouTubeVideo[]): string {
8 const nowPlaying = "Now playing: " + formatTitle(result.shift());
9
10 if (!result.length) {
11 return nowPlaying;
12 }
13
14 let reply = "Queue:"
15 const characterLimit = CHARACTER_LIMIT_API - nowPlaying.length - 6; // 4 chars for "\n...", 2 chars for "\n\n"
16
17 for (let r in result) {
18 const video = result[r];
19 const new_string = `\n${r + 1}. ${formatTitle(video)}`;
20 if (reply.length + new_string.length > characterLimit) {
21 reply += "\n...";
22 break;
23 }
24 reply += new_string;
25 }
26 return reply + "\n\n" + nowPlaying;
27}
28
29module.exports = {
30 data: new SlashCommandBuilder()
31 .setName('queue')
32 .setDescription('Show current queue status.'),
33
34 async execute(interaction: ChatInputCommandInteraction) {
35 const channel = await getChannel(interaction);
36 if (typeof channel == 'string')
37 return await interaction.reply({ content: channel, ephemeral: true });
38
39 const queue = getQueue(interaction.guildId);
40 const result = queue.queue;
41 if (result) {
42 const reply = getReply(result);
43 return await interaction.reply({ content: reply });
44 }
45 return await interaction.reply({ content: 'Queue is empty.' });
46 },
47};