【问题标题】:Discord Bot - Wait for Reply after InteractionDiscord Bot - 交互后等待回复
【发布时间】:2022-01-21 02:18:09
【问题描述】:

当我们使用awaitMessages 时,我可能不太了解 Discord API 的工作原理。我正在尝试做的是在私人频道中单击按钮后等待用户的消息:

client.on('interactionCreate', async interaction => {

if (interaction.isButton()) {
    if (interaction.customId.startsWith('dialogue-')) {
        const embed = new MessageEmbed()
            .setColor('#1a8175')
            .setTitle('???? Dialogue')
            .setDescription('Please type your dialgoue')

        await interaction.channel.send({embeds: [embed]})

        // My problem lies here
        const filter = m => m.author.id === interaction.author.id;
        await interaction.channel.awaitMessages(filter, {
            max: 1,
            time: 60000,
            errors: ['time']
        }).then(
            async(collected) => {
                await interaction.channel.send('Received: ' + collected.first().content.toLowerCase())
            })
    }
}

如您所见,用户单击按钮,会发送一条消息,要求进行对话。之后,机器人应该会收到下一条消息。

调试后,我看到我在消息发送给用户后键入的所有内容都会触发messageCreate 事件,这就是我的代码无法正常工作的原因。据我了解,当我们使用 awaitMessages 时,机器人应该等待 Promise 完成。我无法弄清楚我在这里缺少什么。有任何想法吗?提前致谢

【问题讨论】:

    标签: javascript discord discord.js bots


    【解决方案1】:

    正如@GodderE2D 所说,您需要在过滤器中将interaction.author.id 更改为interaction.user.id

    您还需要根据docs 在对象中移动您的过滤器,如下所示:

    const filter = m => m.author.id === interaction.user.id
    interaction.channel.awaitMessages({ filter, max: 1, time: 60000 })
    

    【讨论】:

      【解决方案2】:

      阅读更多文档后,我发现了另一种完成相同任务的方法:使用MessageCollectors

      const filter = m => m.author.id === interaction.user.id
              const collector = interaction.channel.createMessageCollector(filter, {max: 1, time: 60000})
              collector.once('collect', async (message) => {
                  const embed = new MessageEmbed()
                      .setColor('#1a8175')
                      .setTitle(`? Dialogue ${dialogueNumber} received with success!!`)
                      .setDescription(`Dialogue received: ${message.content}`)
      
                  await interaction.channel.send({embeds: [embed]})
              })
      

      它完成了工作并且运行良好。但是time 指令无法正常工作。我已将时间设置为 4 秒,以便在回复时间过长时向用户发送消息。使用侦听器end 应该可以完成这项工作,不知何故无法正常工作,并且机器人等待回复很长时间(我更喜欢这种方式)但我想了解为什么机器人仍然挂在那里,等待用户回复。我有一种感觉,过滤器一定是错误的:

              collector.on('end', collected => {
                  if (collected.size === 0) {
                      interaction.channel.send('Timeout - You did not send a dialogue')
                  }
              });
      

      【讨论】:

        【解决方案3】:

        要获取初始化交互的用户,您应该使用Interaction#user。虽然您应该使用 Message#author 访问消息的作者,但您需要使用 user 进行交互。

        const filter = m => m.author.id === interaction.user.id;
        

        如果您不了解某些属性或方法,可以随时参考the documentation

        【讨论】:

          猜你喜欢
          • 2022-11-06
          • 2021-10-16
          • 2018-06-05
          • 2021-03-31
          • 2018-11-07
          • 2022-01-09
          • 2021-02-07
          • 2020-12-18
          • 2021-12-15
          相关资源
          最近更新 更多