簡體   English   中英

如何使用等待響應 discord.js 來回移動幫助頁面

[英]How to move back and forth for help page using awaiting reactions discord.js

這是我的示例代碼:

const Discord = require('discord.js')
module.exports = {
    name: 'help',
    description: 'help',
    execute(message, args) { 
        const embed = new Discord.MessageEmbed()
            .setTitle('Page One')
            .setDescription('This is page one')
        message.channel.send(embed).then((msg) => {  
            msg.react('⬅️')
            msg.react('➡️')
 
            const filter = (reaction, user) => {
                return ['⬅️', '➡️'].includes(reaction.emoji.name) && user.id === message.author.id;
            };

            msg.awaitReactions(filter, { max: 1, time: 60000, errors: ['time'] })
                .then(collected => {
                    const reaction = collected.first()

                    if (reaction.emoji.name === '⬅️') {

                        message.channel.send(embed)
                    } 
                    else {
                        const secEmbed = new Discord.MessageEmbed()
                            .setTitle('Help')
                            .setDescription('This is help page 2')
                        msg.edit(secEmbed);
                    }
                }) 
        })
    }
} 

這是可行的,但是當我在移動到第二頁后嘗試將其向后移動到上一頁時,事情就停止了……我的意思是,嵌入頁面不會前后移動。 有沒有辦法解決這個問題? 謝謝你。

你可以簡單地再次調用msg.awaitReactions() 將其包裹在 function 中,以便更容易重復反應收集。

使用msg.awaitReactions()

message.channel.send(embed).then((msg) => {
  msg.react("⬅️");
  msg.react("➡️");

  function handleReactions() {
    const filter = (reaction, user) => {
      return ['⬅️', '➡️'].includes(reaction.emoji.name) && user.id === message.author.id;
    };

    msg.awaitReactions(filter, {max: 1, time: 60000, errors: ["time"]})
      .then((collected) => {
        const reaction = collected.first();
        const name = reaction.emoji.name;
        if (name === "⬅️") {
          // edit page a certain way
          handleReactions();
        } else if (name === "➡️") {
          // edit page another way
          handleReactions();
        }
      });
  }
  handleReactions();

});

另一種方法是監聽反應事件

message.channel.send(embed).then((msg) => {
  msg.react("⬅️");
  msg.react("➡️");

  // handles reactions
  function handleReaction(reaction, user) {
    // ignore the reaction if the reaction is on a different message
    if (reaction.message.id !== msg.id && user.id === message.author.id) {return;}

    const name = reaction.emoji.name;
    if (name === "⬅️") {
      // move page a certain way
    } else if (name === "➡️") {
      // move page another way
    }
    
  }

  // add a listener for message reactions
  message.client.on("messageReactionAdd", handleReaction);

  // wait a specific amount of time to stop listening
  setTimeout(() => {
    // remove the listener
    message.client.off("messageReactionAdd", handleReaction);
  }, 60000); // 60 seconds

  /*
    You could add functions to reset the timeout after each reaction as well.
  */

});

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM