diff --git a/README.md b/README.md index ad75707..730619c 100644 --- a/README.md +++ b/README.md @@ -14,7 +14,7 @@

Eyesense is a feature-rich discord bot that plays music from various platforms and more.
- The bot is still in the development phase You can check the release page for the stable releases
+ The bot is still in the development phase. You can check the release page for the stable releases.

@@ -95,19 +95,19 @@ if you are having problems installing sodium, make sure to install the following First you need to create a new application in the [Discord Developer Portal](https://discord.com/developers/applications) and then create a bot to get your own discord Token and invite the bot to your server. Once you got your key, rename `config.example.json` to `config.json`. Now open the `config.json` file and add your discord bot tokens. -Once you are done, you can now start the bot by running the follwoing: +Once you are done, you can now start the bot by running the following: ```node index.js``` ### Recommendation -Use pm2 to run the bot in the background and restart it on internet issues, or in case the bot crashed +Use pm2 to run the bot in the background and automatically restart it when e.g. your server has internet issues: Simply install the pm2 package globally using ```npm i pm2 -g``` and then instead of ```node index.js``` run ```pm2 start index.js``` ## Contributions -Software contributions are welcome. If you are not a dev, testing and reproting bugs can also be very helpful! +Software contributions are welcome. If you are not a dev, testing and reporting bugs can also be very helpful! ## Questions? diff --git a/events/interactionCreate.js b/events/interactionCreate.js new file mode 100644 index 0000000..432c1b1 --- /dev/null +++ b/events/interactionCreate.js @@ -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, + }); + } + }, +}; diff --git a/events/messageCreate.js b/events/messageCreate.js new file mode 100644 index 0000000..fba8711 --- /dev/null +++ b/events/messageCreate.js @@ -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] }); + } + }, +}; diff --git a/events/ready.js b/events/ready.js index 049cde5..9dd35fe 100644 --- a/events/ready.js +++ b/events/ready.js @@ -1,4 +1,4 @@ -const { registerSlashCommands } = require("../deploy-commands"); +const { registerSlashCommands } = require("../modules/deploy-commands"); module.exports = { name: "ready", diff --git a/index.js b/index.js index 0d3ef1d..70dffdc 100644 --- a/index.js +++ b/index.js @@ -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, @@ -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); diff --git a/modules/commandsHelper.js b/modules/commandsHelper.js new file mode 100644 index 0000000..6b2cd5a --- /dev/null +++ b/modules/commandsHelper.js @@ -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(); \ No newline at end of file diff --git a/deploy-commands.js b/modules/deploy-commands.js similarity index 77% rename from deploy-commands.js rename to modules/deploy-commands.js index f20a809..6bd0fb6 100644 --- a/deploy-commands.js +++ b/modules/deploy-commands.js @@ -1,18 +1,18 @@ const fs = require("fs"); const { REST } = require("@discordjs/rest"); const { Routes } = require("discord-api-types/v9"); -const { token } = require("./config.json"); +const { token } = require("../config.json"); const rest = new REST({ version: "9" }).setToken(token); const commands = []; -const commandFolders = fs.readdirSync("./commands"); +const commandFolders = fs.readdirSync("../commands"); for (const folder of commandFolders) { const commandFiles = fs - .readdirSync(`./commands/${folder}`) + .readdirSync(`../commands/${folder}`) .filter((file) => file.endsWith(".js")); for (const file of commandFiles) { - const command = require(`./commands/${folder}/${file}`); + const command = require(`../commands/${folder}/${file}`); commands.push(command.data.toJSON()); } }