最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - how to make a menu in discord.js? - Stack Overflow

programmeradmin0浏览0评论

I am currently making a discord bot using discord.js and i am trying to make a menu mand so when a user types [prefix]menu it sends a picture of the menu i can do that easily but i am trying to make it multipaged, i know i could just make a menu1, menu2 and menu3 mand but id like to have a mand saying nextpage and previouspage this is what i have so far but it doesn't work and there is no errors

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

        if (mand === "next page") {
          output()
                curPage++;
                if(curPage >= 3) {
                  output()
                    curPage = 3; message.channel.send("You're on the last page!");
                }
            } else if (mand === "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`});
            }

I am currently making a discord bot using discord.js and i am trying to make a menu mand so when a user types [prefix]menu it sends a picture of the menu i can do that easily but i am trying to make it multipaged, i know i could just make a menu1, menu2 and menu3 mand but id like to have a mand saying nextpage and previouspage this is what i have so far but it doesn't work and there is no errors

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

        if (mand === "next page") {
          output()
                curPage++;
                if(curPage >= 3) {
                  output()
                    curPage = 3; message.channel.send("You're on the last page!");
                }
            } else if (mand === "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`});
            }
Share Improve this question edited Apr 24, 2018 at 13:55 André 4,4974 gold badges32 silver badges58 bronze badges asked Oct 25, 2017 at 11:23 James MalkinJames Malkin 1492 gold badges2 silver badges15 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 5

Use a ReactionCollector instead.

A collect event would be emitted when a user reacts to the targeted message.

Example:

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

Docs: Message, ReactionCollector, Reaction, User

I think the best way to achieve this, would be with .awaitMessages?

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

It might be worth trying something along these lines, however I'm not 100% sure about how to await multiple times for paging back and forth... I'd be interested in seeing someone elses solution for this.

For example:

if (mand === "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 
            });
        });
    }
发布评论

评论列表(0)

  1. 暂无评论