Rapptz / discord.py

An API wrapper for Discord written in Python.
http://discordpy.rtfd.org/en/latest
MIT License
14.86k stars 3.76k forks source link

Button cooldown #7536

Open CaedenPH opened 2 years ago

CaedenPH commented 2 years ago

Summary

Add button cooldowns

What is the feature request for?

The core library

The Problem

By adding a cooldown decorator there can be an internal cooldown before a button can be pressed again. This replicates the commands cooldown decorator.

it would be per user and per role

@disord.ui.cooldown(1, 30, BucketType.role)
async def button(...):

This makes it a lot easier to do button checking instead of having to write some harder code inside the view itself.

The Ideal Solution

Perhaps move the cooldowns outside of ext but this would hurt the entire structure of ext

The Current Solution

Refactoring the whole lib :flushed:

Additional Context

This problem would require a large amount of lib refactoring due to the cooldowns being stuck inside ext/

imvaskel commented 2 years ago

It is possible to construct your own commands.CooldownMapping and handle this yourself. For example:

class SomeCog(commands.Cog):
    def __init__(self):
        self._cd = commands.CooldownMapping.from_cooldown(1.0, 60.0, commands.BucketType.user)

    async def cog_check(self, ctx):
        bucket = self._cd.get_bucket(ctx.message)
        retry_after = bucket.update_rate_limit()
        if retry_after:
            # you're rate limited
            # helpful message here
            pass
        # you're not rate limited

is an example on how to use cooldown mapping to apply a cog-wide cooldown, this should also be able to be applied to your use case.

Pseudohumanoid commented 2 years ago

For non cog users it will be nice to have an attribute: discord.ui.Button(label = 'timed', cooldown = 5)