discord-interactions.py

SlashCommandManager

Class

Entry point for slash commands: declaration, execution, autocomplete and sync. Alias: SlashManager.

from discord_interactions import SlashCommandManager

Aliases SlashManager

Constructor

SlashCommandManager(    client: discord.Client,    *,    owner_ids: Set[int] | None = None,    auto_sync: bool = True,    sync_on_ready: bool = True,    default_guilds: List[int] | None = None,)
NameTypeDefaultDescription
clientdiscord.Clientrequireddiscord.Client or commands.Bot.
owner_idsSet[int] | NoneNoneOwners for @owner_only(); read from the application otherwise.
auto_syncboolTrueSync commands on startup.
sync_on_readyboolTrueSync on the ready event.
default_guildsList[int] | NoneNoneGuilds to sync to (instant) instead of globally.

Methods

find_command

def find_command(    data: Dict[str, Any],) -> Tuple[SlashCommand | None, List[Dict[str, Any]]]

command

def command(    name: str | None = None,    description: str = 'No description',    *,    options: List[OptionBase] | None = None,    guild_ids: List[int] | None = None,    auto_defer: bool = False,    defer_ephemeral: bool = False,    ephemeral: bool = False,    name_localizations: Dict[str, str] | None = None,    description_localizations: Dict[str, str] | None = None,    **kwargs: Any,) -> Callable[[~T], ~T]

Decorator: declares a command.

group

def group(    name: str,    description: str = 'No description',    *,    guild_ids: List[int] | None = None,    **kwargs: Any,) -> SlashGroup

Creates a subcommand group.

add_middleware

def add_middleware(middleware: Middleware) -> None

Adds a ready-made middleware.

remove_middleware

def remove_middleware(name: str) -> bool

middleware

def middleware(    name: str | None = None,    *,    priority: int = 100,    commands: List[str] | None = None,    exclude: List[str] | None = None,) -> Callable

Decorator: before, after or error middleware, depending on the signature.

on_error

def on_error(func: Callable) -> Callable

Decorator: error handler. Returning False lets the default handling run.

autocomplete

def autocomplete(command_name: str, option_name: str) -> Callable

Decorator: autocomplete for an option.

build_payload

def build_payload() -> List[Dict[str, Any]]

Commands in API format, without sending anything.

sync

async def sync(guilds: List[int] | None = None) -> Dict[int | None, Any]

Replaces the commands on Discord, globally or per guild.

get_command

def get_command(name: str) -> SlashCommand | None

get_all_commands

def get_all_commands() -> List[SlashCommand]

get_group

def get_group(name: str) -> SlashGroup | None

remove_command

def remove_command(name: str) -> bool

Example

import discordfrom discord_interactions.slash import SlashManager client = discord.Client(intents=discord.Intents.default())slash = SlashManager(client, default_guilds=[123456789012345678]) @slash.command(description="Latence du bot")async def ping(ctx):    await ctx.respond(f"Pong ! {ctx.bot.latency * 1000:.0f} ms") client.run(TOKEN)

Details

client = discord.Client(intents=discord.Intents.default()) slash = SlashManager(client)

@slash.command(description="Latence du bot") async def ping(ctx):

await ctx.respond(f"Pong ! {ctx.bot.latency * 1000:.0f} ms")

Type a name: create_button, ModalBuilder, cooldown.