Saltar a contenido

Inicio rápido

Bostie escribe código en una terminal

Esta página crea una aplicación de Discord, la agrega a un servidor y ejecuta un bot con un comando slash. Da por hecho que bostie ya está instalado.

¿Usas un asistente de programación con IA?

Instala la skill de bostie para que tu asistente de programación trabaje mejor con bostie. Copia el prompt y pégalo en tu asistente de programación. Más sobre la skill

Instalar la skill de bostie
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/es/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.

Crear la aplicación

  1. Abre el Developer Portal y crea una aplicación.
  2. En su página Bot, restablece el token y cópialo. Cualquiera que tenga el token controla el bot, así que mantenlo fuera de tu código y de tu repositorio.
  3. En su página Installation, agrega los scopes applications.commands y bot a la instalación en servidor (Guild Install), luego abre el enlace de instalación y agrega la aplicación a un servidor que administres.

Escribir el bot

Guarda esto como 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 convierte la función en el comando slash /hello. Su nombre es el nombre de la función y su descripción es la primera línea del docstring. ctx es el Context de un uso del comando: quién lo ejecutó, dónde, y los métodos para responder.

Ejecutarlo

Pasa el token con la variable de entorno BOSTIE_TOKEN y ejecuta el archivo:

export BOSTIE_TOKEN="your token"
python bot.py

bot.run() configura el logging en INFO, se conecta y compara los comandos que definiste con los registrados en Discord. El primer arranque registra /hello y escribe en el log commands synced (global): added ['hello'], ...; los arranques siguientes no envían nada a menos que un comando haya cambiado. Escribe /hello en tu servidor y el bot responde.

Ctrl+C, o un SIGTERM de un gestor de procesos, cierra el bot de forma limpia.

Agregar una opción

Los parámetros que van después de ctx se convierten en las opciones del comando, con el tipo que indican sus anotaciones:

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 es una opción entera de 2 a 100 que vale 6 por defecto, así que Discord la muestra como opcional y rechaza los valores fuera del rango antes de que el bot los vea. Reinicia el bot y enviará el comando modificado.

Siguiente paso

  • Comandos slash cubre cada tipo de opción, las choices, el autocompletado y los subcomandos.
  • Responder a interacciones explica respond, las respuestas diferidas, los mensajes de seguimiento y las respuestas efímeras.
  • Registrar comandos muestra cómo registrar los comandos en un servidor de pruebas mientras trabajas en ellos.
  • Ejecutar un bot cubre el token, el logging, el ciclo de vida del bot y cómo ejecutarlo dentro de otro programa.