v2.0 #55

Merged
Vylpes merged 73 commits from develop into main 2021-10-04 19:45:15 +01:00
5 changed files with 95 additions and 72 deletions
Showing only changes of commit c816f182b2 - Show all commits

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",
"version": "1.0.4",
"version": "20.0.0",
"lockfileVersion": 1,
"requires": true,
"dependencies": {

View file

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

View file

@ -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');
}
}
}

View file

@ -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');
});
});