Developer Guild Commands

In this page, we will show you how to create commands only visible in the developer guild.

# The handler function (src/functions)

const { REST } = require("@discordjs/rest");
const { Routes } = require('discord-api-types/v9');
const fs = require('fs');

module.exports = (client) => {
    /**
     * 
     * @param {string} commandFolders - Your dev-command-folder
     * @param {string} path - the path to the files
     * @returns {string} - Uploads command data to the Discord REST Api V9
    */
    client.handleDevCommands = async (commandFolders, path) => {
        const clientId = process.env.ID.toString();
        const devGuildId = process.env.DEVGUILDID.toString();
        client.commandArray = [];
        for (folder of commandFolders) {
            const commandFiles = fs.readdirSync(`${path}/${folder}`).filter(file => file.endsWith('.js'));
            for (const file of commandFiles) {
                const command = require(`../dev/${folder}/${file}`);
                client.commands.set(command.data.name, command);
                client.commandArray.push(command.data.toJSON());
            }
        }

        const rest = new REST({
            version: '9'
        }).setToken(process.env.TOKEN.toString());

        (async () => {
            try {
                console.log('[ CLIENT ] Started refreshing application (/) developer commands.');

                await rest.put(
                    Routes.applicationCommands(clientId, devGuildId), {
                        body: client.commandArray
                    },
                );

                console.log('[ CLIENT ] Successfully reloaded application (/) developer commands.');
            } catch (error) {
                console.error(error);
            }
        })();
    };
};

Setup

Folder Setup and .env configuration

Create a folder called dev in the src folder. This is where you will put in the commands for the developer server only. Keep in mind that they are ONLY visible in the server NO WHERE ELSE! Also the code checks if the interaction.user.id is in the developer array. And dont forget to add

DEVGUILDID = "SERVER ID HERE"

to your .env file. Also there is no need to add devOnly: true, in the command export.

interactionCreate.js edits

Edit your src/events/interactionCreate.js by adding this into the code:

else if (interaction.guild.id === process.env.DEVGUILDID && !process.env.OWNERID.includes(interaction.user.id)) {
                return await interaction.reply({
                    content: "Sorry, this command is meant to be used by developers of this Discord Bot and in their Developer Guild only!",
                    ephemeral: true
                })
}

Deploying commands

Deploying commands work in the same way as the regular command deploy method. You can also create subfolders named like you want to sort them better and then use the regular setup for a command.

Last updated