From b7b992375d6e9338ae93c4383c6abfb9b8a0fa64 Mon Sep 17 00:00:00 2001 From: Rapptz Date: Tue, 29 Mar 2022 06:00:20 -0400 Subject: [PATCH] Add examples for new cooldown checks --- discord/app_commands/checks.py | 39 ++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) diff --git a/discord/app_commands/checks.py b/discord/app_commands/checks.py index 54c88516c..7b7d45041 100644 --- a/discord/app_commands/checks.py +++ b/discord/app_commands/checks.py @@ -432,6 +432,23 @@ def cooldown( If a cooldown is triggered, then :exc:`~discord.app_commands.CommandOnCooldown` is raised to the error handlers. + Examples + --------- + + Setting a one per 5 seconds per member cooldown on a command: + + .. code-block:: python3 + + @tree.command() + @app_commands.checks.cooldown(1, 5.0, key=lambda i: (i.guild_id, i.user.id)) + async def test(interaction: discord.Interaction): + await interaction.response.send_message('Hello') + + @test.error + async def on_test_error(interaction: discord.Interaction, error: app_commands.AppCommandError): + if isinstance(error, app_commands.CommandOnCooldown): + await interaction.response.send_message(str(error), ephemeral=True) + Parameters ------------ rate: :class:`int` @@ -480,6 +497,28 @@ def dynamic_cooldown( If a cooldown is triggered, then :exc:`~discord.app_commands.CommandOnCooldown` is raised to the error handlers. + Examples + --------- + + Setting a cooldown for everyone but the owner. + + .. code-block:: python3 + + def cooldown_for_everyone_but_me(interaction: discord.Interaction) -> Optional[app_commands.Cooldown]: + if interaction.user.id == 80088516616269824: + return None + return app_commands.Cooldown(1, 10.0) + + @tree.command() + @app_commands.checks.dynamic_cooldown(cooldown_for_everyone_but_me) + async def test(interaction: discord.Interaction): + await interaction.response.send_message('Hello') + + @test.error + async def on_test_error(interaction: discord.Interaction, error: app_commands.AppCommandError): + if isinstance(error, app_commands.CommandOnCooldown): + await interaction.response.send_message(str(error), ephemeral=True) + Parameters ------------ factory: Optional[Callable[[:class:`discord.Interaction`], Optional[:class:`~discord.app_commands.Cooldown`]]]