Python等待消息的时间限制

时间:2018-10-31 17:49:29

标签: python-3.x discord.py

当我们使用!ping命令时,您好,下面的代码工作正常,机器人首先询问Question 1,然后如果成员键入任何内容,则机器人询问第二个Question 2

但如果用户未键入任何内容,则机器人将等待timeout = 30 seconds并告诉Expired并询问下一个问题。因此,如何使漫游器停止运行,然后在用户未输入任何内容时告诉Expired

@bot.command(pass_context=True)
async def ping(ctx):
    await bot.send_message(ctx.message.channel, "Question 1")
    answer1 = await bot.wait_for_message(timeout= 30, author=ctx.message.author, channel=ctx.message.channel)
    if answer1 is None:
        await bot.send_message(ctx.message.channel, "Expired.")

    await bot.send_message(ctx.message.channel, "Question 2")
    answer2 = await bot.wait_for_message(timeout= 30, author=ctx.message.author, channel=ctx.message.channel)
    if answer2 is None:
        await bot.send_message(ctx.message.channel, "Expired.")

1 个答案:

答案 0 :(得分:1)

您可以像常规函数一样从协程中return

@bot.command(pass_context=True)
async def ping(ctx):
    await bot.send_message(ctx.message.channel, "Question 1")
    answer1 = await bot.wait_for_message(timeout= 30, author=ctx.message.author, channel=ctx.message.channel)
    if answer1 is None:
        await bot.send_message(ctx.message.channel, "Expired.")
        return
    await bot.send_message(ctx.message.channel, "Question 2")
    answer2 = await bot.wait_for_message(timeout= 30, author=ctx.message.author, channel=ctx.message.channel)
    if answer2 is None:
        await bot.send_message(ctx.message.channel, "Expired.")
        return