npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

discordjs-decorate

v0.0.64

Published

typescript decorators for discordjs

Downloads

1

Readme

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

  • [x] Basic Command Service
  • [x] Slash Commands
  • [x] Required Parameters
  • [x] Optional Parameters
  • [x] NPM Package
  • [x] Automatic integration with npm
  • [x] 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