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

时间:2020-11-06 17:40:42

标签: 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 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')

2 个答案:

答案 0 :(得分:0)

我想这段代码在on_message()中。
如果您希望答案将在单独的消息中显示,则:
与on_message()不一致的是,它使函数在发送消息时运行,因此尝试等待类似这样的消息将不起作用,因为message.content仍为!examplecommand
您需要做的是找到一种扫描将来的消息作为答案的方法。
可以这样完成:

#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") 

因此,您可以使!examplecommand成为一个单独的命令,而不是将其放入on_masseges()中。

答案 1 :(得分:0)

使用命令扩展中的wait_for方法可以轻松实现这一点

@client.command()
async def examplecommand(ctx):
    await message.channel.send("Example Question?")
    
    def check(msg):
        return msg.author.id == ctx.author.id and msg.channel.id == ctx.channel.id

    answermsg = await client.wait_for('message', check = check)

    if "yes" in answermsg.content.lower() or "no" in answermsg.content.lower():
        await ctx.send("example answer")

    else:
        await ctx.send("Invalid response")

如您所见,使用on_message事件来实现这一点根本不需要,如果您还没有的话,也应该开始使用命令扩展名。

有关更多信息,请访问the documentation