【问题标题】:Discord.py different cooldown per userDiscord.py 每个用户的冷却时间不同
【发布时间】:2021-06-09 08:47:57
【问题描述】:

如果用户不在列表中,我想创建一个只有冷却时间的命令 这是我的代码:

@commands.command(name='cooldown')
@commands.cooldown(1, 300, commands.BucketType.user)
async def cdown(self, ctx):
  await ctx.send("Executed")

应该是这样的:

@commands.command(name='cooldown')
if not user in no_cooldown_users:
  @commands.cooldown(1, 300, commands.BucketType.user)
async def cdown(self, ctx):
  await ctx.send("Executed")

有没有可能做这个而不是使用配置?

【问题讨论】:

    标签: python discord discord.py bots


    【解决方案1】:

    你必须自定义冷却时间,这里是一个非常简单的例子

    from discord.ext import commands
    
    class CustomCooldown:
        def __init__(self, rate, per, alter_rate, alter_per, bucket, *, elements):
            self.elements = elements
            # Default mapping is the default cooldown
            self.default_mapping = commands.CooldownMapping.from_cooldown(rate, per, bucket)
            # Alter mapping is the alternative cooldown
            self.alter_mapping = commands.CooldownMapping.from_cooldown(alter_rate, alter_per, bucket)
            # Copy of the original BucketType
            self._bucket_type = bucket
    
        def __call__(self, ctx):
            key = self.alter_mapping._bucket_key(ctx.message)
    
            if self._bucket_type is commands.BucketType.member: # `BucketType.member` returns a tuple
                key = key[1] # The second (last) value is the member ID, the first one is the guild ID
    
            if key in self.elements:
                # If the key is in the elements, the bucket will be taken from the alternative cooldown
                bucket = self.alter_mapping.get_bucket(ctx.message)
            else:
                # If not, from the default cooldown
                bucket = self.default_mapping.get_bucket(ctx.message)
    
            # Getting the ratelimit left (can be None)
            retry_after = bucket.update_rate_limit()
    
            if retry_after: # If the command is on cooldown, raising the error
                raise commands.CommandOnCooldown(bucket, retry_after)
            return True
    

    使用它:

    @command.commands()
    @commands.check(CustomCooldown(1, 300, 1, 0, commands.BucketType.user, elements=[list of ids]))
    async def foo(self, ctx):
        ...
    

    前两个值是默认冷却时间的速率和单位,接下来的两个值是“特殊”冷却时间。接下来是桶类型,最后是元素列表(id)。

    您也可以使用此方法为每个角色/公会/类别/频道提供不同的冷却时间...

    【讨论】:

      猜你喜欢
      • 2021-05-15
      • 1970-01-01
      • 2021-05-02
      • 2022-11-09
      • 2021-04-21
      • 2021-10-15
      • 2022-01-16
      • 2021-03-11
      相关资源
      最近更新 更多