Discord.js v12:awaitMessages()的过滤器不起作用

时间:2020-07-05 14:34:19

标签: javascript node.js discord discord.js

我似乎无法过滤掉消息,以便该机器人仅在原始命令的作者发送该消息之前才注册该消息。

var op = message.author.id;

function first_name() {
  message.channel.send(' — First Name:');
  const verify = (member) => {
    member.id == op
  };
  message.channel.awaitMessages(verify)
    .then(collected => {
      var f_name = collected.first();
      message.channel.send("Please confirm you want your character's name to be " + f_name)
        .then(function (message) {
          message.react('✅');
          message.react('?️');
          const confirmation = (reaction, member) => {
            return ['✅', '?️'].includes(reaction.emoji.name) && member.id == message.author.id;
          };
          message.awaitReactions(confirmation, {
            max: 1,
            time: 60000,
            errors: ['time']
          })
            .then(collected => {
              var reaction = collected.first();
              if (reaction.emoji.name == '✅') {
                db.set((message.author).fname, f_name);
                message.channel.send("First name set to: " + f_name)
              } else if (reaction.emoji.name == '?️') {
                message.delete();
                first_name();
              }
            });
        });
    })
}

原始邮件的作者发送下一封邮件时没有任何反应。

1 个答案:

答案 0 :(得分:1)

这种事情可以通过多种不同方式大大简化。

首先,我建议对多个async使用await / awaitMessages,因为代码会非常嵌套且难以查看。

代替

message.channel.awaitMessages(verify)
  .then(collected => {

使用

var collectedMessages = await message.channel.awaitMessages(filter, {time: /*An amount in ms to wait for messages/*, max: 1 /*Since you're just waiting for one specific message from a user, use this to prevent multiple collected messages./*, errors: ['time']});
//the next part of the code won't execute until a message is collected
var response = collectedMessages.first().content; //collectedMessages is a collection of Messages, so we need to grab the first one out of it (even if there's only one) and then get its content like we do any other Message.

对于一个好的过滤器,您并不太明确要寻找哪种过滤器,但让我尝试使用您所说的内容。

var filter = m => //filter content;

这是一个很好的开始模板。

如果您只想允许触发命令的用户通过过滤器,则只需匹配以下ID:

var filter = m => m.author.id == message.author.id;

m是通过过滤器传递的消息,而message是首先触发该机器人命令的同一消息。

最后,万一用户无法回复,只需使用try / catch块,像这样

var filter = m => m.author.id == message.author.id;
try {
  var collectedMessages = await message.channel.awaitMessages(filter, {time: 100000, max: 1, errors: ['time']});
} catch (e) {return message.reply("Looks like you took too long to reply!");};
var response = collectedMessages.first().content;

我希望能有所帮助。如果您就问题和所需条件进行了澄清,我将为您提供更多帮助。