我对一个命令使用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)