Skip to content

Commit

Permalink
[New] Added new messageCreate event and started working on #2, theore…
Browse files Browse the repository at this point in the history
…tically I should be able to reproduce all the commands without having to create new file for each command
  • Loading branch information
naseif committed Oct 2, 2021
1 parent 3cae372 commit b164c4f
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 2 deletions.
7 changes: 7 additions & 0 deletions commands/misc/help.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
const { SlashCommandBuilder } = require("@discordjs/builders");

module.exports = {
name: "help",
category: "Information",
aliases: ["h"],
description: "Return all commands, or one specific command",
async run(message, args, client, prefix) {
console.log(args);
},
data: new SlashCommandBuilder()
.setName("help")
.setDescription("Shows all available commands for this bot!")
Expand Down
33 changes: 31 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const fs = require("fs");
const { Client, Collection, Intents } = require("discord.js");
const { logger } = require("./modules/logger.js");
const { token } = require("./config.json");
const { token, prefix } = require("./config.json");
const client = new Client({
intents: [
Intents.FLAGS.GUILD_MEMBERS,
Expand All @@ -17,8 +17,8 @@ const { Player } = require("discord-player");
const player = new Player(client);
client.player = player;
client.commands = new Collection();
client.precommands = new Collection();
client.logger = logger;
playerEvents(client.player);

// All commands!
const allCommandsFolders = fs.readdirSync("./commands");
Expand All @@ -31,6 +31,7 @@ for (const folder of allCommandsFolders) {
const command = require(`./commands/${folder}/${file}`);
command.category = folder;
client.commands.set(command.data.name, command);
client.precommands.set(command.name, command);
}
}

Expand All @@ -48,6 +49,8 @@ for (const file of eventFiles) {
}
}

playerEvents(client.player);

// Initialize the client on interaction
client.on("interactionCreate", async (interaction) => {
if (!interaction.isCommand()) return;
Expand All @@ -65,4 +68,30 @@ client.on("interactionCreate", async (interaction) => {
}
});

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.precommands.get(commandName) ||
client.precommands.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] });
}
});

client.login(token);

0 comments on commit b164c4f

Please sign in to comment.