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

@nortex/handler

v7.5.2

Published

The easy to use, all-in-one command and event handler.

Downloads

155

Readme

@nortex/handler - effortlessly handle commands and events.

Prerequisites

  • Discord.js v14.x
  • TypeScript v4.7+
  • NodeJS v16+

Warning: Versions v7 of this package DO NOT WORK with JavaScript and are TypeScript only. This is due to the severe dependency on decorators which are not implemented in pure ECMAScript (yet).

Installation

npm i @nortex/handler

Basic usage - handling commands

// ./client.ts
import { Client, GatewayIntentBits, Interaction, InteractionType } from "discord.js";
import { CommandHandler, Command, ExecutionError } from "@nortex/handler";
import * as path from "path";

const client: Client = new Client({
  intents: [GatewayIntentBits.Guilds, GatewayIntentBits.GuildMessages],
});

const commandHandler: CommandHandler = new CommandHandler({
  client: client, // The Discord.js client instance
  directory: path.join("./commands/") // The directory where the commands should be imported from
});

/*
* It is important that no files are stored inside the directory specified above except for command files.
* Comamnd files must export a default class which extend Command.
* Any files that are stored within this directory (in shown example "./commands/" and do not export a command class will throw an exception.
* */

// You can optionally listen to handler events for example 'load':
commandHandler.on("load", (command: Command) => {
  // `command` is the command class which contains name, description, etc.
  console.log("Loaded", command.name);
});

client.on("ready", () => {
  console.log("Ready!");
});

// You have to manually run the interaction.
// If you don't, it is not going to run and the user will see a "The app hasn't responded" message.
client.on("interactionCreate", (interaction: Interaction) => {
  // Do not run interactions that are not application commands.
  // If you attempt to run a non-application-command interaction with CommandHandler, an error will be thrown.
  if(interaction.type !== InteractionType.APPLICATION_COMMAND) return;
  
  commandHandler.runInteraction(interaction).catch((err: ExecutionError) => {
    /*
    * Some error happened during the execution of the command itself.
    * This could be caused by:
    * - the command being disabled
    * - the user trying to execute the command not being in the userIds array (if present)
    * - the user trying to execute the command not being in the guildIds array (if present)
    * */
    interaction.reply({
      content: err.message,
    });
  });
});

client.login(process.env.TOKEN);
// ./commands/example_command.ts
import { CommandInteraction } from "discord.js";
import { Command, Name, Description } from "@nortex/handler";

// The bare minimum for commands are the Name and Description decorators.
@Name("hello")
@Description("Say hello.")
export default class ExampleCommand extends Command {
  // The class must have its own run() command, otherwise a MethodNotOverridenError will be thrown.
  async run(interaction: CommandInteraction) {
    // do something with the interaction
    interaction.reply({
      content: "Hello!",
    });
  }
}

You can duplicate the example_command.ts file to create new commands.