DEV Community

owen
owen

Posted on • Edited on

14 9

🤖 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.

PulumiUP 2025 image

From Infra to Platforms: PulumiUP 2025 Panel

Don’t miss the expert panel at PulumiUP 2025 on May 6. Learn how teams are evolving from infrastructure engineering to platform engineering—faster, more secure, and at scale.

Save Your Spot

Top comments (0)

Neon image

Next.js applications: Set up a Neon project in seconds

If you're starting a new project, Neon has got your databases covered. No credit cards. No trials. No getting in your way.

Get started →

👋 Kindness is contagious

Engage with a wealth of insights in this thoughtful article, valued within the supportive DEV Community. Coders of every background are welcome to join in and add to our collective wisdom.

A sincere "thank you" often brightens someone’s day. Share your gratitude in the comments below!

On DEV, the act of sharing knowledge eases our journey and fortifies our community ties. Found value in this? A quick thank you to the author can make a significant impact.

Okay