A boilerplate for building Discord bots using TypeScript, Discord.js, and Node.js.
-
Clone the repository:
git clone https://github.com/DFanso/discord-bot-boilerplate.git cd discord-bot-boilerplate
-
Install the dependencies:
yarn install
-
Compile the TypeScript code:
yarn build
Create a config.json
file in the src
directory with the following structure:
{
"token": "YOUR_BOT_TOKEN",
"clientId": "YOUR_CLIENT_ID"
}
Replace YOUR_BOT_TOKEN
and YOUR_CLIENT_ID
with your actual Discord bot token and client ID.
Rename .env.example
file to .env
and add the MONGO_URI
:
MONGO_URI=mongodb+srv://<username>:<password>@cluster0.mongodb.net/<dbname>?retryWrites=true&w=majority
To start the bot, run:
yarn start
This will use nodemon
to watch for file changes and automatically restart the bot.
discord-bot-boilerplate/
├── src/
│ ├── commands/
│ │ └── exampleCommand.ts
│ ├── events/
│ │ └── ready.ts
│ ├── services/
│ │ └── UserService.ts
| ├── models/
│ │ └── user.ts
│ ├── dto/
│ │ └── UserDTO.ts
│ ├── utils/
| | └── deploy.ts
│ │ └── database.ts
│ ├── config.json
│ └── index.ts
├── package.json
├── tsconfig.json
└── nodemon.json
- commands/: Directory for command files.
- events/: Directory for event files.
- services/: Directory for additional services.
- dtos/: Directory for Data Transfer Objects.
- utils/: Directory for utility files, such as database connection.
- config.json: Configuration file containing the bot token and client ID.
- index.ts: Entry point of the bot.
- Commands are stored in the
src/commands
directory and must export adata
property with command metadata and anexecute
function. - Events are stored in the
src/events
directory and must export aname
andexecute
function.
import { CommandInteraction } from 'discord.js';
module.exports = {
data: {
name: 'example',
description: 'Example command'
},
async execute(interaction: CommandInteraction) {
await interaction.reply('Hello from the example command!');
}
};
import { Client } from 'discord.js';
module.exports = {
name: 'ready',
once: true,
execute(client: Client) {
console.log(`${client.user?.tag} is online!`);
}
};
Contributions are welcome! Please create a pull request or open an issue to discuss your ideas.
This project is licensed under the MIT License. See the LICENSE file for details.