Getting started
Install the module and run a bot with a slash command and a button.
Your first bot answers /hello with a button, and the click updates the message. Thirty lines, no View, no CommandTree.
Installation#
pip install levraiberet-discord-interactionsThe package is imported as discord_interactions. Requires Python 3.9 and discord.py 2.6 or later.
The complete bot#
import os import discord from discord_interactions import InteractionRouter, create_action_row, create_button, update_messagefrom discord_interactions.slash import SlashManager client = discord.Client(intents=discord.Intents.default())slash = SlashManager(client)router = InteractionRouter() @slash.command(description="Says hello")async def hello(ctx): button = create_button("wave", "Wave back", style="success") await ctx.respond(f"Hello {ctx.user.mention}!", components=[create_action_row(button)]) @router.on("wave")async def wave(interaction): await update_message(interaction, content="Hello to you too", components=[]) @client.eventasync def on_interaction(interaction): await router.handle(interaction) client.run(os.environ["DISCORD_BOT_TOKEN"])Run it:
DISCORD_BOT_TOKEN=your_token python bot.pyWhat happens#
SlashManagerregisters/helloand syncs it with Discord on startup.ctx.respondsends the response through the API directly, without discord.py views.- The button carries the
custom_idwave. On click,router.handlecalls the function declared with@router.on("wave"). update_messageedits the original message instead of sending a new one.
Module layout#
| Import | Contents |
|---|---|
discord_interactions |
Components, builders, modals, sending messages, router |
discord_interactions.slash |
Slash commands, options, permissions, cooldowns |
Everything you need is imported from these two entry points.
Next#
The guides follow the order of a real bot: components, components V2, modals, slash commands, then routing and message updates. The reference covers every function.