DEV Community

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

Posted on • Updated on

Build your Discord bot using TypeScript and decorators !

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 {
  Discord,
  On,
  Client // Use the Client that are provided by @typeit/discord
} from "@typeit/discord";
// You must import the types from @types/discord.js
import {
  Message
} from "discord.js";

// Decorate the class with the @Discord decorator
@Discord
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.
    this._client.login(
      "YOUR_TOKEN",
      `${__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)
  @On("message")
  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":
            message.reply(this._sayHelloMessage);
            break;
          default:
            message.reply(this._commandNotFoundMessage);
            break;
        }
      }
    }
  }
}

// Start your app
AppDiscord.start();
Enter fullscreen mode Exit fullscreen mode

Et voilà !

Top comments (1)

Collapse
 
dany_tsg profile image
Dany

vramen vramen interesting