当我们使用!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.")
答案 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