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

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

src/commands/queue.ts (view raw)

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