跳转至

快速入门

Bostie 在终端里敲代码

本页会创建一个 Discord 应用,把它添加到服务器,并运行一个带有一条斜杠命令的机器人。这里假设你已安装好 bostie。

在用 AI 编程助手?

安装 bostie 技能,让你的编程助手更擅长处理 bostie 相关任务。复制下面的提示词,粘贴到你的编程助手中即可。 进一步了解该技能

安装 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/zh/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.

创建应用

  1. 打开 Developer Portal,创建一个应用。
  2. 在它的 Bot 页面上重置令牌并复制下来。任何拿到令牌的人都能控制这个机器人,所以不要把它写进代码或提交到仓库。
  3. 在它的 Installation 页面上,为服务器安装添加 applications.commands 和 bot 两个授权范围,然后打开安装链接,把应用添加到你管理的某个服务器。

编写机器人

把下面的代码保存为 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 把这个函数变成斜杠命令 /hello。命令名就是函数名,描述是文档字符串的第一行。ctx 是该命令某一次使用的 Context:包括谁执行了命令、在哪里执行,以及用来回应的方法。

运行

通过 BOSTIE_TOKEN 环境变量传入令牌,然后运行这个文件:

export BOSTIE_TOKEN="your token"
python bot.py

bot.run() 会把日志级别设为 INFO,建立连接,并将你定义的命令与 Discord 上已注册的命令进行比较。第一次启动会注册 /hello 并输出日志 commands synced (global): added ['hello'], ...;之后的启动只有在命令有变化时才会发送内容。在你的服务器里输入 /hello,机器人就会回应。

按 Ctrl+C,或由进程管理器发送 SIGTERM,都会干净地关闭机器人。

添加选项

ctx 之后的参数会成为命令的选项,类型由类型提示决定:

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 是一个取值 2 到 100、默认值为 6 的整数选项,因此 Discord 会把它显示为可选项,并在机器人收到之前就拒绝超出范围的值。重启机器人,它就会发送修改后的命令。

下一步

  • 斜杠命令介绍了所有选项类型、候选项、自动补全和子命令。
  • 回应交互讲解了 respond、延迟回应、后续消息和仅自己可见的回应。
  • 注册命令展示了开发期间如何只在一个测试服务器中注册命令。
  • 运行机器人介绍了令牌、日志、机器人的生命周期,以及如何在其他程序中运行它。