[ PROMPT_NODE_25728 ]
discord-bot-architect
[ SKILL_DOCUMENTATION ]
# Discord 机器人架构师
## 模式
### Discord.js v14 基础
使用 Discord.js v14 和斜杠命令的现代 Discord 机器人设置
**适用场景**:['使用 JavaScript/TypeScript 构建 Discord 机器人', '需要完整的网关连接和事件处理', '构建具有复杂交互的机器人']
javascript
// src/index.js
const { Client, Collection, GatewayIntentBits, Events } = require('discord.js');
const fs = require('node:fs');
const path = require('node:path');
require('dotenv').config();
// 创建具有最少必要意图的客户端
const client = new Client({
intents: [
GatewayIntentBits.Guilds,
// 仅添加你需要的:
// GatewayIntentBits.GuildMessages,
// GatewayIntentBits.MessageContent, // 特权意图 - 尽可能避免
]
});
// 加载命令
client.commands = new Collection();
const commandsPath = path.join(__dirname, 'commands');
const commandFiles = fs.readdirSync(commandsPath).filter(f => f.endsWith('.js'));
for (const file of commandFiles) {
const filePath = path.join(commandsPath, file);
const command = require(filePath);
if ('data' in command && 'execute' in command) {
client.commands.set(command.data.name, command);
}
}
// 加载事件
const eventsPath = path.join(__dirname, 'events');
const eventFiles = fs.readdirSync(eventsPath).filter(f => f.endsWith('.js'));
for (const file of eventFiles) {
const filePath = path.join(eventsPath, file);
const event = require(filePath);
if (event.once) {
client.once(event.name, (...args) => event.execute(...args));
} else {
client.on(event.name, (...args) => event.execute(...args));
}
}
client.login(process.env.DISCORD_TOKEN);
javascript
// src/commands/ping.js
const { SlashCommandBuilder } = require('discord.js');
module.exports = {
data: new SlashCommandBuilder()
.setName('ping')
.setDescription('回复 Pong!'),
async execute(interaction) {
const sent = await interaction.reply({
content: '正在 Ping...',
fetchReply: true
});
const latency = sent.createdTimestamp - interaction.createdTimestamp;
await interaction.editReply(`Pong! 延迟: ${latency}ms`);
}
};
javascript
// src/events/interactionCreate.js
const { Events } = require('discord.js');
module.exports = {
name: Event
### Pycord 机器人基础
使用 Pycord (Python) 和应用命令的 Discord 机器人
**适用场景**:['使用 Python 构建 Discord 机器人', '偏好 async/await 模式', '需要良好的斜杠命令支持']