我尝试为我的discord机器人制作一个带有菜单和按钮的帮助命令,但我遇到了多个问题:
我不知道如何处理单击按钮时显示的“交互失败”文本,即使按钮已响应。
如何在最新的discord.js中使用***deferUpdate()***按钮?如果我应该使用收集器,我应该把收集器放在哪里?我看了官方文档,但仍然不明白如何使用它。
我也尝试过不使用收集器或等待,但第二次使用help命令时总是得到“Interaction has been acknowledged”。
此外,我总是得到错误,如无效的身体形式,未知的互动,或互动已经确认时,单击按钮。
下面是处理交互的代码部分:
message.channel.send({
embeds: [page1], components:[row]
}).then((msg) => {
let times = 0
var interval = setInterval(function(){
times += 1;
if(times === 15){
clearInterval(interval);
firstBtn.setDisabled(true)
lastBtn.setDisabled(true)
}
}, 1000);
client.on('interactionCreate', interaction => {
if (!interaction.isButton()) return;
if(interaction.customId == 'next'){
curPage += 1
firstBtn.setDisabled(false)
previousBtn.setDisabled(false)
nextBtn.setDisabled(false)
if(curPage == maxPage){
nextBtn.setDisabled(true)
lastBtn.setDisabled(true)
}
msg.edit({embeds: [pages[curPage-1]], components:[row]})
} else if(interaction.customId == 'previous'){
curPage -= 1
lastBtn.setDisabled(false)
previousBtn.setDisabled(false)
nextBtn.setDisabled(false)
if(curPage == 1){
previousBtn.setDisabled(true)
firstBtn.setDisabled(true)
}
msg.edit({embeds: [pages[curPage-1]], components:[row]})
} else if(interaction.customId == 'first'){
curPage = 1
firstBtn.setDisabled(true)
lastBtn.setDisabled(false)
previousBtn.setDisabled(true)
nextBtn.setDisabled(false)
msg.edit({embeds: [pages[curPage-1]], components:[row]})
} else if(interaction.customId == 'last'){
curPage = maxPage
firstBtn.setDisabled(false)
lastBtn.setDisabled(true)
nextBtn.setDisabled(true)
previousBtn.setDisabled(false)
msg.edit({embeds: [pages[curPage-1]], components:[row]})
}
const collector = interaction.channel.createMessageComponentCollector({ time: 3500 });
collector.on('collect', async i => {
if(i.user.id === interaction.user.id){
if (interaction.isButton()) {
interaction.deferUpdate();
}
}
});
collector.on('end', collected => {console.log(`Collected ${collected.size} items`); collector.stop()});
});
})
},
help命令的完整代码:
help: function (message, client) {
let categories = listCmds.getCates();
let maxPage = 2;
let curPage = 1;
let funCmds = listCmds.getCmds(listCmds.liCmds.categories.FUN)
let row = new ActionRowBuilder()
.addComponents(
firstBtn = new ButtonBuilder()
.setCustomId('first')
.setLabel('First page')
.setStyle(ButtonStyle.Success)
.setDisabled(true)
)
.addComponents(
previousBtn = new ButtonBuilder()
.setCustomId('previous')
.setLabel('<<')
.setStyle(ButtonStyle.Primary)
)
.addComponents(
nextBtn = new ButtonBuilder()
.setCustomId('next')
.setLabel('>>')
.setStyle(ButtonStyle.Primary)
)
.addComponents(
lastBtn = new ButtonBuilder()
.setCustomId('last')
.setLabel('Last page')
.setStyle(ButtonStyle.Success)
)
let page1 = new EmbedBuilder()
.setAuthor({ name: 'Help menu', iconURL: 'https://i.postimg.cc/rwXj33rv/sonnnn.png' })
.setTitle(`${categories[0]}`)
.setColor([144, 238, 144])
.setURL('https://www.youtube.com/channel/UCVUWJ4CIuE9e7hXNiKOjg5w/featured')
.addFields(
{ name: funCmds[0][0], value: funCmds[0][1]},
{ name: funCmds[1][0], value: funCmds[1][1]},
{ name: funCmds[2][0], value: funCmds[2][1]},
{ name: funCmds[3][0], value: funCmds[3][1]},
)
.setFooter({ text: `Page 1/${maxPage}. If you find any bugs, please report at https://discord.gg/wRtZ6fRhZC.`})
let page2 = new EmbedBuilder()
.setAuthor({ name: 'Help menu', iconURL: 'https://i.postimg.cc/rwXj33rv/sonnnn.png' })
.setTitle(`This is the end!`)
.setColor([144, 238, 144])
.setURL('https://www.youtube.com/channel/UCVUWJ4CIuE9e7hXNiKOjg5w/featured')
.addFields(
{
name: 'More categories of commands coming soon!',
value: ':)',
inline: true
},
)
.setFooter({ text: `Page 2/${maxPage}. If you find any bugs, please report at https://discord.gg/wRtZ6fRhZC.`})
let pages = [page1, page2]
message.channel.send({
embeds: [page1], components:[row]
}).then((msg) => {
let times = 0
var interval = setInterval(function(){
times += 1;
if(times === 15){
clearInterval(interval);
firstBtn.setDisabled(true)
lastBtn.setDisabled(true)
}
}, 1000);
client.on('interactionCreate', interaction => {
if (!interaction.isButton()) return;
if(interaction.customId == 'next'){
curPage += 1
firstBtn.setDisabled(false)
previousBtn.setDisabled(false)
nextBtn.setDisabled(false)
if(curPage == maxPage){
nextBtn.setDisabled(true)
lastBtn.setDisabled(true)
}
msg.edit({embeds: [pages[curPage-1]], components:[row]})
} else if(interaction.customId == 'previous'){
curPage -= 1
lastBtn.setDisabled(false)
previousBtn.setDisabled(false)
nextBtn.setDisabled(false)
if(curPage == 1){
previousBtn.setDisabled(true)
firstBtn.setDisabled(true)
}
msg.edit({embeds: [pages[curPage-1]], components:[row]})
} else if(interaction.customId == 'first'){
curPage = 1
firstBtn.setDisabled(true)
lastBtn.setDisabled(false)
previousBtn.setDisabled(true)
nextBtn.setDisabled(false)
msg.edit({embeds: [pages[curPage-1]], components:[row]})
} else if(interaction.customId == 'last'){
curPage = maxPage
firstBtn.setDisabled(false)
lastBtn.setDisabled(true)
nextBtn.setDisabled(true)
previousBtn.setDisabled(false)
msg.edit({embeds: [pages[curPage-1]], components:[row]})
}
const collector = interaction.channel.createMessageComponentCollector({ time: 3500 });
collector.on('collect', async i => {
if(i.user.id === interaction.user.id){
if (interaction.isButton()) {
interaction.deferUpdate();
}
}
});
collector.on('end', collected => {console.log(`Collected ${collected.size} items`); collector.stop()});
});
})
},
1条答案
按热度按时间k4aesqcs1#
您需要处理
collector
内部的按钮,并延迟collector
的按钮,而不是交互本身。因此,完全删除
client.on
部分,并执行以下操作: