簡體   English   中英

Bot無法在私人訊息中正確回復

[英]Bot not replying correctly in a private message

因此,此代碼塊是游戲者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.');
    });
  });
}

在此代碼塊之前,我設置了將消息記錄到服務器上的通道,向質詢者和目標發送質詢信息。 該漫游器通過pm成功聯系了他們所挑戰的目標,但是回復(即使回復“接受”時仍會認為該請求已被拒絕)。
感謝您提供的所有幫助!

擴展@Stock Overflaw的答案后, awaitMessages將始終返回獲取的消息的集合,這意味着collected === 'accepted'將不起作用。 您正在檢查集合對象是否與字符串相同。

您需要的是從集合中獲取第一條消息(僅在您的情況下),並根據字符串檢查其內容。 在下面,您可以找到重新編寫的.then(...)語句。 試試看,讓我知道結果如何。

ps您的收藏夾過濾器將無法正常工作。 過濾器僅檢查是否將消息添加到集合中。 由於您的“過濾器”是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
     */
  }
})

暫無
暫無

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

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