YaW*_*YaW 6 python coroutine discord discord.py
假设命令与此类似:
@bot.command()
async def test(ctx):
def check(r, u):
return u == ctx.message.author and r.message.channel == ctx.message.channel and str(r.emoji) == '?'
await ctx.send("React to this with ?")
try:
reaction, user = await bot.wait_for('reaction_add', timeout=300.0, check=check)
except asyncio.TimeoutError:
await ctx.send('Timeout')
else:
await ctx.send('Cool, thanks!')
Run Code Online (Sandbox Code Playgroud)
wait_for如果用户在实际对消息作出反应之前多次发送相同的命令,有什么办法可以取消?因此,机器人停止等待先前发送的消息的反应,而仅等待最后一条消息。
这样的事情对你有用吗?
\npending_tasks = dict()\n\nasync def test(ctx):\n def check(r, u):\n return u == ctx.message.author and r.message.channel == ctx.message.channel and str(r.emoji) == \'\xe2\x9c\x85\'\n\n await ctx.send("React to this with \xe2\x9c\x85")\n try:\n if ctx.message.author in pending_tasks:\n pending_tasks[ctx.message.author].close()\n pending_tasks[ctx.message.author] = bot.wait_for(\'reaction_add\', timeout=300.0, check=check)\n reaction, user = await pending_tasks[ctx.message.author]\n except asyncio.TimeoutError:\n await ctx.send(\'Timeout\')\n else:\n await ctx.send(\'Cool, thanks!\')\nRun Code Online (Sandbox Code Playgroud)\n您将所有待处理的请求存储在一个字典中,在创建另一个请求之前,您检查是否已经有该用户的现有任务,如果有,则取消它并创建一个新任务
\n编辑:16/05/23\n当你完成任务后,你应该从字典中删除旧任务,否则它会不必要地增长
\n| 归档时间: |
|
| 查看次数: |
65 次 |
| 最近记录: |