如何等待用户在discord.py中回答问题?

如何等待用户在discord.py中回答问题?,discord.py,Discord.py,我在discord.py中遇到了问题,因为当我问一个问题并检查答案是否有效时,它会扫描缺少的消息,并立即发送我编码的错误消息,这应该只在他们发送无效响应时才会发生 if message.content.startswith('!examplecommand'): await message.channel.send("Example Question?") if message.content in Yes or if message.content in

我在discord.py中遇到了问题,因为当我问一个问题并检查答案是否有效时,它会扫描缺少的消息,并立即发送我编码的错误消息,这应该只在他们发送无效响应时才会发生

if message.content.startswith('!examplecommand'):
    
  await message.channel.send("Example Question?")
  if message.content in Yes or if message.content in No:
     await message.channel.send('Example answer')
  else:
    await message.channel.send("Invalid response")  
有人能帮我在检查前等待答复吗

编辑:谢谢你的帮助,但是代码似乎不起作用

@client.event
async def on_message(message):
  if message.content.startswith("!examplecommand"):
    await message.channel.send("Example Question?")
    question_message.append(message)
    for msg in question_message: 
      if message.channel == question_message:
        if message.content() in Example_Group or message.content() in 
        Example_Group_1 or message.content() in Example_Group_2 or 
        message.content() in Example_Group_3: 
          await message.channel.send("Example Response")
        else:
          await message.channel.send('Invalid Response')

我想这段代码在on_message()中。
如果您的意思是答案将出现在单独的消息中,那么:
_message()上的不一致之处在于,它使函数在发送消息时运行,因此尝试等待这样的消息是行不通的,因为message.content仍然是-
!示例命令

您需要做的是找到一种方式来扫描将来的消息作为您的答案。
可以这样做:

#outside on_message
asked_question = False 
question_message = []
# these two variables will hold your message from the command and if there was a question asked.
----
#inside on_message
if message.content.startswith('!examplecommand'):
     await message.channel.send("Example Question?")
     asked_question = True
     question_message = message
elif asked_question and message.channel == question_message :
     if message.content.lower() == "yes" or message.content.lower() == "no":
     #lower meant so there wont be any capitalizing problems
        await message.channel.send('Example answer')
     else:
        await message.channel.send("Invalid response") 
这样做的问题是,有人可以在另一个频道或另一台服务器上使用“!examplecommand”,而外部变量将再次更改,因此发送的第一个“!examplecommand”将不会得到响应。
如果您真的想让人们发送多个“!examplecommand”,您可能需要一个类似于列表的东西来保存以下所有消息:

#outside on_message
question_message = []
----------------
#inside on_message
if message.content.startswith('!examplecommand'):
     await message.channel.send("Example Question?")
     question_message.append(message)
for msg in question_message: #this wont run if there aren't messages in question_message
     if message.channel == msg.channel :
        if message.content.lower() == "yes" or message.content.lower() == "no": 
        #lower meant so there wont be any capitalizing problems
           await message.channel.send('Example answer')
        else:
           await message.channel.send("Invalid response") 

正因为如此,你才有可能成功!例如,在命令扩展中使用
wait\u for
方法可以很容易地实现这一点

@client.command()
异步定义示例命令(ctx):
等待message.channel.send(“示例问题?”)
def检查(msg):
返回msg.author.id==ctx.author.id和msg.channel.id==ctx.channel.id
answermsg=wait client.wait_for('message',check=check)
如果answermsg.content.lower()中的“是”或answermsg.content.lower()中的“否”:
等待ctx.send(“示例答案”)
其他:
等待ctx.send(“无效响应”)
正如您所看到的,使用
on_message
事件来实现这一点是完全不需要的,如果您还没有开始使用commands extension,那么您也应该开始使用commands extension


更多信息,请参见。

那么您是否将第一块代码中的任何内容放入代码中?因为它似乎不能只处理第二个语句,所以if语句行出现问题,因为您不能执行
if[statement]或if[statement]
,所以需要将其更改为
if[statement]或[statement]
。如果你想再次复制,我修复了第二个块。如果有其他错误,请告诉我谢谢你的帮助!我已经自己修复了
或if
功能,但它似乎仍然不起作用。我很确定我做的每件事都是对的,只是我用了一组单词代替了单词。我想这可能是个错误,但当我以与您相同的方式用普通单词替换组时,甚至当我尝试运行您的精确代码时,它似乎仍然不起作用。它在
之后问了
示例问题?
!examplecommand
,但未运行检查,也从未返回任何响应。代码太长,无法附加到注释中,因此我将更新问题..是的,还有一个小问题。在
if message.channel==question\u message:
中,如果message.channel==msg.channel:,则应为
if message.channel==msg.channel:
,因为您需要检查它是否是同一个频道,但语句检查channel obj==msg obj。(问题_消息是存储MSG的阵列,这也是它无法工作的原因)