discord-interactions.py

Slash commands

Declare commands, their typed options, groups and autocomplete.

SlashManager declares, runs and syncs your commands. Every option is typed and validated, and users, channels, roles and files arrive as discord.py objects.

Declaring a command#

from discord_interactions.slash import Integer, SlashManager, String, option slash = SlashManager(client)  @slash.command(description="Repeats a text")@option(String("text", "Text to repeat", max_length=200))@option(Integer("times", "Repetitions", required=False, default=1, min_value=1, max_value=5))async def repeat(ctx, text, times):    await ctx.respond("\n".join([text] * times))

The command is named after the function; pass name="..." to pick another name, for example one with a dash.

Options#

Factory Received value Checks
String str length, choices, regex, autocomplete
Integer int bounds, step, choices, autocomplete
Number float bounds, rounding
Boolean bool
User discord.Member or discord.User reject bots, require voice
Channel channel channel types
Role discord.Role reject @everyone or managed roles
Mentionable member or role
Attachment discord.Attachment file types, extensions, size

Every option also accepts validator= (returns True, False or an error message) and transformer= (converts the value before the call).

@option(String("hex", "Color", pattern=r"^#[0-9A-Fa-f]{6}$", pattern_error="Expected format: #RRGGBB"))@option(Attachment("image", "Image", file_types=["image"], max_size=8_000_000))

Responding#

Method Effect
ctx.respond(...) Initial response, then an automatic followup
ctx.defer(ephemeral=...) Shows the thinking state to answer later
ctx.edit(...) Edits the original response
ctx.send_modal(modal) Opens a modal

@slash.command(ephemeral=True) makes every response ephemeral by default, and auto_defer=True acknowledges before running the function.

Groups#

config = slash.group("config", "Bot configuration")channels = config.subgroup("channels", "Bot channels")  @config.command(description="Show the configuration")async def show(ctx): ...  @channels.command(description="Log channel")@option(Channel("channel", "Channel"))async def logs(ctx, channel): ...

Result: /config show and /config channels logs. Option, cooldown and permission decorators work on subcommands.

Autocomplete#

from discord_interactions.slash import filter_choices, from_list  async def cities(ctx):    country = ctx.options.get("country")    return filter_choices([(c, c) for c in CITIES[country]], ctx.value, fuzzy=True)  @slash.command(description="Weather")@option(String("country", "Country", autocomplete=from_list(["France", "Belgium"])))@option(String("city", "City", autocomplete=cities))async def weather(ctx, country, city): ...

ctx.value holds the current input, ctx.options the other options already filled.

Sync#

Commands are synced on startup. With default_guilds=[...], they are synced to those guilds, which is instant. Without it, they are global.

Type a name: create_button, ModalBuilder, cooldown.