Update util to check if folder exists
This commit is contained in:
parent
bebaf30d16
commit
c816f182b2
5 changed files with 95 additions and 72 deletions
3
cmd-config.json
Normal file
3
cmd-config.json
Normal file
|
@ -0,0 +1,3 @@
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
2
package-lock.json
generated
2
package-lock.json
generated
|
@ -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": {
|
||||||
|
|
|
@ -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"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -20,64 +20,70 @@ 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 (existsSync(`${process.cwd()}/${folder}/${name}.js`)) {
|
// If the folder exists
|
||||||
// Require the command file, now that we know it exists and initialise it
|
if (existsSync(`${process.cwd()}/${folder}/`)) {
|
||||||
const commandFile = require(`${process.cwd()}/${folder}/${name}.js`);
|
// If the file exists inside the folder
|
||||||
const command = new commandFile();
|
if (existsSync(`${process.cwd()}/${folder}/${name}.js`)) {
|
||||||
|
// Require the command file, now that we know it exists and initialise it
|
||||||
// Require the command config file and get the config for the current command
|
const commandFile = require(`${process.cwd()}/${folder}/${name}.js`);
|
||||||
const configJson = this._client.commandConfig;
|
const command = new commandFile();
|
||||||
const config = configJson[name];
|
|
||||||
|
// Require the command config file and get the config for the current command
|
||||||
// Get the list of required configurations the command needs
|
const configJson = this._client.commandConfig;
|
||||||
const commandConfigs = command.configs;
|
const config = configJson[name];
|
||||||
|
|
||||||
// Loop through all the required configs of the command
|
// Get the list of required configurations the command needs
|
||||||
for (const i in commandConfigs) {
|
const commandConfigs = command.configs;
|
||||||
// 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`);
|
// Loop through all the required configs of the command
|
||||||
if (!config[commandConfigs[i]]) return generateResponse(false, `${commandFile.name} requires ${commandConfigs[i]} in it's configuration`);
|
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`);
|
||||||
// Get the roles required for this command to run
|
if (!config[commandConfigs[i]]) return generateResponse(false, `${commandFile.name} requires ${commandConfigs[i]} in it's configuration`);
|
||||||
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 roles required for this command to run
|
||||||
// Get the ids of the users that are only permitted to run this command
|
const requiredRoles = command.roles;
|
||||||
const users = command.users;
|
|
||||||
|
// Get the category, if there is no category, set it to a default string
|
||||||
// If the command has any limits, limit the command, otherwise default to anyone
|
if (!command.category) command.category = "none";
|
||||||
if (users.length > 0) {
|
|
||||||
if (!users.includes(message.member.id)) {
|
// Loop through all roles required
|
||||||
message.reply(`You do not have permission to run this command`);
|
for (const i in requiredRoles) {
|
||||||
return generateResponse(false, "You do not have permission to run this command");
|
// 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 {
|
} 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
|
// Get the current folder to check
|
||||||
const folder = this._client.config.events;
|
const folder = this._client.config.events;
|
||||||
|
|
||||||
// Get the files inside of this folder
|
// If the folder exists
|
||||||
const eventFiles = readdirSync(`${process.cwd()}/${folder}/`);
|
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
|
// Loop through all the files in the folder
|
||||||
for (let i = 0; i < eventFiles.length; i++) {
|
for (let i = 0; i < eventFiles.length; i++) {
|
||||||
// Ignore non-javascript files
|
// Ignore non-javascript files
|
||||||
if (eventFiles[i].includes('.js')) {
|
if (eventFiles[i].includes('.js')) {
|
||||||
// Get the event name, by taking the command file and removing the ".js" from the end
|
// Get the event name, by taking the command file and removing the ".js" from the end
|
||||||
const eventName = eventFiles[i].split('.')[0];
|
const eventName = eventFiles[i].split('.')[0];
|
||||||
|
|
||||||
// Get the file of the event
|
// Get the file of the event
|
||||||
const file = require(`${process.cwd()}/${folder}/${eventName}.js`);
|
const file = require(`${process.cwd()}/${folder}/${eventName}.js`);
|
||||||
|
|
||||||
// Initialise the event class
|
// Initialise the event class
|
||||||
const event = new file;
|
const event = new file;
|
||||||
|
|
||||||
// Set the client to emit to this event
|
// Set the client to emit to this event
|
||||||
this._client.on(eventName, event[event.run]);
|
this._client.on(eventName, event[event.run]);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
|
return generateResponse(false, 'Event folder does not exist');
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -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');
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
Reference in a new issue