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
Name | Version |
---|---|
Node.js | >= 20.x.x |
TypeScript | >= 5.x.x |
Easy setup - starter project
- Setup new project
npx create-discordx
- Build
- npm
- Yarn
- pnpm
npm run build
yarn build
pnpm run build
- Setup bot token
- Windows
- Linux
// cmd
set BOT_TOKEN=REPLACE_THIS_WITH_YOUR_BOT_TOKEN
// powershell
$ENV:BOT_TOKEN="REPLACE_THIS_WITH_YOUR_BOT_TOKEN"
export BOT_TOKEN=REPLACE_THIS_WITH_YOUR_BOT_TOKEN
- Run
- npm
- Yarn
- pnpm
npm run start
yarn run start
pnpm run start
- And you're done, everything has been done for you! 🚀
Installation
Use npm or yarn to install discordx with discord.js
- npm
- Yarn
- pnpm
npm install discordx discord.js
yarn add discordx discord.js
pnpm add discordx discord.js
Install your TypeScript dev dependencies too
- npm
- Yarn
- pnpm
npm install --save-dev typescript
yarn add --dev typescript
pnpm add --save-dev typescript
And you should see this in your package.json
{
// ...
"dependencies": {
"discordx": "^X.X.X",
"discord.js": "^X.X.X"
},
"devDependencies": {
"typescript": "^X.X.X"
}
// ...
}
If you need to update again in the future just run:
- npm
- Yarn
- pnpm
npm install discordx discord.js
yarn add discordx discord.js
pnpm add 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.
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);
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:
- ECMAScript
- CommonJs
{
...
"type": "module",
...
}
{
"compilerOptions": {
"module": "ESNext", // required
"target": "ESNext", // required
"rootDir": "src",
"outDir": "dist",
"emitDecoratorMetadata": false, // required
"experimentalDecorators": true // required
}
}
{
...
"type": "commonjs",
...
}
{
"compilerOptions": {
"module": "CommonJS", // required
"target": "ESNext", // required
"rootDir": "src",
"outDir": "dist",
"emitDecoratorMetadata": false, // required
"experimentalDecorators": true // required
}
}
Need help?
You can also find help with examples folder