0.0.64 • Published 3 years ago

discordjs-decorate v0.0.64

Weekly downloads
-
License
Apache-2.0
Repository
github
Last release
3 years ago

Table of Contents

  1. Why
  2. Basic Usage
  3. Options
  4. Todo
  5. Changelog
  6. Dependencies

1. Why?

This is a fun/learning project for me to learn TypeScript decorators. So this repository is not actively developed on to deliver the fastest and best experience in creating discord bots. If you want a already established version of what I'm trying to do use OwenCalvin's discord.ts

Pull requests welcome!

2. Basic Usage

1. Install npm package

npm install discordjs-decorate
//or
yarn add discordjs-decorate

2. create a slash command in another directory

//./commands/ping.command.ts
import { BaseCommand, Command } from 'discordjs-decorate';
import { CommandInteraction } from 'discord.js';

@Command({ name: "ping", description: "responds with pong" })
export class PingCommand extends BaseCommand {
  async execute(interaction: CommandInteraction) {
    await interaction.reply("pong");
  }
}

3. Register slash commands - this is 99% like in discordjs

/// index.ts
import { REST } from '@discordjs/rest';
import { Routes } from 'discord-api-types/v9';
import { CommandService } from 'discordjs-decorate'; // <-- this is new
const rest = new REST({ version: '9' }).setToken("YOUR BOT TOKEN");

(async() => {
  try {
    console.log("Started refreshing Application");
    const commands = await CommandService.getSlashCommandsObject("YOUR COMMANDS PATH"); // <-- and this is new
    // default command path  is commands/

    await rest.put(
      Routes.applicationGuildCommands("YOUR APPLICATION ID ", "YOUR GUILD ID"),
      { body: commands},
    );

    console.log("Successfully reloaded application");
  } catch(error) {
    console.error(error);
  }
})();

3. Setup client like -- also 99% like in discordjs

import { Client, Intents, CommandInteraction } from 'discord.js';
import { CommandService } from 'discordjs-decorate'; // <-- this is new
const client = new Client({ intents: [Intents.FLAGS.GUILDS] });

client.on('ready', () => {
  console.log(`Logged in as ${client.user.tag}`);
});

client.on('interactionCreate', async(interaction: CommandInteraction) => {
  if (!interaction.isCommand()) return;
  await CommandService.getInstance().executeCommand(interaction); // <-- this is new
});

client.login("YOUR BOT TOKEN");

4. Start client and test the command

>> input: /example
>> expected output: works!

3. Options

if you want to use options for your slash commands you can use the @Option Decorator.

The fields are the same as in discordjs, name, description, type, required and choices.

Option without choices

@Command({ name: "ping", description: "replies with pong" })
@Option({ name: "option", description: "string  option", type: 3, required: false })
class PingCommand extends BaseCommand {
  async execute(interaction: CommandInteraction) {
    interaction.reply(interaction.options.getString("option"));
  }
}

Option with choices

@Command({ name: "ping", description: "replies with pong" })
@Option({ name: "animal", description: "string  option", type: 3, required: false, choices: [
  { name: "cat", value: "animal_cat" }, 
  { name: "dog", value: "animal_dog" }
]})
class PingCommand extends BaseCommand {
  async execute(interaction: CommandInteraction) {
    interaction.reply(interaction.options.getString("animal"));
  }
}

4. Todo

  • Basic Command Service
  • Slash Commands
  • Required Parameters
  • Optional Parameters
  • NPM Package
  • Automatic integration with npm
  • Automatic Command Registration
  • Decorator for discord client
  • Decorator for discord rest api
  • Automatic Help Command
  • Sub Commands
  • Optimizations

5. Changelog

v0.0.62

Fixes

  • Solved an issue that resulted in the CommandService not finding your command directory

v0.0.61

Additions

  • Added readCommandDir(path: string) to CommandService to automatically import all commands from given directory (default commands/, files have to end with .command.ts or .command.js)

Fixes

  • Solved an issue where the bot would crash if the given directory would not exits

6. Dependencies:

  • @discordjs/rest
  • discord-api-types
  • discord.js
  • typescript