Pho3niX90 / discord.ts-1

๐Ÿค– Create a discord bot with TypeScript and Decorators!

Home Page:https://discord-ts.js.org

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Discord server NPM version NPM downloads Build status paypal

Create a discord bot with TypeScript and Decorators!

๐Ÿ“– Introduction

This module is an extension of discord.js, so the internal behavior (methods, properties, ...) is the same.

This library allows you to use TypeScript decorators on discord.js, it simplifies your code and improves the readability!

This repository is a fork of OwenCalvin/discord.ts from @OwenCalvin, which is no longer actively maintained.

๐Ÿ’ป Installation

Version 16.6.0 or newer of Node.js is required

npm install discordx
yarn add discordx

installation guide

๐Ÿ“œ Documentation

discord-ts.js.org

Tutorials (dev.to)

๐Ÿค– Bot Examples

discord.ts-example (starter repo)

discord-music-bot from @oceanroleplay

WeebBot from @VictoriqueMoe

๐Ÿ’ก Why discordx?

With discordx, we intend to provide latest upto date package to build bots with many features, such as multi-bot, simple commands, pagination, music etc. Updated daily with discord.js changes.

Try discordx now with CodeSandbox

If you have any issues or feature requests, Please open an issue at Github or join discord server

๐Ÿ†• Features

  • Support multiple bots in a single nodejs instance (@Bot)
  • @SimpleCommand to use old fashioned command, such as !hello world
  • @SimpleCommandOption parse and define command options like @SlashOption
  • client.initApplicationCommands to create/update/remove discord application commands
  • client.initApplicationPermissions to update discord application commands permissions
  • Handler for all discord interactions (slash/button/menu/context)
  • Support TSyringe
  • Support ECMAScript

๐Ÿงฎ Packages

Here are more packages from us to extend the functionality of your Discord bot.

Package Description
discordx Main package
@discordx/changelog Changelog generator
@discordx/importer Import solution for ESM and CJS
@discordx/music Discord music player
@discordx/utilities @Category, pagination, etc.

๐Ÿ“” Decorators

There is a whole system that allows you to implement complex slash/simple commands and handle interactions like button, select menu, contextmenu etc.

General

Commands

GUI Interactions

๐Ÿ“Ÿ @Slash

Discord has it's own command system now, you can simply declare commands and use Slash commands this way

import { Discord, Slash } from "discordx";
import { CommandInteraction } from "discord.js";

@Discord()
abstract class AppDiscord {
  @Slash("hello")
  private hello(
    @SlashOption("text")
    text: string,
    interaction: CommandInteraction
  ) {
    // ...
  }
}

๐Ÿ“Ÿ @ButtonComponent

create discord button handler with ease!

@Discord()
class buttonExample {
  @Slash("hello")
  hello(interaction: CommandInteraction) {
    const helloBtn = new MessageButton()
      .setLabel("Hello")
      .setEmoji("๐Ÿ‘‹")
      .setStyle("PRIMARY")
      .setCustomId("hello-btn");

    const row = new MessageActionRow().addComponents(helloBtn);

    interaction.reply({
      content: "Say hello to bot",
      components: [row],
    });
  }

  @ButtonComponent("hello-btn")
  mybtn(interaction: ButtonInteraction) {
    interaction.reply(`๐Ÿ‘‹ ${interaction.member}`);
  }
}

๐Ÿ“Ÿ @SelectMenuComponent

create discord select menu handler with ease!

const roles = [
  { label: "Principal", value: "principal" },
  { label: "Teacher", value: "teacher" },
  { label: "Student", value: "student" },
];

@Discord()
class buttons {
  @SelectMenuComponent("role-menu")
  async handle(interaction: SelectMenuInteraction) {
    await interaction.deferReply();

    // extract selected value by member
    const roleValue = interaction.values?.[0];

    // if value not found
    if (!roleValue)
      return await interaction.followUp("invalid role id, select again");
    await interaction.followUp(
      `you have selected role: ${
        roles.find((r) => r.value === roleValue).label
      }`
    );
    return;
  }

  @Slash("roles", { description: "role selector menu" })
  async myroles(interaction: CommandInteraction): Promise<unknown> {
    await interaction.deferReply();

    // create menu for roels
    const menu = new MessageSelectMenu()
      .addOptions(roles)
      .setCustomId("role-menu");

    // create a row for meessage actions
    const buttonRow = new MessageActionRow().addComponents(menu);

    // send it
    interaction.editReply({
      content: "select your role!",
      components: [buttonRow],
    });
    return;
  }
}

๐Ÿ“Ÿ @ContextMenu

create discord context menu options with ease!

@Discord()
class contextTest {
  @ContextMenu("MESSAGE", "message context")
  messageHandler(interaction: MessageContextMenuInteraction) {
    console.log("I am message");
  }

  @ContextMenu("USER", "user context")
  userHandler(interaction: UserContextMenuInteraction) {
    console.log("I am user");
  }
}

๐Ÿ“Ÿ @SimpleCommand

Create a simple command handler for messages using @SimpleCommand. Example !hello world

@Discord()
class commandTest {
  @SimpleCommand("permcheck", { aliases: ["ptest"] })
  @Permission(false)
  @Permission({
    id: "462341082919731200",
    type: "USER",
    permission: true,
  })
  permFunc(command: SimpleCommandMessage) {
    command.message.reply("access granted");
  }
}

๐Ÿ’ก@On / @Once

We can 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 "discordx";

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

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

โš”๏ธ Guards

We implemented a guard system thats work pretty like the Koa middleware system

You can use functions that are executed before your event to determine if it's executed. For example, if you want to apply a prefix to the messages, you can simply use the @Guard decorator.

The order of execution of the guards is done according to their position in the list, so they will be executed in order (from top to bottom).

Guards can be set for @Slash, @On, @Once, @Discord and globally.

import { Discord, On, Client, Guard } from "discordx";
import { NotBot } from "./NotBot";
import { Prefix } from "./Prefix";

@Discord()
abstract class AppDiscord {
  @On("messageCreate")
  @Guard(
    NotBot // You can use multiple guard functions, they are excuted in the same order!
  )
  onMessage([message]: ArgsOf<"messageCreate">) {
    switch (message.content.toLowerCase()) {
      case "hello":
        message.reply("Hello!");
        break;
      default:
        message.reply("Command not found");
        break;
    }
  }
}

โ˜Ž๏ธ Need help?

Ask in discord server or check code examples

Thank you

Show your support for discordx by giving us a star on github.

About

๐Ÿค– Create a discord bot with TypeScript and Decorators!

https://discord-ts.js.org

License:Apache License 2.0


Languages

Language:TypeScript 98.8%Language:JavaScript 1.2%Language:Shell 0.0%