【问题标题】:How to use max_concurrency() in Discord.py bot如何在 Discord.py 机器人中使用 max_concurrency()
【发布时间】:2020-12-21 04:41:22
【问题描述】:

我有这段代码:

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 异常句柄,但它根本没有工作,有人知道如何使用此命令吗?

【问题讨论】:

    标签: python discord discord.py


    【解决方案1】:

    我想通了。

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

    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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-05-03
      • 1970-01-01
      • 2021-02-13
      • 1970-01-01
      • 2021-02-12
      • 2020-09-27
      • 2020-12-12
      相关资源
      最近更新 更多