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

djs-command-helper

v3.0.1

Published

A simple, easy to use module that houses functions to can refresh global and guild specific commands for your Discord App.

Downloads

128

Readme

djsCommandHelper

A simple, easy to use module that houses functions to can refresh global and guild specific commands for your Discord App.

Installation

npm install git+ssh://[email protected]:The-LukeZ/djsCommandHelper.git
npm install djs-command-helper

Usage

Folder structure

The src folder it can be any folder. Just the commands folder should be a subfolder of the directory your index.js is located in.

Example:

📂src
 ┣ 📄index.js
 ┗ 📂commands
   ┗📂utility
     ┣ 📄ping.js
     ┗ 📄your-private-command.js

The command file

Note that for using this you need to install discord.js!

| Key name | Description | Default | | -------- | --------------------------------------------------------------------------------------------------------- | ------- | | ignore | If set to true then this command will be ignored upon refreshing | false | | guildIds | An Array of guild ids in which the command should be registered/updated ; command is global if not set | [] | | data | The raw command data Learn more about it here | - | | run | The function to call (It's only important for your own logic - so name this whatever you want) | - |

- means that it doesn't have a default value

const { SlashCommandBuilder } = require("discord.js");

module.exports = {
    ignore: true,
    guildIds: ["123456789", "987654321"], // Note: This wont automatically delete them from guilds!
    data: new SlashCommandBuilder()
        .setName("ping")
        .setDescription("Replies with Pong!"),

    // The function to call whenever the command is executed (Doesnt matter when calling client.deployCommands())
    async run(interaction) {
        await interaction.reply("Pong!");
    },
};

Deploy the commands

// Don't forget to load all things from discord.js

const { deployCommands } = require("djs-command-deployer");
const path = require("node:path");
require("dotenv").config(); // load your token and app's id

// Set up your client
// Use the guide as an example if you need help
// https://discordjs.guide/creating-your-bot/main-file

// When the client is ready, run this code once.
client.once(Events.ClientReady, (readyClient) => {
    console.log(`Ready! Logged in as ${readyClient.user.tag}`);

    // Call deployCommands to refresh your commands
    deployCommands(
        path.join(__dirname, "commands", "utility")
        {
            appToken: process.env.DISCORD_TOKEN,
            appId: process.env.ClientId,
        }
        // logs are optional
    );
});

// Log in to Discord with your client's token
client.login(process.env.DISCORD_TOKEN);

Delete a guild-command

Global commands can be automatically deleted if you just delete the file or set ignore: true and run the deployCommands function again.

Guild commands on the other hand need to be deleted manually.

The name of a command of an application is unique, but only in its type. Command ids are unique. Make sure that you have command's id and not it's name.

You can get the command id by either typing in the command and then right click the description above or by giong into the guild settings > Integrations > YOUR_BOT > right mouse click on the command > Copy Command ID

In this example we are building a manager-command that has StringOptions for the command and the guild id where one can paste in the ID or the name.

const { SlashCommandBuilder, EmbedBuilder } = require("discord.js");
const { deleteCommand } = require("djs-command-helper");

module.exports = {
    guildIds: ["12345"], // the guild id of the dev's guild so that no one can delete every command
    data: new SlashCommandBuilder()
        .setName("manage-commands")
        .setDescription("Replies with Pong!")
        .addStringOption((op) =>
            op
                .setName("command-id")
                .setDescription("The ID of the command to be removed")
        )
        .addStringOption((op) =>
            op
                .setName("server-id")
                .setDescription(
                    "The ID of the server from which the command is to be removed"
                )
        ),

    // This function is called whenever an interactionCreate event is triggered.
    async run(interaction) {
        const guildId = interaction.options.getString("server-id");
        const command = interaction.options.getString("command-id");
        await interaction.deferReply({ ephemeral: true }); // Defer to remove the risk of not responding in time

        try {
            const response = await deleteCommand(command, {
                appToken: interaction.client.token,
                appId: interaction.application.id,
                guildId: guildId,
            });
        } catch (err) {
            // respond with an error if the operation fails in some way
            return await interaction.editReply({
                embeds: [
                    new EmbedBuilder()
                        .setTitle("❌ Command not deleted due to an error")
                        .setDescription("```" + err + "```")
                        .setColor(0xff0000),
                ],
            });
        }

        // Success!
        await interaction.editReply({
            embeds: [
                new EmbedBuilder()
                    .setTitle("✅ Command deleted")
                    .setColor(0x44ff44),
            ],
        });
    },
};