Skip to content

Commit

Permalink
Merge pull request #4 from stho32/main
Browse files Browse the repository at this point in the history
[update] removed redundant code for command discovery
  • Loading branch information
naseif committed Oct 2, 2021
2 parents fa36e98 + 4c77d00 commit 8beef0f
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 55 deletions.
118 changes: 76 additions & 42 deletions modules/commandsHelper.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,51 +6,85 @@
const fs = require("fs");

let commandsHelper = function () {
let api = {};

/**
* Registers all commands in the given subdirectory into the client collection
*
* @param {string} commandsFolder Path to folder that contains all commands
* @param {Client} client The discord client
*/
api.registerAllCommands = function (commandsFolder, client) {
const allCommandsFolders = fs.readdirSync(commandsFolder);

for (const folder of allCommandsFolders) {
const commandFiles = fs
.readdirSync(`${commandsFolder}/${folder}`)
.filter((file) => file.endsWith(".js"));
for (const file of commandFiles) {
const command = require(`${commandsFolder}/${folder}/${file}`);
command.category = folder;
client.commands.set(command.data.name, command);
}
let api = {};

/**
* Collects all available commands
*
* @param {string} commandsFolder Path to folder that contains all commands
* @returns array of commands
*/
api.getAllCommands = function(commandsFolder) {
var result = [];
const allCommandsFolders = fs.readdirSync(commandsFolder);

for (const folder of allCommandsFolders) {
const commandFiles = fs
.readdirSync(`${commandsFolder}/${folder}`)
.filter((file) => file.endsWith(".js"));
for (const file of commandFiles) {
const command = require(`${commandsFolder}/${folder}/${file}`);
command.category = folder;
result.push(command);
}
}

return result;
}
};

/**
* Registers all events in the given subdirectory on the client
*
* @param {string} eventFolder Path to folder that contains all events
* @param {Client} client The discord client
*/
api.registerAllEvents = function (eventFolder, client) {
const eventFiles = fs
.readdirSync(eventFolder)
.filter((file) => file.endsWith(".js"));

for (const file of eventFiles) {
const event = require(`${eventFolder}/${file}`);
if (event.once) {
client.once(event.name, (...args) => event.execute(...args));
} else {
client.on(event.name, (...args) => event.execute(...args));
}

/**
* Registers all commands in the given subdirectory into the client collection
*
* @param {string} commandsFolder Path to folder that contains all commands
* @param {Client} client The discord client
*/
api.registerAllCommands = function (commandsFolder, client) {
let commands = api.getAllCommands(commandsFolder);

for (const command of commands) {
client.commands.set(command.data.name, command);
}
};

/**
* Get an array of json definitions for registering slash commands
*
* @param {string} commandsFolder Path to folder that contains all commands
* @returns an array of json definitions
*/
api.getAllCommandsAsJson = function (commandsFolder) {
let commands = api.getAllCommands(commandsFolder);

let result = [];
for (const command of commands) {
result.push(command.data.toJSON());
}

return result;
}
};

return api;
/**
* Registers all events in the given subdirectory on the client
*
* @param {string} eventFolder Path to folder that contains all events
* @param {Client} client The discord client
*/
api.registerAllEvents = function (eventFolder, client) {
const eventFiles = fs
.readdirSync(eventFolder)
.filter((file) => file.endsWith(".js"));

for (const file of eventFiles) {
const event = require(`${eventFolder}/${file}`);
if (event.once) {
client.once(event.name, (...args) => event.execute(...args));
} else {
client.on(event.name, (...args) => event.execute(...args));
}
}
};

return api;
};

module.exports.commandsHelper = commandsHelper();
16 changes: 3 additions & 13 deletions modules/deploy-commands.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,12 @@ const { REST } = require("@discordjs/rest");
const { Routes } = require("discord-api-types/v9");
const { token } = require("../config.json");
const rest = new REST({ version: "9" }).setToken(token);

const commands = [];
const commandFolders = fs.readdirSync("../commands");

for (const folder of commandFolders) {
const commandFiles = fs
.readdirSync(`../commands/${folder}`)
.filter((file) => file.endsWith(".js"));
for (const file of commandFiles) {
const command = require(`../commands/${folder}/${file}`);
commands.push(command.data.toJSON());
}
}
const { commandsHelper } = require("./commandsHelper");

module.exports.registerSlashCommands = async (clientId, guildId) => {
try {
let commands = commandsHelper.getAllCommandsAsJson(__dirname + "/../commands");

await rest.put(Routes.applicationGuildCommands(clientId, guildId), {
body: commands,
});
Expand Down

0 comments on commit 8beef0f

Please sign in to comment.