Skip to content Skip to sidebar Skip to footer

Make The Same Cooldown For Multiple Discord.py Bot Commands?

This is written in discord.py. I have multiple commands similar to the following: @bot.command(name ='hi') async def hi(ctx): link = ['https://google.com', 'https://youtube.com

Solution 1:

This is how the cooldowns decorator is defined in the code:

def cooldown(rate, per, type=BucketType.default):
    def decorator(func):if isinstance(func, Command):func._buckets = CooldownMapping(Cooldown(rate, per, type))
        else:
            func.__commands_cooldown__ = Cooldown(rate, per, type)
        returnfuncreturn decorator

We can modify this so that we only create one Cooldown object that gets shared between the commands:

def shared_cooldown(rate, per, type=BucketType.default):
    cooldown = Cooldown(rate, per, type=type)
    def decorator(func):if isinstance(func, Command):func._buckets = CooldownMapping(cooldown)
        else:
            func.__commands_cooldown__ = cooldown
        returnfuncreturn decorator

We would use this by calling it to get the decorator that we then apply to the commands:

my_cooldown = shared_cooldown(1, 600, commands.BucketType.user)

@bot.command()@my_cooldownasyncdefhi(ctx):
    await ctx.send("Hi")


@bot.command()@my_cooldownasyncdefbye(ctx):
    await ctx.send("Bye")

Post a Comment for "Make The Same Cooldown For Multiple Discord.py Bot Commands?"