等待消息然后发送响应 discord.py

Wait for message then send response discord.py

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

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 机器人将对所选频道做出响应,然后根据该响应询问用户是否可以回复“是”或“否”。然后,如果用户回答“是”,它将给出“你回答是”的响应,如果不是,它将给出“你没有回答是”的响应。

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

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

我明白你想做什么了。我会再做一个 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”的频道(如果从那以后)。所以我更改了代码,以使机器人在作者输入的同一频道中发送这些消息。

简而言之,您当前比较的是 第一个 消息的内容 'yes' 而不是您等待的那个。换句话说,如果第一条消息(以 'keycode' 开头的消息)不完全等于 yes(永远不会,因为它以 'keycode' 开头),你永远不会发送你的 yes 回复

相反,使用从 wait_for() 返回的消息事件,这将为您提供您等待内省的 new 消息:

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.')