#Different Cooldowns for different users

1 messages · Page 1 of 1 (latest)

hazy radish
#

So i made a class trying to do custom cooldowns if the user is in a list:

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: commands.Context):
        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


I dont know what is the issue here

timid skiff
#

so what's the issue?

#

what doesn't work?

hazy radish
timid skiff
#

so what is key?

hazy radish
timid skiff
#

I'm lost

#

It doesn't return the user id but key is the user id?

hazy radish
#

i got it from hereee

hazy radish
#

im back now

#

this is the error thrown:

Ignoring exception in on_interaction
Traceback (most recent call last):
  File "/home/container/.local/lib/python3.8/site-packages/discord/client.py", line 382, in _run_event
    await coro(*args, **kwargs)
  File "/home/container/.local/lib/python3.8/site-packages/discord/bot.py", line 1045, in on_interaction
    await self.process_application_commands(interaction)
  File "/home/container/.local/lib/python3.8/site-packages/discord/bot.py", line 737, in process_application_commands
    await self.invoke_application_command(ctx)
  File "/home/container/.local/lib/python3.8/site-packages/discord/bot.py", line 993, in invoke_application_command
    await ctx.command.invoke(ctx)
  File "/home/container/.local/lib/python3.8/site-packages/discord/commands/core.py", line 354, in invoke
    await self.prepare(ctx)
  File "/home/container/.local/lib/python3.8/site-packages/discord/commands/core.py", line 276, in prepare
    if not await self.can_run(ctx):
  File "/home/container/.local/lib/python3.8/site-packages/discord/commands/core.py", line 373, in can_run
    return await async_all(predicate(ctx) for predicate in predicates)  # type: ignore
  File "/home/container/.local/lib/python3.8/site-packages/discord/utils.py", line 562, in async_all
    for elem in gen:
  File "/home/container/.local/lib/python3.8/site-packages/discord/commands/core.py", line 373, in <genexpr>
    return await async_all(predicate(ctx) for predicate in predicates)  # type: ignore
  File "/home/container/cogs/PRIVACY.py", line 22, in __call__
    key = self.alter_mapping._bucket_key(ctx.message)
  File "/home/container/.local/lib/python3.8/site-packages/discord/ext/commands/cooldowns.py", line 229, in _bucket_key
    return self._type(msg)
  File "/home/container/.local/lib/python3.8/site-packages/discord/ext/commands/cooldowns.py", line 81, in __call__
    return self.get_key(msg)```