Cover image for Build your Discord bot using TypeScript and decorators !

Build your Discord bot using TypeScript and decorators !

owen profile image owen ・2 min read

After developing several Discord bots in JS, I decided to switch to TypeScript however I was not satisfied with the way we listen on an event that is very "JavaScript".

So last night, I decided to create a very small framework to develop a Discord bot using three TypeScript decorators: @Discord, @On() and @Once().

How does it works ?

It's quite simple, when you decorate your class with @Discord, it's instantiated internally, so when a certain event is triggered, the framework will simply call the corresponding class method and pass it all the arguments provided by triggering this event.

So, how ?

The full documentation can be found here but here is a simple example:

import {
  Client // Use the Client that are provided by @typeit/discord
} from "@typeit/discord";
// You must import the types from @types/discord.js
import {
} from "discord.js";

// Decorate the class with the @Discord decorator
export class AppDiscord {
  private static _client: Client;
  private _prefix: string = "!";
  private _sayHelloMessage: string = "hello !";
  private _commandNotFoundMessage: string = "command not found...";

  static start() {
    this._client = new Client();
    // In the login method, you must specify the glob string to load your classes (for the framework).
    // In this case that's not necessary because the entry point of your application is this file.
      `${__dirname}/*Discord.ts` // glob string to load the classes

  // When the "message" event is triggered, this method is called with a specific payload (related to the event)
  async onMessage(message: Message, client: Client) {
    // Your logic...
    if (AppDiscord._client.user.id !== message.author.id) {
      if (message.content[0] === this._prefix) {
        const cmd = message.content.replace(this._prefix, "").toLowerCase();
        switch (cmd) {
          case "hello":

// Start your app

Et voilà !

Posted on by:

owen profile



dev, maths, ai, this kind of things....


Editor guide

vramen vramen interesting