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

@davipccunha/discordjs-helper

v2.4.1

Published

A package that contains some useful functions to complement discord.js library

Downloads

1,074

Readme

About

Easily create interactions with type safety

discordjs-helper is a Node.js module written in TypeScript that allows you to more easily create and register interactions to the Discord API and discordjs library. This package contains:

  • Interfaces to implement and consistently define your interactions
  • Easier to use EmbedBuilder
  • Decorators to help with common verifications such as users' permissions

Installation

Node.js and npm required!

Run into your project's terminal:

npm install @davipccunha/discordjs-helper

Examples

Creating a new slash command

import { CustomChatInputCommand, ExtendedClient, RegisterCommandInteraction, RequirePermission } from "@davipccunha/discordjs-helper";
import { ApplicationCommandType, ChatInputCommandInteraction, PermissionFlagsBits } from "discord.js";

@RegisterCommandInteraction("ping", "Ping the bot!", true, 1)
@RequireMemberPermission(PermissionFlagsBits.Administrator)
export class PingCommand implements CustomChatInputCommand {
    name!: string;
    type!: ApplicationCommandType.ChatInput;
    description!: string;
    defaultPermission!: boolean;

    async execute(interaction: ChatInputCommandInteraction, client: ExtendedClient): Promise<void> {
        await interaction.reply("Pong!").catch(console.error);
    }
}

The class above defines a Chat Input Command Interaction. It contains the information to create the command and a method execute() that is run when a command interaction with the same name as the class' attribute name is created on Discord.

Registering slash commands

Commands should be either registered using the ExtendedClient#registerCommands() method or by decorating its class with @RegisterCommandInteraction(name, description) and then sent to Discord API using ExtendedClient#loadCommands(). Due to how Node.js loads the modules, a module that contains a command class decorated with @RegisterCommandInteraction must be imported in some point of your program

import path from 'path';
import { pathToFileURL } from 'url';
import { ExtendedClient, recursiveFiles } from "@davipccunha/discordjs-helper";

setConstants();

const client = new ExtendedClient("TOKEN GOES HERE");

// This is necessary due to how Node.js loads and runs modules
await registerInteractions();

// This should be called after registering the commands
// This function caches the registered interactions, starts the bot and start listening for interactions being created
client.start(true);

// This function registers the cached commands to the guilds passed as parameters (it registers to all if no parameter is passed)
client.loadCommands("GUILD ID 1", "GUILD ID 2", ...);

// This code gets all files inside the dist/interactions folder and loads them so interactions decorated with @Register<Type>Interaction are correctly registered
async function registerInteractions() {
    const interactions = await recursiveFiles("dist/interactions");
    for (const interaction of interactions) {
        const fileName = interaction.replaceAll('\\', '/');
        const absolutePath = path.resolve(fileName);

        const fileUrl = pathToFileURL(absolutePath).href;

        await import(fileUrl);
    }
};

// This resets some constants such as error messages 
function setConstants() {
    ErrorMessages.NoPermission = "You don't have permission to do that";
};

Together, the two code snippets above is all there is to get your first slash command working.

The example provided on how to register the commands is a simple way of loading all modules, regardless of how many interactions you have, instead of having to instantiate each one of them and pass them as parameter to the register methods

Reminders

For interactions decorated with @Register...Interaction, the module in which they are defined must be imported somewhere in the main program due to how Node.js loads modules

You can always register all your interactions using one of the following methods in Extended Client:

  • registerCommands()
  • registerButtons()
  • registerSelectMenus()
  • registerModals() and passing false as argument to the ExtendedClient#start() method

Then, the package handles it when an interaction is created

Found a problem?

Please let me know of any problems found by opening an issue at GitHub issue. If you have a suggestion or just want to contact me, please send an email to [email protected]

Donations

If you are feeling generous or would like to support this and other future open-source projects, you can donate at my Buy me a coffee page