Discord 机器人构建器
v1.0.0为discord.py、discord.js和Serenity(Rust)跨平台部署和维护Discord bots。生成项目骨架,注册slash命令和子命令,部署到生产环境,并维护Discord bots的生命周期。使用API、CLI和GitHub等工具简化开发流程。
运行时依赖
安装命令
点击复制技能文档
Discord Bot Builder 使用合适的库、意图、托管和可观察性构建、发布和维护 Discord 机器人 —— 而不必担心通常的陷阱(令牌泄漏、缺少意图、速率限制风暴、突袭漏洞)。涵盖 discord.py、discord.js 和 Serenity(Rust)。针对社区运营团队、服务器管理员自动化管理和爱好者创作者构建功能机器人。
使用此技能时,您需要构建、扩展或修复 Discord 机器人。基本调用:构建一个带有 /ticket 命令的 Discord 机器人,该命令可以打开一个私人线程。为我的 discord.js 机器人添加反应角色 —— 用表情符号反应并获得角色。将我的 discord.py 1.7 机器人迁移到 2.x 并添加斜杠命令。我的机器人被速率限制,帮助我添加分桶和重试。在上下文中:这是我的当前 bot.py,添加一个用于封禁/踢出/暂停的管理日志频道。我需要一个使用 Serenity 的 Rust 机器人,可以处理 500 个公会并跟踪消息计数。我的机器人在本地工作,但 Railway 部署崩溃 —— 读取日志并修复它。选择一个主机:我想要每月低于 10 美元、99% 的正常运行时间、易于访问日志的主机。
代理决定架构、搭建项目、编写命令和处理程序、连接持久性并提供部署计划。
工作原理 步骤 1:机器人类型决策 在编写任何代码之前,代理决定这是什么类型的机器人。该决策驱动意图、托管和库选择。 机器人类型描述所需意图库甜蜜点 仅斜杠命令所有交互都通过 /commands 进行。无消息读取。默认(无特权)任何库;最便宜的托管 消息监听器对普通消息做出反应(自动管理、级别、关键字触发器)MESSAGE_CONTENT(特权)discord.py / discord.js 混合斜杠命令 + 选择性消息处理MESSAGE_CONTENT 仅在需要时discord.py / discord.js 语音机器人音乐、TTS、录音GUILD_VOICE_STATES + MESSAGE_CONTENTdiscord.js + lavalink 或 Serenity 模块 / 审计封禁、静音、审计日志GUILDS、GUILD_MEMBERS(特权)、GUILD_MODERATIONdiscord.py 欢迎 / 离开入职、加入角色任何库 存在感知状态基于功能(罕见、昂贵)GUILD_PRESENCES(特权、难以验证)除非必要,否则避免 决策流程:机器人是否需要读取消息内容?否 -> 仅斜杠命令。跳过 MESSAGE_CONTENT 意图。75 个公会轻松验证。是 -> 是否会扩展到 100 个公会?否 -> MESSAGE_CONTENT 在不需要验证的情况下有效(少于 100 个公会)。是 ->申请验证 + MESSAGE_CONTENT 意图批准(Discord 可能会拒绝)。考虑重写为仅斜杠命令以避免特权意图。
步骤 2:项目搭建 代理生成一个与所选库相匹配的项目骨架。 discord.py(Python 3.11+,discord.py 2.4+):
# bot.py
导入 os
导入 logging
导入 discord
从 discord.ext 导入 commands
从 dotenv 导入 load_dotenv
load_dotenv()
logging.basicConfig(level=logging.INFO, format="%(asctime)s %(levelname)s %(name)s: %(message)s")
intents = discord.Intents.default()
intents.message_content = False # 仅在需要时翻转为 True
类 MyBot(commands.Bot):
def __init__(self):
super().__init__(command_prefix="!", intents=intents)
异步 def setup_hook(self):
等待 self.load_extension("cogs.tickets")
等待 self.load_extension("cogs.moderation")
等待 self.tree.sync() # 全局同步斜杠命令 (~1 小时传播)
异步 def on_ready(self):
logging.info(f"已登录为 {self.user} ({self.user.id})")
bot = MyBot()
bot.run(os.environ["DISCORD_TOKEN"])
项目/
bot.py
cogs/
tickets.py
moderation.py
db/
schema.sql
.env
# DISCORD_TOKEN=... (gitignored)
requirements.txt
# discord.py>=2.4, python-dotenv, aiosqlite
Dockerfile
README.md
discord.js(Node 20+,discord.js v14+):
javascript
// index.js
导入 'dotenv/config';
导入 { Client, GatewayIntentBits, Collection, Events } 从 'discord.js' 导入;
导入 { readdirSync } 从 'node:fs' 导入;
导入 { fileURLToPath, pathToFileURL } 从 'node:url' 导入;
导入 { dirname, join } 从 'node:path' 导入;
const client = new Client({
intents: [GatewayIntentBits.Guilds, GatewayIntentBits.GuildMembers],
});
client.commands = new Collection();
const __dirname = dirname(fileURLToPath(import.meta.url));
for (const file of readdirSync(join(__dirname, 'commands'))) {
const mod = await import(pathToFileURL(join(__dirname, 'commands', file)).href);
client.commands.set(mod.data.name, mod);
}
client.on(Events.InteractionCreate, async (i) => {
if (!i.isChatInputCommand()) return;
const cmd = client.commands.get(i.commandName);
if (!cmd) return;
try {
await cmd.execute(i);
} catch (e) {
console.error(e);
await i.reply({ content: '错误。', ephemeral: true });
}
});
client.login(process.env.DISCORD_TOKEN);
项目/
index.js
commands/
ticket.js
ban.js
deploy-commands.js
# 通过 REST 注册斜杠命令