如何在Discord.py bot中使用max_concurrency()

时间:2020-09-02 03:31:18

标签: python discord discord.py

我有这段代码:

from discord.ext import commands

@bot.command()
@commands.max_concurrency(1,per=commands.BucketType.default,wait=False)
async def function(ctx,arg1,arg2,arg3):

max_concurrency()可以工作,但是当满足最大并发性时,我希望它向作者发送一条消息,指出该机器人很忙,我已经尝试了MaxConcurrencyReached异常句柄,但是它根本不起作用,有人知道如何使用此命令吗?

1 个答案:

答案 0 :(得分:3)

我知道了。

定义函数后,我要设置最大并发限制:

from discord.ext import commands

@bot.command()
@commands.max_concurrency(1,per=commands.BucketType.default,wait=False)
async def function(ctx,arg1,arg2,arg3):

然后,您必须使用on_command_error()isinstance()处理它。例如:

@bot.event
async def on_command_error(ctx,error):
    await ctx.message.delete()
    if isinstance(error, commands.MaxConcurrencyReached):
        await ctx.author.send('Bot is busy! Please retry in a minute')
        return