javascript - 不断收到 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)是显示的歌曲位置,因此我为每一页设置了一个新的嵌入,我知道这很乱,但我想不出一种方法让它更整洁,因为我对编程不太熟悉然而。感谢您的帮助,我非常感谢。
解决方案
分页在网站或开发者网页上使用得更多,尝试在机器人上使用它是可能的,但更加困难和复杂。我建议你使用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')
))
}
推荐阅读
- java - File.createNewFile():如何处理“未报告的异常”错误?
- javascript - React Reducer 没有更新状态
- java - 这是编写异步迭代器的正确方法吗?
- c# - 从测试方法传递空对象
- cplex - 在 Cplex 中调用变量
- windows - 如何在 Windows 内核中通过句柄/对象杀死系统线程
- wordpress - 如何在每个 Gutenberg 块中添加更高级的字段?
- php - 如何查找外部查询的总记录
- python - IBM cloud/bluemix 上的 Python Flask 应用程序,带有 Textblob 库抛出异常 - textblob.exceptions.MissingCorpusError
- ios - 当根视图设置为不同的视图控制器时,导航控制器将变为视图控制器