discord-interactions.py

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-interactions

The package is imported as discord_interactions. Requires Python 3.9 and discord.py 2.6 or later.

The complete bot#

bot.py
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.py

What happens#

  1. SlashManager registers /hello and syncs it with Discord on startup.
  2. ctx.respond sends the response through the API directly, without discord.py views.
  3. The button carries the custom_id wave. On click, router.handle calls the function declared with @router.on("wave").
  4. update_message edits 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.

Type a name: create_button, ModalBuilder, cooldown.