Skip to content
This repository has been archived by the owner on Aug 22, 2024. It is now read-only.

Getting Started

Adam Pine edited this page May 18, 2021 · 4 revisions

Setup and start your application

In order to start your application, you must use the discord.TS Client (not the client that is provided by discord.JS!).
It works the same as the discord.JS Client (same methods, properties, etc).

You have different parameters in addition to discord.js when you initialize your Client:

  • classes (required):
    Indicate the class jacket of your classes containing the @Discord decorator. It accepts a list of classes or of (glob) paths.

  • silent (false by default):
    Allows you to disable your event information at startup.

  • variablesChar (":" by default):
    Allows you to change the prefix character of a variable.

You must specify the glob path(s) where your decorated classes are

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

async function start() {
  const client = new Client({
    classes: [
      `${__dirname}/*Discord.ts`, // glob string to load the classes
      `${__dirname}/*Discord.js` // If you compile using "tsc" the file extension change to .js
    ],
    silent: false,
    variablesChar: ":"
  });

  await client.login("YOUR_TOKEN");
}

start();

@Discord - Getting started

So we start with an empty class (abstract is not necessary but this is more type-safe, the class shouldn't be initialized).

abstract class AppDiscord {
}

Then you must declare it as a Discord app class with the @Discord decorator :

import { Discord } from "@typeit/discord";

@Discord() // Decorate the class
abstract class AppDiscord {
}

@On / @Once - Listen to the events

We can now declare methods that will be executed whenever a Discord event is triggered.
Our methods must be decorated with the @On(event: string) or @Once(event: string) decorator.
That's simple, when the event is triggered, the method is called:

import {
  Discord,
  On,
  Once
} from "@typeit/discord";

@Discord()
abstract class AppDiscord {
  @On("message")
  private onMessage() {
    // ...
  }

  @Once("messageDelete")
  private onMessageDelete() {
    // ...
  }
}

Client payload injection

For each event a list of arguments is injected in your decorated method, you can type this list thanks to the ArgsOf<Event> type provided by discord.ts. You also receive other useful arguments after that:

  1. The event payload (ArgsOf<Event>)
  2. The Client instance
  3. The guards payload

You should use JS desctructuring for ArgsOf<Event> like in this example

import {
  Discord,
  On,
  Client,
  ArgsOf
} from "@typeit/discord";

@Discord()
abstract class AppDiscord {
  @On("message")
  private onMessage(
    [message]: ArgsOf<"message">, // Type message automatically
    client: Client, // Client instance injected here,
    guardPayload: any
  ) {
    // ...
  }
}