Skip to content

Commit

Permalink
[Update] added event.run for messageCreate event
Browse files Browse the repository at this point in the history
  • Loading branch information
naseif committed Oct 2, 2021
1 parent 8beef0f commit d724d11
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 72 deletions.
9 changes: 3 additions & 6 deletions events/messageCreate.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
const { prefix } = require("../config.json");

module.exports = {
name: "messageCreate",
once: false,
async execute(message) {
async run(message) {
if (message.author.bot) return;
if (!message.guild) return;

Expand All @@ -20,10 +21,6 @@ module.exports = {
command.run(message, args, client, prefix);
} catch (error) {
client.logger(error.message, "error");
embed.setDescription(
"There was an error executing that command.\nI have contacted the owner of the bot to fix it immediately."
);
return message.channel.send({ embeds: [embed] });
}
},
};
144 changes: 78 additions & 66 deletions modules/commandsHelper.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,85 +6,97 @@
const fs = require("fs");

let commandsHelper = function () {
let api = {};
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);
/**
* 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;
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);
}
}

/**
* 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);
return result;
};

for (const command of commands) {
client.commands.set(command.data.name, command);
}
};
/**
* 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);

/**
* 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);
for (const command of commands) {
client.commands.set(command.data.name, command);
}
};

let result = [];
for (const command of commands) {
result.push(command.data.toJSON());
}
/**
* 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);

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

/**
* 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"));
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));
}
for (const file of eventFiles) {
const event = require(`${eventFolder}/${file}`);
if (event.run) {
if (event.once) {
client.once(event.name, (...args) => event.run(...args));
console.log(`Registered Run Once: ${event.name}`);
} else {
client.on(event.name, (...args) => event.run(...args));
console.log(`Registered Run On: ${event.name}`);
}
};
} else {
if (event.once) {
client.once(event.name, (...args) => event.execute(...args));
console.log(`Registered Once: ${event.name}`);
} else {
client.on(event.name, (...args) => event.execute(...args));
console.log(`Registered On: ${event.name}`);
}
}
}
};

return api;
return api;
};

module.exports.commandsHelper = commandsHelper();

0 comments on commit d724d11

Please sign in to comment.