Skip to content
T.F.A edited this page Nov 19, 2023 · 4 revisions

DiscordJS-V14-Bot-Template Wiki

This is a full guide to creating application commands and events.

Commands Handler

Create an application command:

An application command must have a valid structure, check from the Discord API Docs. This is in general, not for the handler.

  • A name and a description.
  • The command type (Learn more).
  • The command name and options name must be lowercase.
  • Each option must have a description and a valid type.
  • Each required option must be on the first index of the array.
  • Valid sub-commands and sub-commands group structure (Learn more).

For the handler, you must provide a valid structure for the application command because it's going to be registered to Discord API (using REST client). The following code is the base of an application command, it could be a Chat input, User context, or Message context command. The Awaitable type is defined in @discordjs/util (Click here).

module.exports = {
    structure: ...,
    options: {
        [key: string]: any
    },
    run: (client: ExtendedClient, interaction: Interaction) => Awaitable<void>
};

The handler doesn't support options for any context command, only for Chat input commands. Note that the Interaction type could be different, for an example, if you have created a Chat input command, the interaction should be ChatInputCommandInteraction, it's also applied for the other context commands (User context: UserContextCommandInteraction, Message context: MessageContextCommandInteraction).

Make sure that you have created a command in its correct sub-directory in the commands directory.

  • slash: Chat input commands
  • user: User context commands
  • message: Message context commands

Create a prefix command:

A prefix command, originally known as "Message commands" before the era of slash commands, are non-built-in Discord commands. They are based on an author's message content. For example, if the MESSAGE_CREATE event was triggered, the handler will automatically try to check if the content starts with the prefix or not. If it is, the handler will also check automatically if the command exists or not, if it exists, the handler will run the callback function of the command.

The following code shows how it looks the structure of the prefix command:

module.exports = {
    structure: {
        name: string,
        description: string,
        aliases: string[],
        permissions: PermissionResolvable | undefined,
        cooldown: number | undefined,
        developers: boolean | undefined,
        nsfw: boolean | undefined
    },
    run: (client: ExtendedClient, message: Message, args: string[]) => Awaitable<void>
};

If a property has a type with undefined, it means that the property is optional.

Events Handler

An event means something that happened right now, but it's on Discord. Any Discord bot can listen to any event (Intents are important for these events), and the handler has one for them because they are pretty useful.

The following code below is the structure of the event:

module.exports = {
    event: K,
    once: boolean | undefined,
    run: (client: ExtendedClient, ...args: ClientEvents[K]) => Awaitable<void>
};

The K is defined as keyof ClientEvents in TypeScript because the spread operator args must return the valid types of a chosen event, for an example, if the event is "messageCreate", the structure should look like this:

module.exports = {
    event: 'messageCreate',
    run: (client: ExtendedClient, message: Message) => Awaitable<void>
};

If a property has a type with undefined, it means that the property is optional. Note that the intents are very important because, without them, you are unable to receive some required data of a thing.