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

Javascript 如何在discord.js中制作菜单?,javascript,node.js,discord.js,Javascript,Node.js,Discord.js,我目前正在使用discord.js制作一个discord机器人,我正在尝试制作一个菜单命令,这样当用户键入[prefix]菜单时,它会发送菜单的图片 我可以很容易地做到这一点,但我正在尝试使它多页,我知道我可以只做一个menu1,menu2和menu3命令,但我想有一个命令说,下一页和上一页这是我到目前为止,但它不工作,没有错误 if (command === "menu") { output() message.channel.send(

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

      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`});
            }
if(命令==“菜单”){
输出()
message.channel.send({file:'./images/menu1.png'});
curPage=1;
}
如果(命令==“下一页”){
输出()
curPage++;
如果(curPage>=3){
输出()
curPage=3;message.channel.send(“您在最后一页了!”);
}
}else if(命令==“上一页”){
输出()
curPage--;

如果(curPage我认为实现这一点的最佳方法是使用.waitingmessages

这可能是值得尝试的东西沿着这些路线,但我不是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 
            });
        });
    }

我认为实现这一目标的最佳方法是使用。等待消息

这可能是值得尝试的东西沿着这些路线,但我不是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 
            });
        });
    }
改用a

当用户对目标消息作出反应时,将发出一个事件

例如:

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();
});

文档:,

请改用

当用户对目标消息作出反应时,将发出一个事件

例如:

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();
});


文档:,,

另一种效率较低的方法可能是让菜单命令在设定的时间内发送图像,然后让机器人删除该消息。然后,您可以使用单独的“下一步”或“上一步”命令,仅在删除该消息之前有效,也可以刷新计时器并简单地删除图像和消息结束请求的新菜单页。等待消息的另一种替代方法是使用另一种效率较低的方法来实现这一点,可能是让菜单命令在设定的时间内发送图像,然后让机器人删除消息。然后,您可以使用单独的“下一步”或“上一步”命令,这些命令仅在消息被取消之前起作用删除并刷新计时器,只需删除图像并发送请求的新菜单页。等待消息的另一种方法是使用