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

command-discord

v2.0.0

Published

Framework to discordjs for easily doing commands

Downloads

30

Readme

Command Framework

A framework to easily do commands using discord.js

Installation

Check that you have node and npm installed

  • To check if you have Node.js installed, run this command in your terminal:
node -v
  • To confirm that you have npm installed you can run this command in your terminal:
npm -v
  • Ok, now an installation of our npm
npm i command-discord

Usage

const command = require("command-discord");
// all parameters in this object are optional, you can pass token in the start function, bellow parameters are the default
const client = command.Client({
    token:"Your bot token",
    color:"65535", //optional color for  embeds in decimal (65535 default)
    path:"./commands", // path for commands folder, (./commands default)
    prefix:"h!", // prefix can be an array if you need multiple prefix, (! default)
    // example: ['h!','!']
    logErrors:true, // true default, if you dont want to console log errors in command false
    // you can get errors using the commandError event
    commandExists:false,
    commandExistsContent:{
        embed:{ // Message using in commandExists
            color: "16711680",
            description:"We dont have this command yet"
        }
    },
    // if commmand dont exists reply with a content (in this case a embed) default is false
    prefixConfig:{
         useUsername: true,
          useMention: true,
          // if you dont want to use username or mention as an prefix put these false (default is true)
          editMessage:true // if editing a message can run a command default is true
    }, 
    // hooks
    before: ({ args, prefix, command, message }, execute) => {
            execute({args,prefix,message})
    },
    after: ({message,command}) => {
            console.log(`Time: ${Date.now() - message.client.readyTimestamp}\n
            Command: ${command.name[command.name.length -1]}\n
            Author: ${message.author.tag}`)
    }
    
},{
    // client options for discordjs (https://discord.js.org/#/docs/main/stable/typedef/ClientOptions)
});

client.start(); // to start the bot you can pass token here, if you dont pass options
  • All parameters (client options) can be used in external codes -- Example: client.prefix, client.color, client.external.Discord
  • For restart your bot use in your code
client.restart();

Console Error

client.on("commandError", function (command, error) {
    console.error(`Error ${error.toString()} in command ${command.name}`)
    //this log is automatic if you dont disable the logErrors option
})

Bot Playing

client.on("ready", async () => {
    console.log(`Logged as ${client.user.username} with ${client.users.size} users and ${client.guilds.size} guilds`)
    const phrases = [`Use ${client.prefix[0]}help`, `Use ${client.prefix[0]}help to view my Commands`]
    setInterval(() => {
        let selected = phrases[Math.floor(Math.random() * phrases.length)]
        client.user.setPresence({ game: { name: `${selected}` } })
    }, 5 * 60 * 1000)
    client.user.setPresence({ game: { name: phrases[0] } })
});

Commands Examples

  • Let's do a simple avatar command
// commands/others/avatar.js
// we can do this command in this way

exports.name = "avatar"
exports.help = "See someone avatar"
exports.cooldown = 2 // cooldown in seconds
exports.cdMessage = "Wait 2 seconds to use this again" // message if someone try to use command in cooldown
exports.aliases = ["profilepic","picture"] 
exports.category = "others" // optional better for filters
// all params are opcional
exports.run = function(params){
     // params.message is the message for the command you can use params.message.client for the client
     //param.prefix for the prefix  and param.args for command argumentes
     const {message,args} = params;
     message.channel.send({
         embed:{
             title:"avatar",
             color:message.client.color,
             image:{
                 url:message.mentions.users.first() ? message.mentions.users.first().displayAvatarURL : message.author.displayAvatarURL
             }
         }
     })
}

// or we can do this more "beutifull"
module.exports = new (class cmd{
  constructor(){
      this.name = "avatar";
      this.category = "others"
      this.help = "See someone avatar";
      this.cooldown = 2;
      this.cdMessage = "Wait 2 seconds to use this again";
      this.aliases = ["profilepic","picture"] 
  }
  run({message,buildMessage,args}){
      // buildMessage is used for embeds
   buildMessage({
      image:{
          url:message.mentions.users.first() ? message.mentions.users.first().displayAvatarURL : message.author.displayAvatarURL
      }
  }).send() // you can pass an channel id to send
}
})
  • Help Command
// commands/help.js
// commands/help/help.js
module.exports = new (class cmd{
  constructor(){
      this.name = "help";
      this.category = "others"
      this.help = "See my commands";
      this.cooldown = 2;
      this.cdMessage = "Wait 2 seconds to use this again";
      this.aliases = ["cmds","commands"] 
  }
  run({message,buildMessage,client,args}){
   buildMessage({
        description:client.commands.map(a => "`"+a.name[a.name.length-1]+"("+a.help+")`").join(", ")
    }).send()
    // or we can just send a category commands
    buildMessage({
        title:"Util commands",
        description:client.commands.filter(a=>a.category == "others").map(a => "`"+a.name[a.name.length-1]+"("+a.help+")`").join(", ")
    }).send()
}
})
  • Ping Command
// commands/others/ping.js
module.exports = new (class cmd {
    constructor() {
        this.name = "ping";
        this.category = "others";
        this.help = "I show my latency";
        this.cooldown = 3;
        this.cdMessage = "Wait 3 seconds to use this again";
        this.aliases = ["pong"]
    }
    run({ message, buildMessage, client, args}){
        message.reply(`:ping_pong:Pong ${Math.floor(client.ping)}`)
    }
})

You can find the documentation of discord.js Here Example of bot using this framework Here