不和谐等待消息超时| Discord V12

时间:2020-10-05 23:04:41

标签: discord.js

我陷入一个问题。当“ Player 2”(玩家[1])在频道中键入!yes时,则说明其超时。我不确定自己在做什么错。玩家[1]被定义为msg.mentions.members.first();

       let answer = true;
        
    
        if (players[1].user.bot) {
        return;
        } else {
                   
            answer = await msg.channel.awaitMessages((msg) => {
                console.log(msg.author.id === players[1].id && msg.content === `!yes`)  // returns true
                if (msg.author.id === players[1].id && msg.content === `!yes`) {
                    console.log("Player has accepted") // The console does print "Player has accepted"
                    return true;
                }
                return false;
            }, {maxMatches: 1, time: 30000, errors: ['time']})
            .catch(() => {
                console.log("Timed out!") // The console does print "Timed Out as well"
                return false;
            });
        }

        // if user refuses to play
        if (!answer) {
            return channel.send(`${players[1]} preferred to run away.`);
        }

1 个答案:

答案 0 :(得分:0)

您对awaitMessages()的语法有误-第一个参数应该是CollectorFilter(请参阅here),而不是回调。

请考虑改用createMessageCollector()。它比awaitMessages()读起来要好得多,并且比强制将async / await放入收集器更有意义。应该看起来像这样:

const filter = m => (m.author.id===players[1].id && m.content==="!yes");
const collector = msg.channel.createMessageCollector(filter, {max: 1, time: 30000});

collector.on("collect", (m) => {
  // Player has accepted... do whatever
});
collector.on("end", (reason) => {
  if(reason === "time") {
     // Ran out of time
  }
});