Skip to content

Commit

Permalink
[update] untested, but maybe a better way to organize the source
Browse files Browse the repository at this point in the history
  • Loading branch information
stho32 committed Oct 2, 2021
1 parent fef1ec4 commit 93646fb
Show file tree
Hide file tree
Showing 4 changed files with 110 additions and 72 deletions.
19 changes: 19 additions & 0 deletions events/interactionCreate.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
module.exports = {
name: "interactionCreate",
once: false,
async execute(interaction) {
if (!interaction.isCommand()) return;
const command = client.commands.get(interaction.commandName);
if (!command) return;

try {
await command.execute(interaction, client);
} catch (error) {
console.error(error);
await interaction.reply({
content: "There was an error while executing this command!",
ephemeral: true,
});
}
},
};
29 changes: 29 additions & 0 deletions events/messageCreate.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
module.exports = {
name: "messageCreate",
once: false,
async execute(message) {
if (message.author.bot) return;
if (!message.guild) return;

const args = message.content.slice(prefix.length).trim().split(/ +/);
const commandName = args.shift().toLowerCase();

const command =
client.commands.get(commandName) ||
client.commands.find(
(cmd) => cmd.aliases && cmd.aliases.includes(commandName)
);

if (!command) return;

try {
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] });
}
},
};
78 changes: 6 additions & 72 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
const fs = require("fs");
const { Client, Collection, Intents } = require("discord.js");
const { logger } = require("./modules/logger.js");
const { token, prefix } = require("./config.json");
const { commandsHelper } = require("./modules/commandsHelper");

const client = new Client({
intents: [
Intents.FLAGS.GUILD_MEMBERS,
Expand All @@ -19,77 +20,10 @@ client.player = player;
client.commands = new Collection();
client.logger = logger;

// All commands!
const allCommandsFolders = fs.readdirSync("./commands");

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

// Loop through the events and require them
const eventFiles = fs
.readdirSync("./events")
.filter((file) => file.endsWith(".js"));

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

// Register everything...
commandsHelper.registerAllCommands("./commands", client);
commandsHelper.registerAllEvents("./events", client);
playerEvents(client.player);

// Initialize the client on interaction
client.on("interactionCreate", async (interaction) => {
if (!interaction.isCommand()) return;
const command = client.commands.get(interaction.commandName);
if (!command) return;

try {
await command.execute(interaction, client);
} catch (error) {
console.error(error);
await interaction.reply({
content: "There was an error while executing this command!",
ephemeral: true,
});
}
});

client.on("messageCreate", async (message) => {
if (message.author.bot) return;
if (!message.guild) return;

const args = message.content.slice(prefix.length).trim().split(/ +/);
const commandName = args.shift().toLowerCase();

const command =
client.commands.get(commandName) ||
client.commands.find(
(cmd) => cmd.aliases && cmd.aliases.includes(commandName)
);

if (!command) return;

try {
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] });
}
});

// ... and go!
client.login(token);
56 changes: 56 additions & 0 deletions modules/commandsHelper.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/***
* CommandsHelper - Help for commands
*
* It may make no sense, but it is ours!
*/
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);
}
}
}

/**
* 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();

0 comments on commit 93646fb

Please sign in to comment.