Skip to content

A discord.js command handler that requires little setup, perfect for beginners

License

Notifications You must be signed in to change notification settings

ghSP/djs-command-handler

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

20 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Command Handler

ghub-activity-shield npmv-shield MIT

Installation

This one is nice and simple!

With npm:

$ npm install @yaas/command-handler

With yarn:

$ yarn add @yaas/command-handler

Usage

This is meant to simplify creating discord bots that are not single-file

const CommandHandler = require('@yaas/command-handler');
const { Client } = require('discord.js');

const client = new Client();
const CH = new CommandHandler({
  folder: __dirname + '/commands/',
  prefixes: ['?', '>'] // NOTE: prefixes may not contain spaces
});

client.on('message', message => {
  const args = message.content.split(/\s+/g);
  const command = args.shift();

  const cmd = CH.get(command);

  if (cmd === null) return;

  try {
    cmd.run(client, message, args);
  } catch {
    console.error(`There was an error running command: ${cmd.name}`);
  }
});

This should be your file structure:

.
├── index.js
├── node_modules/
│   └── ...
└── commands/
    └── test.js

This is what test.js looks like:

module.exports = class command {
  constructor() {
    this.name = 'test';
    this.aliases = [];
    this.description = '?test';
  }

  async run(client, message, args) {
    message.reply('Test works!');
  }
};