Update util to check if folder exists

This commit is contained in:
Vylpes 2021-02-07 14:12:56 +00:00
parent bebaf30d16
commit c816f182b2
5 changed files with 95 additions and 72 deletions

3
cmd-config.json Normal file
View file

@ -0,0 +1,3 @@
{
}

2
package-lock.json generated
View file

@ -1,6 +1,6 @@
{ {
"name": "vylbot-core", "name": "vylbot-core",
"version": "1.0.4", "version": "20.0.0",
"lockfileVersion": 1, "lockfileVersion": 1,
"requires": true, "requires": true,
"dependencies": { "dependencies": {

View file

@ -22,8 +22,8 @@
"bot", "bot",
"client" "client"
], ],
"repository": "github:vylpes/vylbot-core", "repository": "github:vylpes/vylbot-core",
"devDependencies": { "devDependencies": {
"eslint": "^7.17.0" "eslint": "^7.17.0"
} }
} }

View file

@ -20,6 +20,9 @@ class util {
// Get the current folder to check // Get the current folder to check
const folder = this._client.config.commands; const folder = this._client.config.commands;
// If the folder exists
if (existsSync(`${process.cwd()}/${folder}/`)) {
// If the file exists inside the folder
if (existsSync(`${process.cwd()}/${folder}/${name}.js`)) { if (existsSync(`${process.cwd()}/${folder}/${name}.js`)) {
// Require the command file, now that we know it exists and initialise it // Require the command file, now that we know it exists and initialise it
const commandFile = require(`${process.cwd()}/${folder}/${name}.js`); const commandFile = require(`${process.cwd()}/${folder}/${name}.js`);
@ -79,6 +82,9 @@ class util {
} else { } else {
return generateResponse(false, 'File does not exist'); return generateResponse(false, 'File does not exist');
} }
} else {
return generateResponse(false, 'Command folder does not exist');
}
} }
// Load the events // Load the events
@ -86,6 +92,8 @@ class util {
// Get the current folder to check // Get the current folder to check
const folder = this._client.config.events; const folder = this._client.config.events;
// If the folder exists
if (existsSync(`${process.cwd()}/${folder}/`)) {
// Get the files inside of this folder // Get the files inside of this folder
const eventFiles = readdirSync(`${process.cwd()}/${folder}/`); const eventFiles = readdirSync(`${process.cwd()}/${folder}/`);
@ -106,6 +114,9 @@ class util {
this._client.on(eventName, event[event.run]); this._client.on(eventName, event[event.run]);
} }
} }
} else {
return generateResponse(false, 'Event folder does not exist');
}
} }
} }

View file

@ -1,5 +1,6 @@
const util = require('../../../src/client/util'); const util = require('../../../src/client/util');
const { readFileSync, read } = require('fs'); const { readFileSync, read } = require('fs');
const { test, expect } = require('@jest/globals');
// Mocks // Mocks
jest.mock('discord.js'); jest.mock('discord.js');
@ -74,4 +75,12 @@ describe('util.loadCommand', () => {
expect(res.valid).toBe(false); expect(res.valid).toBe(false);
expect(res.message).toBe("test requires tester in it's configuration"); 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');
});
}); });