Discord.js使用.awaitMessages创建响应命令

时间:2020-06-04 16:31:17

标签: discord.js

基本上我想使用.awaitMessages命令, 我想将答案存储在MessageEmbeds

如果机器人要求我提供标题,则应将其存储在.setTitle(title)中 如果漫游器要求我提供描述,则应将其存储在.setDescription(description)中。

1 个答案:

答案 0 :(得分:1)

如评论中所建议,您应该使用TextChannel.awaitMessages():每次要问的问题时,您都需要等待该用户的消息,然后转到下一件事情,直到您拥有所需的一切为止。
您可以使用Promise.then()方法来做所有事情,但是我发现只使用异步函数就更容易了

您需要执行以下操作:

// Let's say that message is the message with the command
async function getInfo(message) {
  // We'll use this if the user doesn't reply
  let timedOut = false

  // Save channel and author so that it's easier to write
  const { channel, author } = message

  // Define a filter function that will accept only messages from this author
  const isFromAuthor = m => m.author.id == author.id

  const options = {
    max: 1,
    time: 60 * 1000 // max time to wait (in ms)
  }

  // First question
  await channel.send('Title?')
  // await for the first response
  const firstColl = await channel.awaitMessages(isFromAuthor, options)

  // Check that there's at least one message
  if (firstColl.size > 0) {
    const title = firstColl.first().content

    await channel.send('Description?')
    const secondColl = await channel.awaitMessages(isFromAuthor, options)

    if (secondColl.size > 0) {
      const description = secondColl.first().content

      // You have everything you need, you can now send your embed
      let embed = new Discord.MessageEmbed({
        title,
        description
      })

      channel.send(embed)
    } else timedOut = true
  } else timedOut = true

  if (timedOut)
    channel.send('Command canceled (timed out)')
}