💻
discordjsguide
  • Intro
  • How to use this guide
  • Starting and hosting your bot
    • Getting your application ready
    • Making your bot online
  • Hosting your bot 24/7
    • Glitch
  • Basic Guides
    • Making your first command
    • making a command handler
Powered by GitBook
On this page
  • Command Handler Features
  • Actual Part
  • Step #1 - importing required packages
  • Step #2 - Put codes together for index.js
  • Step #3 - creating nessecary folders
  • Step #4 - add event
  • Step #5 - adding commands

Was this helpful?

  1. Basic Guides

making a command handler

This guide will shows you how to make a command handler.

Command Handler Features

Command handler allows you to have more organize code files and will have some special command like advanced help command for you to use.

Actual Part

Step #1 - importing required packages

Type in the following code and import required package from npm

index.js
const Enmap = require("enmap");//npm i enmap
const fs = require("fs");//npm i fs
const Discord = require("discord.js")
const client = new Discord.Client();

Step #2 - Put codes together for index.js

After all the importing stuff and your index.js code, type in the code below

index.js
fs.readdir("./events/", (err, files) => {
  if (err) return console.error(err);
  files.forEach(file => {
    const event = require(`./events/${file}`);
    let eventName = file.split(".")[0];
    client.on(eventName, event.bind(null, client));
  });
});

client.commands = new Enmap();

fs.readdir("./commands/", (err, files) => {
  if (err) return console.error(err);
  files.forEach(file => {
    if (!file.endsWith(".js")) return;
    let props = require(`./commands/${file}`);
    let commandName = file.split(".")[0];
    console.log(`Attempting to load command ${commandName}`);
    client.commands.set(commandName, props);
  });
});

Step #3 - creating nessecary folders

After this, create two folders with name events and commands.

Step #4 - add event

In the folder events , create a file with name message.js and type in the following codes:

/events/message.js
module.exports = (client, message) => {
  // Ignore all bots
  if (message.author.bot) return;

  // Ignore messages not starting with the prefix (in config.json)
  if (message.content.indexOf(client.config.prefix) !== 0) return;

  // Our standard argument/command name definition.
  const args = message.content.slice(client.config.prefix.length).trim().split(/ +/g);
  const command = args.shift().toLowerCase();

  // Grab the command data from the client.commands Enmap
  const cmd = client.commands.get(command);

  // If that command doesn't exist, silently exit and do nothing
  if (!cmd) return;

  // Run the command
  cmd.run(client, message, args);
};

Step #5 - adding commands

To add a command, create a file like: {command-name}.js Here is and example of the kick command

commands/kick.js
exports.run = (client, message, args) => {
  let reason = args;
  const modRole = message.guild.roles.find(role => role.name === "Mods");
  if (!modRole)
    return console.log("The Mods role does not exist");

  if (!message.member.roles.has(modRole.id))
    return message.reply("You can't use this command.");

  if (message.mentions.members.size === 0)
    return message.reply("Please mention a user to kick");

  if (!message.guild.me.hasPermission("KICK_MEMBERS"))
    return message.reply("");

  const kickMember = message.mentions.members.first();

  kickMember.kick(reason.join(" ")).then(member => {
    message.reply(`${member.user.username} was succesfully kicked.`);
  });
};
PreviousMaking your first command

Last updated 5 years ago

Was this helpful?