【问题标题】:How to define commands error messages by BucketType如何通过 BucketType 定义命令错误消息
【发布时间】:2021-03-13 23:59:27
【问题描述】:

我在一个命令中使用了两次 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?")

【问题讨论】:

    标签: python error-handling discord discord.py discord.py-rewrite


    【解决方案1】:

    通过使用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)
                 
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-06-20
      • 2018-10-21
      • 2015-11-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多