Discord Bot JavaScript Project Structure
JavaScript Discord bot using discord.js v14. File-based command handler, slash commands, and event modules.
Project Directory
discord-bot/
src/
Bot source code
index.js
Entry point, client setup
commands/
Slash commands
utility/
ping.js
help.js
moderation/
kick.js
ban.js
fun/
8ball.js
events/
Event handlers
ready.js
interactionCreate.js
guildMemberAdd.js
handlers/
Loaders
commands.js
Load all commands
events.js
Load all events
utils/
logger.js
embeds.js
scripts/
deploy-commands.js
Register slash commands
config.json
Bot config (not secrets)
package.json
.env.example
.gitignore
README.md
Why This Structure?
This structure uses file-based command and event handlers. Commands are auto-loaded from folders, making it easy to add new features. The handlers/ folder contains the loader logic that reads command files and registers them with the client.
Key Directories
- src/commands/-Slash commands grouped by category
- src/events/-One file per Discord event
- src/handlers/-Dynamic loaders for commands and events
- scripts/deploy-commands.js-Register slash commands with Discord API
Getting Started
npm init -y && npm install discord.js dotenvcp .env.example .envand add your bot tokennode scripts/deploy-commands.jsto register commandsnode src/index.js
Command Structure
// src/commands/utility/ping.js
module.exports = {
data: {
name: "ping",
description: "Check bot latency"
},
async execute(interaction) {
const latency = interaction.client.ws.ping;
await interaction.reply(`Pong! ${latency}ms`);
}
};
Command Deployment
Slash commands must be registered with Discord before use. Run deploy-commands.js once after adding new commands. Use guild-specific deployment for testing (instant), global deployment for production (up to 1 hour to propagate).
When To Use This
- Teams familiar with Node.js ecosystem
- Bots needing npm package ecosystem
- Projects already using JavaScript
- Quick prototyping with hot reload (nodemon)
Trade-offs
- No built-in cogs-Must implement your own handler pattern
- Command registration-Extra step to deploy slash commands
- Memory usage-Node.js uses more memory than Python for simple bots