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

2024-05-18 07:12:59 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一段代码:

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()可以工作,但当满足最大并发性时,我希望它向作者发送一条消息,说明bot正忙,我尝试了MaxConcurrencyReached异常句柄,但它根本不工作,有人知道如何使用此命令吗


Tags: 代码fromimportfalsedefaultbotextmax
1条回答
网友
1楼 · 发布于 2024-05-18 07:12:59

我想出来了

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

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

相关问题 更多 >