all repos — simple-discord-music-bot @ f1b6c2135d3740fb92fc2a8f095e159d93480a4a

A Discord bot making use of discord.js and play-yt.

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 result = await getQueue();
40        if (result) {
41            const reply = getReply(result);
42            return await interaction.reply({ content: reply });
43        }
44        return await interaction.reply({ content: 'Queue is empty.' });
45    },
46};