Zum Inhalt

Schnellstart

Bostie tippt Code in ein Terminal

Auf dieser Seite erstellst du eine Discord-Anwendung, fügst sie einem Server hinzu und startest einen Bot mit einem Slash-Befehl. Sie setzt voraus, dass bostie installiert ist.

Nutzt du einen KI-Coding-Assistenten?

Installiere den bostie-Skill, damit dein Coding-Assistent bostie-Aufgaben besser löst. Kopiere den Prompt und füge ihn in deinen Coding-Assistenten ein. Mehr über den Skill

Den bostie-Skill installieren
Install the bostie skill for my coding agent so it writes correct bostie code: Discord bots with slash commands, components, events, REST calls, voice and tests.

Use the Agent Skills installer with the bostie repository, https://github.com/nicokim/bostie:

    npx skills add nicokim/bostie --skill bostie --yes

If I ask for a global install instead, use:

    npx skills add nicokim/bostie --skill bostie --yes --global

Detect which agent or editor I am using. If it is one of these and I prefer the plugin path, install the bostie plugin instead, as https://bostie.dev/de/assistants/ shows:

    Claude Code: /plugin marketplace add nicokim/bostie, then /plugin install bostie@bostie
    Codex CLI: codex plugin marketplace add nicokim/bostie, then codex plugin add bostie@bostie
    GitHub Copilot CLI: copilot plugin marketplace add nicokim/bostie, then copilot plugin install bostie@bostie
    VS Code with GitHub Copilot: run "Chat: Install Plugin from Source" and enter nicokim/bostie
    Devin: devin plugins install nicokim/bostie#docs
    Factory Droid: droid plugin marketplace add nicokim/bostie, then droid plugin install bostie@bostie --scope user
    Qwen Code: qwen extensions install nicokim/bostie:bostie

Do not invent other skill names or install URLs.

Then add this line to the instructions file you read at the start of every session (AGENTS.md at the project root, or your global one for a global install), and create the file if needed:

    Before you write or change code that uses bostie, look at how the project is laid out, then load the bostie skill.

After installing, confirm that the bostie skill is available to you.

Die Anwendung erstellen

  1. Öffne das Developer Portal und erstelle eine Anwendung.
  2. Setze auf ihrer Seite Bot das Token zurück und kopiere es. Wer das Token hat, steuert den Bot, also halte es aus deinem Code und deinem Repository heraus.
  3. Füge auf ihrer Seite Installation unter Guild Install die Scopes applications.commands und bot hinzu, öffne dann den Installationslink und füge die App einem Server hinzu, den du verwaltest.

Den Bot schreiben

Speichere das als bot.py:

import bostie
from bostie import Context

bot = bostie.Bot()


@bot.command
async def hello(ctx: Context) -> None:
    """Say hello."""
    await ctx.respond(f"Hello, {ctx.user.display_name}!")


bot.run()

@bot.command macht aus der Funktion den Slash-Befehl /hello. Sein Name ist der Name der Funktion, seine Beschreibung die erste Zeile des Docstrings. ctx ist der Context einer einzelnen Verwendung des Befehls: wer ihn ausgeführt hat, wo, und die Methoden, mit denen du antwortest.

Starten

Übergib das Token über die Umgebungsvariable BOSTIE_TOKEN und starte die Datei:

export BOSTIE_TOKEN="your token"
python bot.py

bot.run() richtet das Logging auf INFO ein, verbindet sich und vergleicht die Befehle, die du definiert hast, mit denen, die bei Discord registriert sind. Der erste Start registriert /hello und loggt commands synced (global): added ['hello'], ...; spätere Starts senden nichts, solange sich kein Befehl geändert hat. Tippe /hello in deinem Server, und der Bot antwortet.

Strg+C oder ein SIGTERM von einem Prozessmanager beendet den Bot sauber.

Eine Option hinzufügen

Parameter nach ctx werden zu den Optionen des Befehls, typisiert durch ihre Type Hints:

import secrets

import bostie
from bostie import Context, Range

bot = bostie.Bot()


@bot.command
async def roll(ctx: Context, sides: Range[int, 2, 100] = 6) -> None:
    """Roll a die."""
    rolled = secrets.randbelow(sides) + 1
    await ctx.respond(f"You rolled {rolled} on a d{sides}.")


bot.run()

sides ist eine Ganzzahl-Option von 2 bis 100 mit dem Standardwert 6. Discord zeigt sie deshalb als optional an und lehnt Werte außerhalb des Bereichs ab, bevor der Bot sie sieht. Starte den Bot neu, und er sendet den geänderten Befehl.

Weiter

  • Slash-Befehle behandelt jeden Optionstyp, Auswahlmöglichkeiten, Autovervollständigung und Unterbefehle.
  • Auf Interaktionen antworten erklärt respond, das Aufschieben, Folgenachrichten und ephemere Antworten.
  • Befehle registrieren zeigt, wie du Befehle in einem einzelnen Testserver registrierst, solange du an ihnen arbeitest.
  • Einen Bot betreiben behandelt das Token, das Logging, den Lebenszyklus des Bots und wie du ihn innerhalb eines anderen Programms laufen lässt.