DEV Community

owen
owen

Posted on • Updated on

🤖 Create your Discord bot by using TypeScript and decorators!

Some time ago, I posted an article about a module allowing the creation of Discord bots using the TypeScript decorators.
Well today, I'm pleased to announce the version 2.0 of this module!

This version includes two new very useful decorators which are @Command and @Guard.

@Command 📟

First of all, @Command allows you to simplify the management of the commands launched by the user in the chat, here is a small example of what this gives:

import {
  Discord,
  Command,
  CommandMessage
}

@Discord({ prefix: "!" })
abstract class App {
  // Executed when the user send "!hello"
  @Command("hello")
  hello(message: CommandMessage) {
    message.reply("Hello!")
  }

  @CommandNotFound()
  hello(message: CommandMessage) {
    message.reply("Command not found :(")
  }
}
Enter fullscreen mode Exit fullscreen mode

@Guard ⚔️

The @Guard decorator, on the other hand, allows functions to be executed before the event or command is executed, e.g. to check whether a condition is satisfied in order to block the event if it's not.

import {
  Discord,
  Command,
  CommandMessage,
  Guard
}

function IsUserMaster(message: CommandMessage) {
  return message.author.username === "TheMaster139";
}

@Discord({ prefix: "!" })
abstract class App {
  // Executed when the user send "!hello" and if user's username is "TheMaster139"
  @Guard(IsUserMaster)
  @Command("hello")
  hello(message: CommandMessage) {
    message.reply("Hello!")
  }

  @CommandNotFound()
  hello(message: CommandMessage) {
    message.reply("Command not found :(")
  }
}
Enter fullscreen mode Exit fullscreen mode

Thanks for reading! 😊

But that's not all, for more information you can go to the GitHub repository.

You can also join the Discord server.

Top comments (0)