如何通过 BucketType 定义命令错误消息

How to define commands error messages by BucketType

我对一个命令使用 max_concurrency 两次,以限制每个公会最多调用 3 次,每个用户最多调用一次:

@commands.max_concurrency(3,per=commands.BucketType.guild,wait=False)
@commands.max_concurrency(1,per=commands.BucketType.user,wait=False)
async def start(self, ctx):

我的问题是如何使用 BucketType 参数创建单独的错误消息。

作为我要问的例子:

@start.error
async def on_command_error(self,ctx,error):

        if isinstance(error, commands.MaxConcurrencyReached):

              guild_error = "Too many playing"
              user_error = "You're already playing"

              await ctx.send("guild_error or user_error depending on BucketType?")

通过使用 MaxConcurrencyReached.per 属性,我们可以找到 BucketType 并采取相应的行动。

例如:

@start.error
async def on_command_error(self,ctx,error):

        if isinstance(error, commands.MaxConcurrencyReached):

              guild_error = "Too many playing"
              user_error = "You're already playing"
              # Because error.per returns a BucketType, we can check it against BucketType.user and BucketType.guild.
              if error.per == commands.BucketType.user:
                  await ctx.send(user_error)
              elif error.per == commands.BucketType.guild:
                  await ctx.send(guild_error)