Command Handling in Discord.js v12(stable) in Glitch 1

Command Handling in Discord.js v12(stable) in Glitch

In this post I am going to show you Command Handling in Discord.js v12(stable) in Glitch, I am sure you are in pain using a single file for every command, I actually know that because i experienced the same things thats why i bring something good for you which is know as Command Handler. By using it you will be able to use single file for single command that will make your work easy and fast.

Command Handling in Discord.js v12(stable) in Glitch

Command Handling in Discord.js v12(stable) in Glitch

So First We have to create config file with name config.js where we will store all main things like TOKEN, PREFIX etc

  "token": "YOUR TOKEN",
  "prefix": "!"

Command Handling

Now head back to your main file which is server.js , and we gonna use this structure in it

const { Client, Collection } = require("discord.js");
const { config } = require("dotenv");
const {prefix, token } = require("./config.json")

const client = new Client({
    disableEveryone: true

// Collections
client.commands = new Collection();
client.aliases = new Collection();

// Run the command loader
["command"].forEach(handler => {

client.on("ready", () => {
    console.log(`Hi, ${client.user.username} is now online!`);

    client.user.setPresence("I am Devil") 

client.on("message", async message => {

    if ( return;
    if (!message.guild) return;
    if (!message.content.startsWith(prefix)) return;

    // If message.member is uncached, cache it.
    if (!message.member) message.member = await message.guild.fetchMember(message);

    const args = message.content.slice(prefix.length).trim().split(/ +/g);
    const cmd = args.shift().toLowerCase();
    if (cmd.length === 0) return;
    // Get the command
    let command = client.commands.get(cmd);
    // If none is found, try to find it by alias
    if (!command) command = client.commands.get(client.aliases.get(cmd));

    // If a command is finally found, run the command
    if (command), message, args);


Now create new file in new folder like this – handlers/command.js and Code will be like given below

const { readdirSync } = require("fs");

const ascii = require("ascii-table");

// Create a new Ascii table
let table = new ascii("Commands");
table.setHeading("Command", "Load status");

module.exports = (client) => {
    // Read every commands subfolder
    readdirSync("./commands/").forEach(dir => {
        // Filter so we only have .js command files
        const commands = readdirSync(`./commands/${dir}/`).filter(file => file.endsWith(".js"));
        // Loop over the commands, and add all of them to a collection
        // If there's no name found, prevent it from returning an error,
        // By using a cross in the table we made.
        for (let file of commands) {
            let pull = require(`../commands/${dir}/${file}`);
            if ( {
                client.commands.set(, pull);
                table.addRow(file, '✅');
            } else {
                table.addRow(file, `❌  -> missing a, or is not a string.`);
            // If there's an aliases key, read the aliases.
            if (pull.aliases && Array.isArray(pull.aliases)) pull.aliases.forEach(alias => client.aliases.set(alias,;
    // Log the table

Now we gonna create simple command with name commands/info/ping.js and code structure will be look like this

module.exports = {
    name: "ping",
    category: "info",
    description: "Returns latency and API ping",
    run: async (client, message, args) => {`Pong - ${}ms`)

You are done with command handler in v12(stable) discord.js

Resulting Code

Here is the resulting code of github –

I hope you have liked our Command Handling in Discord.js v12(stable) in Glitch post.

Also Read: Guide to Create Discord Music Bot with discord.js v12

Related Posts


Leave a Reply

Leave a Reply

Your email address will not be published. Required fields are marked *