- Sao chép file
.env.examplethành.envvà điền thông tin bot của bạn:
TOKEN=your_discord_bot_token_here
CLIENT_ID=your_client_id_here
GUILD_ID=your_guild_id_here
- Cài đặt dependencies:
npm install- Đăng ký lệnh slash:
node src/deploy-commands.js- Khởi động bot:
node src/index.jsdiscord-bot-nodejs/
├── src/
│ ├── commands/ # Thư mục chứa các lệnh slash
│ ├── events/ # Thư mục chứa các event handler
│ ├── utils/ # Thư mục chứa các utility function
│ ├── index.js # File chính của bot
│ └── deploy-commands.js # Script đăng ký lệnh slash
├── .env # File chứa biến môi trường (cần tạo từ .env.example)
├── .eslintrc.json # Cấu hình ESLint
├── .prettierrc.json # Cấu hình Prettier
└── package.json # Cấu hình npm và dependencies
- Tạo file mới trong thư mục
src/commands/ - Sử dụng mẫu sau:
const { SlashCommandBuilder } = require('discord.js');
module.exports = {
data: new SlashCommandBuilder()
.setName('tên-lệnh')
.setDescription('Mô tả lệnh'),
async execute(interaction) {
// Xử lý lệnh ở đây
await interaction.reply('Phản hồi của bạn ở đây');
},
};- Sau khi thêm lệnh mới, chạy lại script đăng ký lệnh
- Tạo file mới trong thư mục
src/events/ - Sử dụng mẫu sau:
const { Events } = require('discord.js');
module.exports = {
name: Events.TênEvent,
once: false, // true nếu event chỉ nên được xử lý một lần
execute(...args) {
// Xử lý event ở đây
},
};Thêm các script sau vào package.json:
"scripts": {
"start": "node src/index.js",
"dev": "nodemon src/index.js",
"deploy": "node src/deploy-commands.js",
"lint": "eslint .",
"format": "prettier --write \"src/**/*.js\""
}