# Discord bot: discord-interactions-py

This project builds Discord interactions with `discord-interactions-py` (package `levraiberet-discord-interactions`, import `discord_interactions`, version 0.3.0).

Before writing or changing Discord code, read the docs instead of guessing the API:
- Compact, every signature: https://discordpy.levraiberet.ovh/llms-small.txt
- Full, guides and examples: https://discordpy.levraiberet.ovh/llms-full.txt
- One symbol: https://discordpy.levraiberet.ovh/docs/en/0.3.0/api/<name>/index.md

## Rules

1. Install `pip install levraiberet-discord-interactions`, import `discord_interactions` (slash commands: `discord_interactions.slash`). Python 3.9+, discord.py 2.6+.
2. Do NOT use `discord.ui.View`, `discord.ui.Button`, `discord.ui.Modal` or `app_commands`: this module replaces them. Components are plain dicts built by `create_*` functions or builders (`container()`, `section()`, `ModalBuilder`).
3. One entry point for components: `router = InteractionRouter()` and `@client.event async def on_interaction(interaction): await router.handle(interaction)`. Slash commands are handled by `slash = SlashManager(client)` on its own (auto-synced on ready).
4. Routing uses the `base_id`, the part of `custom_id` before `:`. `@router.on("id")` for buttons and selects, `@router.modal("id")` for modals. Handler signature: `(interaction)`, `(interaction, data)` or `(interaction, data, base_id)`; for a modal, `data` holds the submitted values.
5. `create_button("id", "Label", data={...})` stores data in the custom_id (100 characters max in total) and it comes back as `data`. Use `create_signed_button` / `parse_signed_custom_id` when the data must not be forged.
6. Every interaction must be answered within 3 seconds, exactly once: `respond_to_interaction`, `update_message` (edits the clicked message), `send_modal`, or `defer_interaction` then `send_followup` / `edit_original_response`. `respond()` and `ctx.respond()` switch to a followup automatically.
7. A modal must be the first response (never after a defer). 5 components max per modal. Read values with `values["id"]`: `str` for text and radio, `bool` for a checkbox, `list` for selects, checkbox groups and uploads.
8. Select values are always a list. Users, roles, channels: `parse_values(interaction).users("id")`, `.roles("id")`, `.channels("id")`, `.mentionables("id")`.
9. Components V2 (`container`, `section`, `mediaGallery`, `separator`, `textDisplay`) cannot be mixed with embeds. The IS_COMPONENTS_V2 flag is added automatically and `content` is turned into a text display.
10. Discord limits: 5 buttons per row, 5 rows (V1), 25 select options, 80-character button labels, unique custom_id per message.

## Minimal bot

```python
import os

import discord
from discord_interactions import InteractionRouter, create_action_row, create_button, update_message
from discord_interactions.slash import SlashManager

client = discord.Client(intents=discord.Intents.default())
slash = SlashManager(client)  # slash commands, synced automatically on ready
router = InteractionRouter()  # buttons, selects and modals


@slash.command(description="Say hello")
async def hello(ctx):
    button = create_button("wave", "Wave back", data={"from": ctx.user.id})
    await ctx.respond(f"Hello {ctx.user.mention}!", components=[create_action_row(button)])


@router.on("wave")
async def wave(interaction, data):
    await update_message(interaction, content=f"<@{data['from']}> got a wave!", components=[])


@client.event
async def on_interaction(interaction):
    await router.handle(interaction)


client.run(os.environ["DISCORD_BOT_TOKEN"])
```
