【问题标题】:Wait for message then send response discord.py等待消息然后发送响应 discord.py
【发布时间】:2021-10-13 16:19:52
【问题描述】:

我正在寻求以下代码的帮助:

if message.content.startswith("keycode"):
      channel = bot.get_channel(00000000000000)
      await channel.send('sentence 1')
      await asyncio.sleep(3)
      await channel.send('sentence 2.')
      await asyncio.sleep(3)
      await channel.send('Please reply "yes" or "no".')

      try:
        await bot.wait_for('message', timeout=15.0)
      except asyncio.TimeoutError:
        await channel.send('You ran out of time to answer!')
      if message.content == 'yes':
        await channel.send('You replied yes')
      else:
        await channel.send('You didn't reply yes.')

基本上,根据请求,discord 机器人会对所选频道做出响应,然后在响应之后询问用户是否可以回复“是”或“否”。然后,如果用户回答“是”,它会给出“你回答是”的响应,如果不是,它会给出“你没有回答是”的响应。

我的问题是,每当我运行命令并在其响应后键入“是”时,我总是得到“您没有回答是”响应”。(它确实发送“您没有时间回答”如果我在给定的时间范围内没有回复,请回复)。

我对这一切都很陌生,所以如果有人能找出我所有的错误,那就太好了。提前致谢。

【问题讨论】:

    标签: discord discord.py bots


    【解决方案1】:

    我明白你想做什么。我会再做一个if message.content == "no": 所以类似..

    if message.content.startswith("keycode"):
          await message.channel.send('sentence 1')
          await asyncio.sleep(3)
          await message.channel.send('sentence 2.')
          await asyncio.sleep(3)
          await message.channel.send('Please reply "yes" or "no".')
    
          try:
            await bot.wait_for('message', timeout=15.0)
          except asyncio.TimeoutError:
            await message.channel.send('You ran out of time to answer!')
          if message.content == 'yes':
            await message.channel.send('You replied yes')
          if message.content == 'no':
            await message.channel.send('You replied no')
          else:
            await message.channel.send("You didn't reply with yes or no.")
    

    我还看到您希望将您的消息发送到看到channel = bot.get_channel(00000000000000) 的某个频道。如果有人在与该频道不同的频道中发送“keycode”,那么机器人不会将其发送到作者输入“keycode”的频道,如果那样的话。所以我更改了代码,以使机器人在作者输入的同一频道中发送这些消息。

    【讨论】:

      【解决方案2】:

      简单地说,您当前正在比较 first 消息的内容是否为“是”,而不是您等待的内容。换句话说,如果第一条消息(以 'keycode' 开头的消息)不完全等于 yes(它永远不会,因为它以 'keycode' 开头),您将永远不会发送 yes 回复

      改为使用来自wait_for() 的返回消息事件,这将为您提供您等待自省的消息:

      if message.content.startswith("keycode"):
          channel = bot.get_channel(00000000000000)
          await channel.send('sentence 1')
          ...
      
          try:
              reply_message = await bot.wait_for('message', timeout=15.0)
          except asyncio.TimeoutError:
              await channel.send('You ran out of time to answer!')
          else:
              if reply_message.content == 'yes':
                  await channel.send('You replied yes')
              else:
                  await channel.send('You didn\'t reply yes.')
      

      【讨论】:

        猜你喜欢
        • 2020-12-03
        • 1970-01-01
        • 1970-01-01
        • 2020-04-19
        • 2020-12-26
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多