discord-interactions.py

Cooldowns and permissions

Rate-limit commands and control who can use them, and where.

Decorators go under @slash.command() and also apply to group subcommands.

Cooldowns#

@slash.command(description="Reward")@cooldown(1, 30)async def work(ctx): ...

@cooldown(rate, per) allows rate uses every per seconds. The default scope is the user; type= changes it.

Scope Decorator
User @user_cooldown(1, 5)
Member (user within a guild) @member_cooldown(1, 5)
Guild @guild_cooldown(1, 60)
Channel @channel_cooldown(1, 10)
Everyone @global_cooldown(10, 60)

Shared and dynamic#

economy = SharedCooldown("economy", rate=5, per=60)  @slash.command(description="Balance")@economyasync def balance(ctx): ...  @slash.command(description="Generation")@DynamicCooldown(rate=1, per_func=lambda i: 5 if is_premium(i.user.id) else 30)async def generate(ctx): ...

When a cooldown blocks, CommandOnCooldown is raised. The default message shows the wait; customize it with @slash.on_error.

@slash.on_errorasync def errors(ctx, error):    if isinstance(error, CommandOnCooldown):        await ctx.respond(f"Wait {error.retry_after:.0f} more seconds.", ephemeral=True)        return True    return False

Permissions#

Decorator Effect
@default_permissions(manage_messages=True) Hides the command in Discord from members without the permission
@has_permissions(...) Checks the user's permissions in the channel
@bot_has_permissions(...) Checks the bot's permissions in the channel
@guild_only() Guilds only
@owner_only() Bot owners, read from the application
@require_roles(id1, id2) One of the roles, or all with require_all=True
@check(function) Custom async check

Checked permissions are the channel's effective permissions, computed by Discord, overwrites included.

User installs#

A user-installable command works everywhere: guilds, DMs with the bot and group chats.

@slash.command(description="Private note", ephemeral=True)@user_installable()async def note(ctx): ...

Also enable “User Install” in the Installation tab of the developer portal. For finer control, combine @integration_types(...) and @contexts(...).

Type a name: create_button, ModalBuilder, cooldown.