BOT不是悄悄话正确回答

问题描述 投票:0回答:1

所以,这个代码块是玩家A(挑战者)的游戏发布到玩家B(目标)是一个挑战守则的一部分。机器人发送悄悄话给玩家B告诉他们,他们的挑战,并询问他们是否接受或拒绝的挑战。 下面的代码似乎并没有回应什么玩家B与答复。

if (message.channel.id === '541736552582086656') return target.send("Do you accept the challenge? Please reply with 'accept' or 'deny'.")
  .then((newmsg) => {
    newmsg.channel.awaitMessages(response => response.content, {
      max: 1,
      time: 150000,
      errors: ['time'],
    }).then((collected) => {
      if (collected === 'accept') {
        newmsg.channel.send("You have ***accepted*** the challenge. Please wait while your battlefield is made...");
      } else if (collected === 'deny') {
        newmsg.channel.send("You have ***denied*** the challenge.")
      }
    }).catch(() => {
      newmsg.channel.send('Please Accept or Deny the challenge.');
    });
  });
}

在此之前的代码块,我设置了登录信息到服务器上的一个通道,发送挑战者和目标的挑战信息。机器人成功地接触,他们通过质疑下午目标,但答复的内容(即使有回复时“接受”还是会觉得被拒绝。 感谢您的任何和所有帮助!

javascript node.js discord.js
1个回答
0
投票

扩大在@Stock Overflaw的回答,awaitMessages总是返回获取信息的集合,这意味着collected === 'accepted'将无法正常工作。你检查,如果一个集合对象相同的字符串。

你需要的是从集合抢第一(和你的情况只)消息,并检查其对字符串的内容。下面你会发现你的.then(...)声明重写。搏一搏,让我知道结果是什么。

附:您的收藏过滤器将无法正常工作,你可能期望。该过滤器只检查,如果消息将被添加到集合与否。因为你的“过滤器”是response => response.content,它只会检查response.content不为空,nullundefined

.then((collected) => {
  // Grabs the first (and only) message from the collection.
  const reply = collected.first();

  if (reply.content === 'accept'){
    reply.channel.send("You have ***accepted*** the challenge. Please wait while your battlefield is made...");
  } else if (reply.content === 'deny') {
    reply.channel.send("You have ***denied*** the challenge.") 
  } else {
    reply.channel.send("Your response wasn't valid.");
    /*
     * Code here to handle invalid responses
     */
  }
})
© www.soinside.com 2019 - 2024. All rights reserved.