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

@thatbadname/discord-command-handler

v1.1.18

Published

An advanced command hander for Discord.JS v14

Downloads

12

Readme

Discord.JS v14 Command Handler

This is an advanced command handler using Discord.js v14
Discord Banner 4

Installation

Run npm i @thatbadname/discord-command-handler discord.js in a console and you are done!

Usage

We reccommend that you copy and paste the example index.js code bellow into your index.js file for your bot. This will automagically create all the files that your bot will need to function.

Cooldowns

Yep, there is a built in cooldown system. You can access it using

const Handler = require("@thatbadname/discord-command-handler")
Handler.main.cooldown(client, key, {id: 0123456789012345678, personal: 0})

client is your discord.js client, key is, for example, the command name (it can also be a button id, ect), {id} is the id of the user you want to put on cooldown and {personal} is the cooldown time in seconds.

Command Handler Options

// The default values have been filled in

Handler.main.DiscordCommandHandler(client,  {
  mainDir: __dirname,
  // The main directory of your bot

  configFile: 'config.json',
  /* Your bots config file. This should be a .json file and should contain:
  {
    "token":"",
    "clientId":"",
    "testGuildId":""
  }
  where token is your bots token, clientId is your bots id and testGuildId is the main development/support server for your bot
  */
 
  eventsDir: '/events',
  // This is the directory where all your event folders should be. Default The structure should follow: mainDir/eventsDir/<folder>/<eventFile>

  commandsDir: '/commands',
  // This is the directory where all your event folders should be. The structure should follow: mainDir/commandDir/<folder>/<commandFile>

  components: {
    main: '/components',
    buttons: '/buttons',
    selectMenus: '/selectMenus'
  },
  // This is where component files should be stored. The structure should follow: mainDir/mainComponentDir/<buttonDir|selectMenuDir>/<componentFile>

  builtIn: {
    automaticRepair: true,
    helpCommand: true
  },
  /*
  automaticRepair, when enabled will create all the files that your bot will need to function. We reccomend leaving this as true if this is your first time running the bot

  helpCommand, when true will create the help command and its parent directory. For this to work automaticRepair must also be enabled.
  */

  ownerIds: [],
  // Put any ids of users that you want to be able to run ownerOnly: true commands

  ownerOnlyMessage: 'This command is owner only',
  // This is the message that will be sent if a user tries to use an ownerOnly command

  cooldownMessage: 'Please try again <t:{time}:R>',
  // This is the message that will be sent if a user tries to use a command while they are on cooldown. {time} will be replaced with the epoch timestamp of when the cooldown ends

  allowPrefixCommands: false
  // Should the bot let prefixed commands work

  prefix: '!'
  // The prefix of the bot. Only works if allowPrefixCommands: true
})

Making a new command

Here is an example of a very simple ping pong command. For more advanced command creation check out the Discord.JS docs

const {
  SlashCommandBuilder,
} = require('discord.js') // Require SlashCommandBuilder from discord.js

module.exports = {
  testOnly: true, // If true this command will only be available in your test server
  ownerOnly: false, // If true this command will only be runnable if the users id is part of ownerIds
  hide: false, // If true this command will be hidden from the built in help command
  slash: true // Make it a slash command. You can use "both" to make it both slash and prefix
  data: new SlashCommandBuilder() // The data of the slash command
    .setName('ping') // The name of the command
    .setDescription('Pong!'), // The commands description

  async execute(interaction, client) {
    // Put your commands code here

    interaction.reply({
      content:
        `Pong!`
    }) // Reply to the interaction with "Pong!"
  }
}

And this is a prefixed command

module.exports = {
  testOnly: true, // If true this command will only be available in your test server
  ownerOnly: false, // If true this command will only be runnable if the users id is part of ownerIds
  hide: false, // If true this command will be hidden from the built in help command
  slash: false // Make it not a slash command
  data: {
    name: 'ping' // Name of the command
  },

  async execute(message, args, client) {
    // Put your commands code here

    message.reply({
      content:
        `Pong!`
    }) // Reply to the message with "Pong!"
  }
}

Making a new event

This is an example of a simple ready event

module.exports = {
  name: 'ready', // The name of the event from the discord api
  once: true, // If true this event will only run once

  async execute(client) {
    // Put code to execute when this event is called here

    console.log(`[Startup] ${client.user.username} is online`)
  }
}

Making a new (button) component

This is an example of a button component

module.exports = {
  data: {
    name: 'example-button' // This is the custom ID of the button
  },

  async execute(interaction, client) {
    // Code to execute when the button is pressed

    interaction.reply({content: `You pressed a button!`})
  }
}

Making a new (select menu) component

This is an example of a select menu component

module.exports = {
  data: {
    name: 'example-selectMenu' // This is the custom ID of the select menu
  },

  async execute(interaction, client) {
    // Code to execute when the select menu is used

    interaction.reply({content: `You chose ${interaction.values[0]}`})
  }
}

Example index.js

const { Client, GatewayIntentBits } = require("discord.js")
const Handler = require("@thatbadname/discord-command-handler")
const client = new Client({
  intents: [
    GatewayIntentBits.Guilds,
    GatewayIntentBits.GuildMessages,
  ]
})

Handler.main.DiscordCommandHandler(client,  {
  mainDir: __dirname,
  configFile: 'config.json',
  eventsDir: '/events',
  commandsDir: '/commands',
  components: {
    main: '/components',
    buttons: '/buttons',
    selectMenus: '/selectMenus'
  }
})

Functions

The package comes loaded with a few functions. These can be access using

const Handler = require("@thatbadname/discord-command-handler")
Handler.functions

Id Generation

You can use the generateId function to make an id

// Usage
const Handler = require("@thatbadname/discord-command-handler")
Handler.functions.generateId(length, prefix)

// Example
const Handler = require("@thatbadname/discord-command-handler")
Handler.functions.generateId(5, 'D') // Returns something like: D-t76Kj

length is the length of id that you want, prefix is the prefix of the id