# 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

{% code title="index.js" %}

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

{% endcode %}

### Step #2 - Put codes together for index.js

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

{% code title="index.js" %}

```javascript
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);
  });
});
```

{% endcode %}

### 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:

{% code title="/events/message.js" %}

```javascript
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);
};
```

{% endcode %}

### Step #5 - adding commands

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

{% code title="commands/kick.js" %}

```javascript
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.`);
  });
};
```

{% endcode %}


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://djsguide.js.org/basic-guides/making-a-command-handler.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
