Discord.js音乐漫游器队列命令

时间:2020-10-25 12:14:11

标签: javascript node.js discord discord.js

我想创建一个可分页的队列命令,以便可以通过反应控制整个队列。但是,每当我运行命令时,我总是收到错误消息。因此,我希望机器人在消息作者对“⏹”反应做出反应时删除队列嵌入消息。这是我的代码:

const { MessageEmbed } = require("discord.js");

module.exports = {
  name: "queue",
  aliases: ["q"],
  description: "Show the music queue and now playing.",
  async execute(message) {
       const serverQueue = message.client.queue.get(message.guild.id);
        if (!serverQueue) return message.channel.send('❌ **Nothing playing in this server**');
      try {
        let currentPage = 0;
        const embeds = generateQueueEmbed(message, serverQueue.songs);
        const queueEmbed = await message.channel.send(`**Current Page - ${currentPage + 1}/${embeds.length}**`, embeds[currentPage]);
        await queueEmbed.react('⬅️');
        await queueEmbed.react('⏹');
        await queueEmbed.react('➡️');

        const filter = (reaction, user) => ['⬅️', '⏹', '➡️'].includes(reaction.emoji.name) && (message.author.id === user.id);
        const collector = queueEmbed.createReactionCollector(filter);
        
        collector.on('collect', async (reaction, user) => {
          try {
            if (reaction.emoji.name === '➡️') {
                if (currentPage < embeds.length - 1) {
                    currentPage++;
                    queueEmbed.edit(`**Current Page - ${currentPage + 1}/${embeds.length}**`, embeds[currentPage]);
                } 
            } else if (reaction.emoji.name === '⬅️') {
                if (currentPage !== 0) {
                    --currentPage;
                    queueEmbed.edit(`**Current Page - ${currentPage + 1}/${embeds.length}**`, embeds[currentPage]);
                }
            } else {
                collector.stop();
                reaction.message.reactions.removeAll();
                queueEmbed.delete();
            }
            await reaction.users.remove(message.author.id);
          } catch {
            console.log();
            return message.channel.send("An error occured");
          }
        });
      } catch {
        console.log();
          return message.channel.send("An error occured");
      }
    }
};

function generateQueueEmbed(message, queue) {
    const embeds = [];
    let k = 10;
    for (let i = 0; i< queue.length; i += 10) {
        const current = queue.slice(i, k);
        let j = i;
        k += 10;
        const info = current.map(track => `${++j} - [${track.title}](${track.url})`).join('\n');
        const embed = new MessageEmbed()
            .setTitle('Song Queue\n')
            .setThumbnail(message.guild.iconURL())
            .setColor("#F8AA2A")
            .setDescription(`**Current Song - [${queue[0].title}](${queue[0].url})**\n\n${info}`)
            .setTimestamp();
        embeds.push(embed);
    }
    return embeds;
}

但是,即使在僵尸程序对停止反应做出反应后将其删除。我从漫游器收到一条消息,说An error occurred。我无法理解为什么,也无法解决。你能帮我吗?预先感谢!

0 个答案:

没有答案