From c816f182b24710314b05335efa81e4c2ada5c9fc Mon Sep 17 00:00:00 2001 From: Vylpes Date: Sun, 7 Feb 2021 14:12:56 +0000 Subject: [PATCH] Update util to check if folder exists --- cmd-config.json | 3 + package-lock.json | 2 +- package.json | 6 +- src/client/util.js | 147 ++++++++++++++++++---------------- tests/src/client/util.test.js | 9 +++ 5 files changed, 95 insertions(+), 72 deletions(-) create mode 100644 cmd-config.json diff --git a/cmd-config.json b/cmd-config.json new file mode 100644 index 0000000..544b7b4 --- /dev/null +++ b/cmd-config.json @@ -0,0 +1,3 @@ +{ + +} \ No newline at end of file diff --git a/package-lock.json b/package-lock.json index 608108a..cbc83c3 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,6 +1,6 @@ { "name": "vylbot-core", - "version": "1.0.4", + "version": "20.0.0", "lockfileVersion": 1, "requires": true, "dependencies": { diff --git a/package.json b/package.json index 6049459..ae18886 100644 --- a/package.json +++ b/package.json @@ -22,8 +22,8 @@ "bot", "client" ], -"repository": "github:vylpes/vylbot-core", -"devDependencies": { + "repository": "github:vylpes/vylbot-core", + "devDependencies": { "eslint": "^7.17.0" -} + } } diff --git a/src/client/util.js b/src/client/util.js index 28a8797..4db8bd8 100644 --- a/src/client/util.js +++ b/src/client/util.js @@ -20,64 +20,70 @@ class util { // Get the current folder to check const folder = this._client.config.commands; - if (existsSync(`${process.cwd()}/${folder}/${name}.js`)) { - // Require the command file, now that we know it exists and initialise it - const commandFile = require(`${process.cwd()}/${folder}/${name}.js`); - const command = new commandFile(); - - // Require the command config file and get the config for the current command - const configJson = this._client.commandConfig; - const config = configJson[name]; - - // Get the list of required configurations the command needs - const commandConfigs = command.configs; - - // Loop through all the required configs of the command - for (const i in commandConfigs) { - // If the command doesn't have the configs in the config string, throw an error - if (!config) return generateResponse(false, `${commandFile.name} requires ${commandConfigs[i]} in it's configuration`); - if (!config[commandConfigs[i]]) return generateResponse(false, `${commandFile.name} requires ${commandConfigs[i]} in it's configuration`); - } - - // Get the roles required for this command to run - const requiredRoles = command.roles; - - // Get the category, if there is no category, set it to a default string - if (!command.category) command.category = "none"; - - // Loop through all roles required - for (const i in requiredRoles) { - // If the user doesn't have a required role, don't run the command and let the user know - if (!message.member.roles.cache.find(role => role.name == requiredRoles[i])) { - message.reply(`You require the \`${requiredRoles[i]}\` role to run this command`); - return generateResponse(false, `You require the \`${requiredRoles[i]}\` role to run this command`); + // If the folder exists + if (existsSync(`${process.cwd()}/${folder}/`)) { + // If the file exists inside the folder + if (existsSync(`${process.cwd()}/${folder}/${name}.js`)) { + // Require the command file, now that we know it exists and initialise it + const commandFile = require(`${process.cwd()}/${folder}/${name}.js`); + const command = new commandFile(); + + // Require the command config file and get the config for the current command + const configJson = this._client.commandConfig; + const config = configJson[name]; + + // Get the list of required configurations the command needs + const commandConfigs = command.configs; + + // Loop through all the required configs of the command + for (const i in commandConfigs) { + // If the command doesn't have the configs in the config string, throw an error + if (!config) return generateResponse(false, `${commandFile.name} requires ${commandConfigs[i]} in it's configuration`); + if (!config[commandConfigs[i]]) return generateResponse(false, `${commandFile.name} requires ${commandConfigs[i]} in it's configuration`); } - } - - // Get the ids of the users that are only permitted to run this command - const users = command.users; - - // If the command has any limits, limit the command, otherwise default to anyone - if (users.length > 0) { - if (!users.includes(message.member.id)) { - message.reply(`You do not have permission to run this command`); - return generateResponse(false, "You do not have permission to run this command"); + + // Get the roles required for this command to run + const requiredRoles = command.roles; + + // Get the category, if there is no category, set it to a default string + if (!command.category) command.category = "none"; + + // Loop through all roles required + for (const i in requiredRoles) { + // If the user doesn't have a required role, don't run the command and let the user know + if (!message.member.roles.cache.find(role => role.name == requiredRoles[i])) { + message.reply(`You require the \`${requiredRoles[i]}\` role to run this command`); + return generateResponse(false, `You require the \`${requiredRoles[i]}\` role to run this command`); + } } + + // Get the ids of the users that are only permitted to run this command + const users = command.users; + + // If the command has any limits, limit the command, otherwise default to anyone + if (users.length > 0) { + if (!users.includes(message.member.id)) { + message.reply(`You do not have permission to run this command`); + return generateResponse(false, "You do not have permission to run this command"); + } + } + + // Run the command and pass the command context with it + command[command.run]({ + "command": name, + "arguments": args, + "client": this._client, + "message": message, + "config": config, + "commandConfigs": commandConfigs + }); + + return generateResponse(true, `loaded command '${name}' with arguments '${args}'`); + } else { + return generateResponse(false, 'File does not exist'); } - - // Run the command and pass the command context with it - command[command.run]({ - "command": name, - "arguments": args, - "client": this._client, - "message": message, - "config": config, - "commandConfigs": commandConfigs - }); - - return generateResponse(true, `loaded command '${name}' with arguments '${args}'`); } else { - return generateResponse(false, 'File does not exist'); + return generateResponse(false, 'Command folder does not exist'); } } @@ -86,25 +92,30 @@ class util { // Get the current folder to check const folder = this._client.config.events; - // Get the files inside of this folder - const eventFiles = readdirSync(`${process.cwd()}/${folder}/`); + // If the folder exists + if (existsSync(`${process.cwd()}/${folder}/`)) { + // Get the files inside of this folder + const eventFiles = readdirSync(`${process.cwd()}/${folder}/`); - // Loop through all the files in the folder - for (let i = 0; i < eventFiles.length; i++) { - // Ignore non-javascript files - if (eventFiles[i].includes('.js')) { - // Get the event name, by taking the command file and removing the ".js" from the end - const eventName = eventFiles[i].split('.')[0]; + // Loop through all the files in the folder + for (let i = 0; i < eventFiles.length; i++) { + // Ignore non-javascript files + if (eventFiles[i].includes('.js')) { + // Get the event name, by taking the command file and removing the ".js" from the end + const eventName = eventFiles[i].split('.')[0]; - // Get the file of the event - const file = require(`${process.cwd()}/${folder}/${eventName}.js`); + // Get the file of the event + const file = require(`${process.cwd()}/${folder}/${eventName}.js`); - // Initialise the event class - const event = new file; + // Initialise the event class + const event = new file; - // Set the client to emit to this event - this._client.on(eventName, event[event.run]); + // Set the client to emit to this event + this._client.on(eventName, event[event.run]); + } } + } else { + return generateResponse(false, 'Event folder does not exist'); } } } diff --git a/tests/src/client/util.test.js b/tests/src/client/util.test.js index 7bde9a4..51a372d 100644 --- a/tests/src/client/util.test.js +++ b/tests/src/client/util.test.js @@ -1,5 +1,6 @@ const util = require('../../../src/client/util'); const { readFileSync, read } = require('fs'); +const { test, expect } = require('@jest/globals'); // Mocks jest.mock('discord.js'); @@ -74,4 +75,12 @@ describe('util.loadCommand', () => { expect(res.valid).toBe(false); expect(res.message).toBe("test requires tester in it's configuration"); }); + + test('Should throw error if command folder does not exist', () => { + client.config.commands = "falsefile"; + let res = instance.loadCommand('testing', 'param1', message); + + expect(res.valid).toBe(false); + expect(res.message).toBe('Command folder does not exist'); + }); });