首页 > 解决方案 > 不断收到 DiscordAPIError:在 Discord.js 中的音乐机器人上使用 reconlx 的分页执行队列命令时出现未知交互

问题描述

所以我写了一个音乐机器人discord.js,我决定对我的队列命令使用分页,这样我就可以整齐地显示整个队列。但是,大多数时候单击按钮切换页面时,我都会收到未知交互不和谐 API 错误。我正在使用reconlx npm package,这是我的队列命令代码:

            const queue = distube.getQueue(message)

        // embeds
        const nothingPlaying = new MessageEmbed()
        .setDescription('Nothing playing right now!');

        if(!queue) return message.channel.send({ embeds: [nothingPlaying]} );

        const queueEmbed1 = new MessageEmbed()
        .setTitle('Current Queue:')
        .setDescription(`${queue.songs
                            .map(
                                (song, id) =>
                                    `**${id ? id : 'Playing'}**. ${song.name} - \`${
                                        song.formattedDuration
                                    }\``,
                        )
                        .slice(0, 20)
                        .join('\n')}`
                    )
        const queueEmbed2 = new MessageEmbed()
        .setTitle('Current Queue:')
        .setDescription(`${queue.songs
                            .map(
                                (song, id) =>
                                    `**${id ? id : 'Playing'}**. ${song.name} - \`${
                                        song.formattedDuration
                                    }\``,
                        )
                        .slice(20, 40)
                        .join('\n')}`
                    )
        const queueEmbed3 = new MessageEmbed()
        .setTitle('Current Queue:')
        .setDescription(`${queue.songs
                            .map(
                                (song, id) =>
                                    `**${id ? id : 'Playing'}**. ${song.name} - \`${
                                        song.formattedDuration
                                    }\``,
                        )
                        .slice(40, 60)
                        .join('\n')}`
                    )
        const queueEmbed4 = new MessageEmbed()
        .setTitle('Current Queue:')
        .setDescription(`${queue.songs
                            .map(
                                (song, id) =>
                                    `**${id ? id : 'Playing'}**. ${song.name} - \`${
                                        song.formattedDuration
                                    }\``,
                        )
                        .slice(60, 80)
                        .join('\n')}`
                    )
        const queueEmbed5 = new MessageEmbed()
        .setTitle('Current Queue:')
        .setDescription(`${queue.songs
                            .map(
                                (song, id) =>
                                    `**${id ? id : 'Playing'}**. ${song.name} - \`${
                                        song.formattedDuration
                                    }\``,
                        )
                        .slice(80, 100)
                        .join('\n')}`
                    )
                    


        const embeds = [
            queueEmbed1,
            queueEmbed2,
            queueEmbed3,
            queueEmbed4,
            queueEmbed5,
        ]

        
        pagination({
            embeds: embeds,
            channel: message.channel,
            author:message.author,
            fastSkip: true,    
        })

切片(NUMBERS)是显示的歌曲位置,因此我为每一页设置了一个新的嵌入,我知道这很乱,但我想不出一种方法让它更整洁,因为我对编程不太熟悉然而。感谢您的帮助,我非常感谢。

标签: javascriptnpmpaginationdiscorddiscord.js

解决方案


分页在网站或开发者网页上使用得更多,尝试在机器人上使用它是可能的,但更加困难和复杂。我建议你使用discord-slider. 在您的情况下,使用和处理面对不和谐 api 的更好问题要简单得多。你可以像这样使用它:

const Discord = require('discord.js')
const client = new Discord.Client()


require("discord-buttons")(client);
require('discord-slider')(client);

// On a command :

channel.createSlider(userID, embedsArray, emojiNext, emojiBack)

例如,在您的情况下,您可以使用

channel.createSlider(message.author.id,embeds, "Next", "Back")

学到更多here

--

PS:您似乎重复地创建了相同的嵌入,只需一点点更改。5尝试创建一个循环,相信我它的效率更高,您甚至可以通过更改第 3 行来选择您想要的嵌入数量!

let embeds = []

for (i = 0; i < 5; i++)
{
 embeds.push(new Discord.MessageEmbed()
        .setTitle('Current Queue:')
        .setDescription(queue.songs
                            .map(
                                (song, id) =>
                                    `**${id ? id : 'Playing'}**. ${song.name} - \`${
                                        song.formattedDuration
                                    }\``,
                        )
                        .slice((i*20), (i+1)*20)
                        .join('\n')
                    ))
}

推荐阅读