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

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

src/commands/queue.ts (view raw)

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