如何在 discord.js 中制作菜单?

how to make a menu in discord.js?

我目前正在使用 discord.js 制作一个 discord 机器人,我正在尝试制作一个菜单命令,因此当用户键入 [prefix]menu 时,它会发送一张菜单图片 我可以很容易地做到这一点,但我试图让它多页,我知道我可以只做一个 menu1、menu2 和 menu3 命令,但我想有一个命令说 nextpage 和 previouspage 这是我到目前为止所拥有的,但它没有工作并且没有错误

      if (command === "menu") {
          output()
            message.channel.send({file: './images/menu1.png'});
            curPage = 1;
        }

        if (command === "next page") {
          output()
                curPage++;
                if(curPage >= 3) {
                  output()
                    curPage = 3; message.channel.send("You're on the last page!");
                }
            } else if (command === "previous page") {
              output()
                curPage--;
                if(curPage <= 0) {
                  output()
                    curPage = 1; message.channel.send("You're on the first page!");
                }
                message.channel.send({file: `./images/menu${curPage}.png`});
            }

我认为最好的方法是使用 .awaitMessages?

https://discord.js.org/#/docs/main/stable/class/TextChannel?scrollTo=awaitMessages

沿着这些思路可能值得尝试一些东西,但是我不是 100% 确定如何等待多次来回分页...我有兴趣看到其他人的解决方案。

例如:

if (command === "menu") {
        message.channel.send({file: './images/menu1.png'});
        .then(() => {
          message.channel.awaitMessages(response => response.content === 'next', {
            max: 1,
            time: 30000,
            errors: ['time'],
          })
          .then((collected) => {
              message.channel.send({file: './images/menu2.png'});
            })
            .catch(() => {
              // Do something with error 
            });
        });
    }

改用 ReactionCollector

当用户对目标消息做出反应时,将发出 collect 事件。

示例:

const collector = message.createReactionCollector((reaction, user) => 
    user.id === message.author.id &&
    reaction.emoji.name === "◀" ||
    reaction.emoji.name === "▶" ||
    reaction.emoji.name === "❌"
).once("collect", reaction => {
    const chosen = reaction.emoji.name;
    if(chosen === "◀"){
        // Prev page
    }else if(chosen === "▶"){
        // Next page
    }else{
        // Stop navigating pages
    }
    collector.stop();
});

文档:Message, ReactionCollector, Reaction, User