bostie¶
bostie 是一个 Python 的 Discord 库,它的网关、HTTP 客户端和语音引擎都运行在 Rust 中。你用带类型注解的异步 Python 编写机器人。

Pre-alpha 阶段
bostie 的 API 仍可能变化。斜杠命令、用户命令和消息命令、组件、模态框、网关事件、缓存、REST、Webhook、HTTP 交互、OAuth2,以及语音的播放与接收,目前都已可用。
import bostie
from bostie import ComponentContext, Context, ui, voice
bot = bostie.Bot()
@bot.command
async def play(ctx: Context, url: str) -> None:
"""Play a direct media URL in your voice channel."""
vc = ctx.voice.current or await ctx.voice.connect()
await vc.play(voice.Url(url))
stop = ui.button("Stop", custom_id=stop_playing.id(guild_id=vc.guild_id))
await ctx.respond(components=[ui.text(f"Playing <{url}>"), ui.row(stop)])
@bot.on_component("stop:{guild_id:int}")
async def stop_playing(ctx: ComponentContext, guild_id: int) -> None:
if (vc := bot.voice.get(guild_id)) is not None:
vc.stop()
await ctx.update(components=[ui.text("Stopped.")])
bot.run()
/play 会加入执行该命令的用户所在的语音频道,以流式方式播放 URL,并回应一个 Stop 按钮。按钮的自定义 ID 中带有服务器 ID,@bot.on_component 会把它解析回 int,因此机器人重启后按钮依然有效。bot.run() 从 BOSTIE_TOKEN 环境变量读取令牌,并在命令有变化时把它发送给 Discord。
在用 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.
它为什么而生¶
语音¶
自 2026 年 3 月 1 日起,Discord 只接受端到端加密的通话(DAVE)。bostie 的语音引擎运行在 GIL 之外的 Rust 线程上:20 ms 时钟、解码、混音、Opus 编码、传输加密和 DAVE 都在那里完成。Python 只需保持每个 sink 的缓冲区有数据,默认是 0.2 秒的音频。bostie 可以播放文件、URL、HLS 流以及你自己的 PCM 或 Opus 数据,能混合多个音源,单独接收每位说话者的声音,还能录制通话。
经得起 Discord 变更的机器人¶
未知的字段、枚举值、组件类型和交互类型都不会导致解码失败。每个事件,以及每个从完整载荷解码出的模型,都会保留原始 JSON;bot.http 和原始事件监听器可以触及 bostie 尚未建模的内容。
与 Discord 文档一致的类型¶
Discord 总会发送的字段绝不会是 None,部分对象也有自己的类:从命令选项解析出的成员是 ResolvedMember,它没有 deaf 和 mute。
由函数签名生成命令¶
选项、候选项、取值范围和自动补全都来自类型提示。名称、描述、选项数量和候选项会在装饰器运行时就按 Discord 的限制进行检查,而不是等到 Discord 拒绝同步时才发现。
无需 Discord 的测试¶
bostie.testing.FakeDiscord 是一个本地的网关和 REST API,可以让你真实的机器人对着它运行。