javascript Discord.js v14如何处理按钮交互

wbrvyc0a  于 2023-02-28  发布在  Java
关注(0)|答案(1)|浏览(151)

我尝试为我的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()});
            });
        })
    },
k4aesqcs

k4aesqcs1#

您需要处理collector内部的按钮,并延迟collector的按钮,而不是交互本身。
因此,完全删除client.on部分,并执行以下操作:

// should definitely increase the time in your collector.
const collector = interaction.channel.createMessageComponentCollector({ time: 3500 });

collector.on('collect', async i => {
  // check if the author triggered the interaction
  if(i.member.id != interaction.user.id) {
    return i.reply({ content: `This interaction is not for you`, ephemeral: true})
  }

  if(i.customId == 'next') {
    // defer the interaction
    await i.deferUpdate();

    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]})
  }

  if(i.customId == 'previous') {
    // defer update
    await i.deferUpdate();

   // Rest of your code
 }

 // and so on

});

相关问题