Skip to main content

 

Discord serverNPM versionNPM downloadsBuild statuspaypal

Create a discord bot with TypeScript and Decorators!

Introduction

This module is an extension of discord.js, so the internal behavior (methods, properties, ...) is the same.

This library allows you to use TypeScript decorators on discord.js, it simplify your code and improve the readability !

Requirements

NameVersion
Node.js>= 20.x.x
TypeScript>= 5.x.x

Easy setup - starter project

  1. Setup new project
npx create-discordx
  1. Build
npm run build
  1. Setup bot token
// cmd
set BOT_TOKEN=REPLACE_THIS_WITH_YOUR_BOT_TOKEN

// powershell
$ENV:BOT_TOKEN="REPLACE_THIS_WITH_YOUR_BOT_TOKEN"
  1. Run
npm run start
  1. And you're done, everything has been done for you! 🚀

Installation

Use npm or yarn to install discordx with discord.js

npm install discordx discord.js

Install your TypeScript dev dependencies too

npm install --save-dev @types/node typescript

And you should see this in your package.json

package.json
{
// ...
"dependencies": {
"discordx": "^X.X.X",
"discord.js": "^X.X.X"
},
"devDependencies": {
"@types/node": "^X.X.X",
"typescript": "^X.X.X"
}
// ...
}

If you need to update again in the future just run:

npm install discordx discord.js

Execution environment

To start your bot you can compile your code into JavaScript with TypeScript using the tsc command or simple use ts-node.

caution

You must specify the .js files when you initialize your client if you compile your code into JavaScript with tsc

const client = new Client({
intents: [
IntentsBitField.Flags.Guilds,
IntentsBitField.Flags.GuildMessages,
IntentsBitField.Flags.GuildMembers,
],
silent: false,
});

client.on("ready", async () => {
console.log(">> Bot started");

// to create/update/delete discord application commands
await client.initApplicationCommands();
});

client.login(BOT_TOKEN);
info

Learn to structure your project and commands efficiently with @discordx/importer for a well-organized directory.

tsconfig.json

Your tsconfig.json file should look like this:

package.json
{
...
"type": "module",
...
}
tsconfig.json
{
"compilerOptions": {
"module": "ESNext", // required
"target": "ESNext", // required
"noImplicitAny": true,
"sourceMap": true,
"strict": true,
"outDir": "dist",
"emitDecoratorMetadata": false, // required
"experimentalDecorators": true, // required
"declaration": false,
"importHelpers": true, // required
"forceConsistentCasingInFileNames": true,
"moduleResolution": "Node", // required
"esModuleInterop": true // required
},
"exclude": ["node_modules"]
}

Need help?

discord server

You can also find help with examples folder

See also

Next step

Setup and start your application 🚀