Project Setup
Follow the instructions below to setup a DiscordJS projects with djs-commands package in Typescript.
Let's Get Started
Create Project Directory
Create a new directory for your project and navigate into it.
In this example, we will be using discordjs-bot as the project name.
mkdir discordjs-bot
cd discordjs-botInitialize Project
Initialize the project with your preferred package manager.
Recommended: pnpm
PNPM (opens in a new tab) is a fast, disk space efficient package manager that uses hard links and symlinks to save space.
npm initInstall Dependencies
Install the required dependencies for the project including discord.js, @d3oxy/djs-commands, dotenv and mongoose.
| Dependency | Description | Link | Required |
|---|---|---|---|
discord.js | A powerful library for interacting with the Discord API | npm (opens in a new tab) | Required |
@d3oxy/djs-commands | A package for creating DiscordJS bots with ease | npm (opens in a new tab) | Required |
dotenv | A package for loading environment variables from a .env file | npm (opens in a new tab) | Required |
mongoose | A MongoDB (opens in a new tab) object modeling tool designed to work in an asynchronous environment | npm (opens in a new tab) | Optional |
npm install discord.js @d3oxy/djs-commands dotenv mongooseInstall Dev Dependencies
Install the required dev dependencies for the project including typescript, ts-node, concurently and nodemon.
| Dependency | Description | Link | Required |
|---|---|---|---|
typescript | A typed superset of JavaScript that compiles to plain JavaScript | npm (opens in a new tab) | Required |
ts-node | TypeScript execution and REPL for node.js | npm (opens in a new tab) | Required |
concurrently | Run commands concurrently | npm (opens in a new tab) | Required |
nodemon | A utility that will monitor for any changes in your source and automatically restart your server | npm (opens in a new tab) | Required |
npm install -D typescript ts-node concurrently nodemonCreate a .env file
In the root of your project create a .env file.
This files is used to store environment variables securely for your project.
touch .envYour folder structure should look like this:
- .env
- package.json
- pnpm-lock.json
pnpm-lock.json may vary based on the package manager you chose.Setup .gitignore File
Create a .gitignore file
In the root of your project create a .gitignore file.
This files is used to tell git which files it should not track / not maintain a version history for.
touch .gitignoreAdd the following to the file
node_modules, .env, dist and pnpm-lock.json should be ignored by git.
Add the following to the .gitignore file.
node_modules
.env
dist
pnpm-lock.jsonYour folder structure should look like this:
- .env
- .gitignore
- package.json
- pnpm-lock.json
Initialize TypeScript
Initialize TypeScript in your project by running the following command.
tsc --initConfigure TypeScript
Open the tsconfig.json file and replace the contents with the following.
{
"compilerOptions": {
"resolveJsonModule": true,
"target": "esnext",
"module": "commonjs",
"outDir": "./dist",
"rootDir": "./src",
"strict": true,
"esModuleInterop": true,
"skipLibCheck": true,
"forceConsistentCasingInFileNames": true,
"allowJs": true,
},
"exclude": [
"dist/**/*"
]
}Your folder structure should look like this:
- .env
- .gitignore
- package.json
- pnpm-lock.json
- tsconfig.json
Create a src directory
Create a src directory in the root of your project.
This directory will contain all of your source code.
mkdir srcYour folder structure should look like this:
- .env
- .gitignore
- package.json
- pnpm-lock.json
- tsconfig.json
Add neccessary scrips to package.json
Open the package.json file and add the following scripts.
{
"scripts": {
"start": "npm run build && node dist/index.js",
"build": "rm -rf ./dist && tsc --outDir dist --rootDir src --skipLibCheck",
"build:dev": "tsc -w --outDir dist --rootDir src --skipLibCheck",
"dev": "concurrently \"npm run build:dev\" \"nodemon dist/index.js\"",
},
}Create the Index File for your Bot
Create a index.ts file in the src directory.
This file will be the entry point for your bot.
touch src/index.tsYour folder structure should look like this:
- index.ts
- .env
- .gitignore
- package.json
- pnpm-lock.json
- tsconfig.json
Write your first code in index.ts
Open the index.ts file and add the following code.
import { Client, IntentsBitField, Partials, Interaction } from "discord.js";
import path from "path";
import CommandHandler from "@d3oxy/djs-commands";
require("dotenv").config();
const client = new Client({
intents: [
IntentsBitField.Flags.Guilds,
IntentsBitField.Flags.GuildMessages,
IntentsBitField.Flags.MessageContent,
IntentsBitField.Flags.GuildMembers,
IntentsBitField.Flags.DirectMessages,
],
partials: [Partials.Channel],
});
client.on("ready", () => {
new CommandHandler({
client,
commandDir: path.join(__dirname, "commands"), // Directory where your commands are located
defaultPrefix: "!", // Default prefix for your bot
});
});
client.login(process.env.DISCORD_BOT_TOKEN);Add your Bot Token to .env
Open the .env file and add the following code.
DISCORD_BOT_TOKEN=YOUR_BOT_TOKEN_HEREIf you don't already have the Bot Token, Follow any of these guides to get one.
Setting up a bot application | discord.js Guide (opens in a new tab)
Discord Developer Portal — Documentation — Getting Started (opens in a new tab)
Create a commands directory
Create a commands directory in the src directory.
This directory will contain all of your commands.
mkdir src/commandsYour folder structure should look like this:
- index.ts
- .env
- .gitignore
- package.json
- pnpm-lock.json
- tsconfig.json
Create your first command
Create a ping.ts file in the commands directory.
touch src/commands/ping.tsAdd the following code to the ping.ts file.
import {
CommandObject,
CommandType,
CommandUsage,
CooldownTypes,
DefaultCommands,
} from "@d3oxy/djs-commands";
export default {
type: CommandType.BOTH, //Required CommandType.SLASH or CommandType.BOTH or CommandType.LEGACY
description: "Replies with Pong!", // Required for slash commands,
callback: ({ client }: CommandUsage) => {
return `Pong! Latency is ${client.ws.ping}ms.`;
},
} as CommandObject;Your folder structure should look like this:
- ping.ts
- index.ts
- .env
- .gitignore
- package.json
- pnpm-lock.json
- tsconfig.json
Run your bot
It's time to run your bot.
Open your terminal and run the following command from the root directory of your bot.
npm run devYou must first add the scripts to your package.json file before running the command.
Refer Add neccessary scrips to package.json
If everything went well, you should see the following output in your terminal.
[nodemon] starting `node dist/index.js`
############################################
# DJS Commands #
# #
# The Bot Has Started! #
# Made with ❤️ by D3OXY. #
############################################
DJSCommands >> No MongoDB URI provided. Any features that require a database will not function properly.Try out the ping command
Open your discord server and type !ping in any channel.
You should see the following output.
Pong! Latency is 69ms.The Slash Command /ping will not work until it's registered by discord, which may take upto 6 hours.
Refer Registering Slash Commands for more info.