v2.0 #55
24 changed files with 5435 additions and 111 deletions
1
.eslintignore
Normal file
1
.eslintignore
Normal file
|
@ -0,0 +1 @@
|
|||
tests
|
49
.eslintrc
Normal file
49
.eslintrc
Normal file
|
@ -0,0 +1,49 @@
|
|||
{
|
||||
"parserOptions": {
|
||||
"ecmaVersion": 6
|
||||
},
|
||||
"extends": [
|
||||
"eslint:recommended"
|
||||
],
|
||||
"rules": {
|
||||
"camelcase": "error",
|
||||
"brace-style": [
|
||||
"error",
|
||||
"1tbs"
|
||||
],
|
||||
"comma-dangle": [
|
||||
"error",
|
||||
"never"
|
||||
],
|
||||
"comma-spacing": [
|
||||
"error",
|
||||
{
|
||||
"before": false,
|
||||
"after": true
|
||||
}
|
||||
],
|
||||
"comma-style": [
|
||||
"error",
|
||||
"last"
|
||||
],
|
||||
"arrow-body-style": [
|
||||
"error",
|
||||
"as-needed"
|
||||
],
|
||||
"arrow-parens": [
|
||||
"error",
|
||||
"as-needed"
|
||||
],
|
||||
"arrow-spacing": "error",
|
||||
"no-var": "error",
|
||||
"prefer-template": "error",
|
||||
"prefer-const": "error"
|
||||
},
|
||||
"globals": {
|
||||
"exports": "writable",
|
||||
"module": "writable",
|
||||
"require": "writable",
|
||||
"process": "writable",
|
||||
"console": "writable"
|
||||
}
|
||||
}
|
7
.gitignore
vendored
7
.gitignore
vendored
|
@ -106,8 +106,11 @@ dist
|
|||
# VylBot-Core Testing Files
|
||||
commands/
|
||||
events/
|
||||
bot.js
|
||||
config.json
|
||||
/bot.js
|
||||
/config.json
|
||||
|
||||
!tests/commands/
|
||||
!tests/events/
|
||||
|
||||
# Linux Environment Files
|
||||
*.swp
|
||||
|
|
17
.gitlab-ci.yml
Normal file
17
.gitlab-ci.yml
Normal file
|
@ -0,0 +1,17 @@
|
|||
image: node
|
||||
|
||||
stages:
|
||||
- lint
|
||||
- test
|
||||
|
||||
eslint:
|
||||
stage: lint
|
||||
script:
|
||||
- npm install
|
||||
- npm run lint
|
||||
|
||||
jest:
|
||||
stage: test
|
||||
script:
|
||||
- npm install
|
||||
- npm test
|
3
jest.config.js
Normal file
3
jest.config.js
Normal file
|
@ -0,0 +1,3 @@
|
|||
module.exports = {
|
||||
testEnvironment: 'node'
|
||||
}
|
4655
package-lock.json
generated
4655
package-lock.json
generated
File diff suppressed because it is too large
Load diff
12
package.json
12
package.json
|
@ -4,13 +4,16 @@
|
|||
"description": "A discord client based upon discord.js",
|
||||
"main": "./src/index",
|
||||
"scripts": {
|
||||
"test": "echo \"No Tests Specified\""
|
||||
"test": "jest --coverage",
|
||||
"lint": "eslint .",
|
||||
"lint:fix": "eslint . --fix"
|
||||
},
|
||||
"author": "Vylpes",
|
||||
"license": "MIT",
|
||||
"funding": "https://ko-fi.com/gravitysoftware",
|
||||
"dependencies": {
|
||||
"discord.js": "^12.3.1"
|
||||
"discord.js": "^12.3.1",
|
||||
"jest": "^26.6.3"
|
||||
},
|
||||
"bugs": "https://github.com/vylpes/vylbot-core/issues",
|
||||
"homepage": "https://github.com/vylpes/vylbot-core",
|
||||
|
@ -19,5 +22,8 @@
|
|||
"bot",
|
||||
"client"
|
||||
],
|
||||
"repository": "github:vylpes/vylbot-core"
|
||||
"repository": "github:vylpes/vylbot-core",
|
||||
"devDependencies": {
|
||||
"eslint": "^7.17.0"
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,32 +1,40 @@
|
|||
// Required Components
|
||||
const { Client } = require('discord.js');
|
||||
const { existsSync } = require('fs');
|
||||
const { validateConfig } = require('./validation');
|
||||
|
||||
const events = require('./events');
|
||||
const util = require('./util');
|
||||
|
||||
// Required JSON
|
||||
const expectedConfig = require('../json/expectedConfig.json');
|
||||
|
||||
// Client Class
|
||||
class client extends Client {
|
||||
constructor(config) {
|
||||
// Call Discord.JS Client
|
||||
constructor(config, commandConfig) {
|
||||
// Call Discord.JS Client
|
||||
super();
|
||||
|
||||
// Set the client's configuration, initialise events, initialise utilities
|
||||
// Set the client's configuration, initialise events, initialise utilities
|
||||
this.config = config;
|
||||
this.commandConfig = commandConfig;
|
||||
this.events = new events();
|
||||
this.util = new util(this);
|
||||
}
|
||||
|
||||
// Method to start the bot
|
||||
start() {
|
||||
// Events to handle commands
|
||||
// Check the bot is ready to start
|
||||
if (!this._config) throw "Config has not been set";
|
||||
if (!this._commandConfig) throw "Command Config has not been set";
|
||||
|
||||
// Events to handle commands
|
||||
super.on("message", this.events.message);
|
||||
super.on("ready", this.events.ready);
|
||||
|
||||
// Login to discord using Discord.JS
|
||||
|
||||
// Login to discord using Discord.JS
|
||||
super.login(this._config.token);
|
||||
|
||||
// Load events
|
||||
// Load events
|
||||
this.util.loadEvents();
|
||||
}
|
||||
|
||||
|
@ -37,26 +45,20 @@ class client extends Client {
|
|||
|
||||
set config(config) {
|
||||
// Validate the config
|
||||
|
||||
// Only allow config to be set once and only as a JSON object
|
||||
if (this._config) throw "Config has already been set";
|
||||
if (typeof config != "object") throw "Config is not a JSON object";
|
||||
|
||||
// Make sure the token and prefix are strings
|
||||
if (typeof config.token != "string") throw "Token is not a string";
|
||||
if (typeof config.prefix != "string") throw "Prefix is not a string";
|
||||
|
||||
// Make sure the command config string is set and the file exists
|
||||
if (typeof config.cmdconfig != "string") throw "Cmdconfig is not a string";
|
||||
if (!existsSync(config.cmdconfig)) throw `The file '${config.cmdconfig}' does not exist`;
|
||||
|
||||
// Make sure commands and events are arrays, each item inside will be validated later
|
||||
if (typeof config.commands != "object") throw "Commands is not a string";
|
||||
if (typeof config.events != "object") throw "Events is not a string";
|
||||
|
||||
const val = validateConfig(config, expectedConfig);
|
||||
if (!val.valid) throw val.message;
|
||||
|
||||
this._config = config;
|
||||
}
|
||||
|
||||
// Command Config
|
||||
get commandConfig() {
|
||||
return this._commandConfig;
|
||||
}
|
||||
|
||||
set commandConfig(config) {
|
||||
this._commandConfig = config;
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = client;
|
||||
|
|
|
@ -3,21 +3,33 @@ class event {
|
|||
// Emit when a message is sent
|
||||
// Used to check for commands
|
||||
message(message) {
|
||||
// Make sure command is sent within a guild and not by a bot, otherwise return and ignore
|
||||
if (!message.guild) return;
|
||||
if (message.author.bot) return;
|
||||
// Make sure command is sent within a guild and not by a bot, otherwise return and ignore
|
||||
if (!message) return false;
|
||||
if (!message.guild) return false;
|
||||
if (message.author.bot) return false;
|
||||
|
||||
// Get the prefix from the config
|
||||
let prefix = this.config.prefix;
|
||||
// Get the prefix from the config
|
||||
const prefix = this.config.prefix;
|
||||
|
||||
// If the message starts with the prefix, then treat it as a command
|
||||
if (message.content.substring(0, prefix.length).toLowerCase() == prefix.toLowerCase()) {
|
||||
// Get the arguments in the message, after the first space (after the command name)
|
||||
let args = message.content.substring(prefix.length).split(" ");
|
||||
let name = args.shift();
|
||||
const args = message.content.substring(prefix.length).split(" ");
|
||||
const name = args.shift();
|
||||
|
||||
// Load the command from the util class
|
||||
this.util.loadCommand(name, args, message);
|
||||
const res = this.util.loadCommand(name, args, message);
|
||||
|
||||
if (!res.valid) {
|
||||
if (res.message != 'File does not exist') throw res.message;
|
||||
}
|
||||
|
||||
return {
|
||||
"prefix": prefix,
|
||||
"name": name,
|
||||
"args": args,
|
||||
"message": message
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -1,5 +1,12 @@
|
|||
// Required Components
|
||||
const { stat, readdirSync } = require('fs');
|
||||
const { readdirSync, existsSync } = require('fs');
|
||||
|
||||
function generateResponse(isValid, message) {
|
||||
return {
|
||||
"valid": isValid,
|
||||
"message": message || "No message was given"
|
||||
}
|
||||
}
|
||||
|
||||
// Util Class
|
||||
class util {
|
||||
|
@ -10,97 +17,91 @@ class util {
|
|||
|
||||
// Load a command and send the arguments with it
|
||||
loadCommand(name, args, message) {
|
||||
// Loop through all folders set in config
|
||||
// c = command folder index
|
||||
for (let c = 0; c < this._client.config.commands.length; c++) {
|
||||
// Get the current folder to check
|
||||
let folder = this._client.config.commands[c];
|
||||
// Get the current folder to check
|
||||
const folder = this._client.config.commands;
|
||||
|
||||
// See if the folder being checked has the command being sent
|
||||
stat(`${process.cwd()}/${folder}/${name}.js`, err => {
|
||||
// If no error, attempt to run the command
|
||||
if (err == null) {
|
||||
// Require the command file, now that we know it exists and initialise it
|
||||
let commandFile = require(`${process.cwd()}/${folder}/${name}.js`);
|
||||
let command = new commandFile();
|
||||
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
|
||||
let configString = this._client.config.cmdconfig
|
||||
let configFile = require(`${process.cwd()}/${configString}`);
|
||||
let config = configFile[name];
|
||||
// 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
|
||||
let commandConfigs = command.configs;
|
||||
// Get the list of required configurations the command needs
|
||||
const commandConfigs = command.configs;
|
||||
|
||||
// Loop through all the required configs of the command
|
||||
for (let i = 0; i < commandConfigs.length; i++) {
|
||||
// If the command doesn't have the configs in the config string, throw an error
|
||||
if (!config) throw `${commandFile.name} requires ${commandConfigs[i]} in it's configuration`;
|
||||
if (!config[commandConfigs[i]]) throw `${commandFile.name} requires ${commandConfigs[i]} in it's configuration`;
|
||||
}
|
||||
// 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
|
||||
let requiredRoles = command.roles;
|
||||
// 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";
|
||||
// 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 (let i = 0; i < requiredRoles.length; i++) {
|
||||
// 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;
|
||||
}
|
||||
}
|
||||
// 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
|
||||
let users = command.users;
|
||||
// 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;
|
||||
}
|
||||
}
|
||||
// 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
|
||||
});
|
||||
} else if (err.code === 'ENOENT') {
|
||||
// FILE DOESN'T 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');
|
||||
}
|
||||
}
|
||||
|
||||
// Load the events
|
||||
loadEvents() {
|
||||
// Loop through all the event folders
|
||||
for (let e = 0; e < this._client.config.events.length; e++) {
|
||||
// Get the current folder to check
|
||||
let folder = this._client.config.events[e];
|
||||
// Get the current folder to check
|
||||
const folder = this._client.config.events;
|
||||
|
||||
// Get the files inside of this folder
|
||||
let eventFiles = readdirSync(`${process.cwd()}/${folder}/`);
|
||||
|
||||
// Loop through all the files in the folder
|
||||
for (let i = 0; i < eventFiles.length; i++) {
|
||||
// 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
|
||||
let eventName = eventFiles[i].split('.')[0];
|
||||
const eventName = eventFiles[i].split('.')[0];
|
||||
|
||||
// Get the file of the event
|
||||
let file = require(`${process.cwd()}/${folder}/${eventName}.js`);
|
||||
|
||||
const file = require(`${process.cwd()}/${folder}/${eventName}.js`);
|
||||
|
||||
// Initialise the event class
|
||||
let event = new file;
|
||||
|
||||
const event = new file;
|
||||
|
||||
// Set the client to emit to this event
|
||||
this._client.on(eventName, event[event.run]);
|
||||
}
|
||||
|
|
33
src/client/validation.js
Normal file
33
src/client/validation.js
Normal file
|
@ -0,0 +1,33 @@
|
|||
function generateResponse(isValid, message) {
|
||||
return {
|
||||
"valid": isValid,
|
||||
"message": message || "No message was given"
|
||||
}
|
||||
}
|
||||
|
||||
function validateConfig(config, expect) {
|
||||
if (!config) return generateResponse(false, "Invalid config");
|
||||
if (typeof config != "object") return generateResponse(false, "Invalid config");
|
||||
|
||||
if (!expect) return generateResponse(false, "Invalid expect");
|
||||
if (typeof expect != "object") return generateResponse(false, "Invalid expect");
|
||||
|
||||
const keys = Object.keys(expect);
|
||||
|
||||
for (const i in keys) {
|
||||
const e = expect[keys[i]];
|
||||
|
||||
if (!config[keys[i]])
|
||||
return generateResponse(false, `'${keys[i]}' is not defined`);
|
||||
|
||||
if (typeof config[keys[i]] != e.type)
|
||||
return generateResponse(false, `Invalid type of '${keys[i]}'. Was '${typeof config[keys[i]]}', Expected '${e.type}'`);
|
||||
}
|
||||
|
||||
return generateResponse(true);
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
generateResponse,
|
||||
validateConfig
|
||||
};
|
|
@ -1,5 +1,3 @@
|
|||
const discord = require('discord.js');
|
||||
|
||||
module.exports = {
|
||||
client: require('./client/client'),
|
||||
command: require('./type/command'),
|
||||
|
|
14
src/json/expectedConfig.json
Normal file
14
src/json/expectedConfig.json
Normal file
|
@ -0,0 +1,14 @@
|
|||
{
|
||||
"token": {
|
||||
"type": "string"
|
||||
},
|
||||
"prefix": {
|
||||
"type": "string"
|
||||
},
|
||||
"commands": {
|
||||
"type": "string"
|
||||
},
|
||||
"events": {
|
||||
"type": "string"
|
||||
}
|
||||
}
|
14
tests/commands/testing.js
Normal file
14
tests/commands/testing.js
Normal file
|
@ -0,0 +1,14 @@
|
|||
const { command } = require('../../src');
|
||||
|
||||
class test extends command {
|
||||
constructor() {
|
||||
super("test");
|
||||
super.configs = "tester";
|
||||
}
|
||||
|
||||
test(context) {
|
||||
context.message.reply(`Testing done by ${context.config.tester}`);
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = test;
|
0
tests/events/.gitkeep
Normal file
0
tests/events/.gitkeep
Normal file
5
tests/json/commandConfig.json
Normal file
5
tests/json/commandConfig.json
Normal file
|
@ -0,0 +1,5 @@
|
|||
{
|
||||
"testing": {
|
||||
"tester": "General Kenobi"
|
||||
}
|
||||
}
|
6
tests/json/config.json
Normal file
6
tests/json/config.json
Normal file
|
@ -0,0 +1,6 @@
|
|||
{
|
||||
"token": "TOKEN",
|
||||
"prefix": "d!",
|
||||
"commands": "tests/commands",
|
||||
"events": "tests/events"
|
||||
}
|
7
tests/json/message.json
Normal file
7
tests/json/message.json
Normal file
|
@ -0,0 +1,7 @@
|
|||
{
|
||||
"guild": "000000000000000000",
|
||||
"author": {
|
||||
"bot": false
|
||||
},
|
||||
"content": "d!testing param1"
|
||||
}
|
57
tests/src/client/client.test.js
Normal file
57
tests/src/client/client.test.js
Normal file
|
@ -0,0 +1,57 @@
|
|||
const { client } = require('../../../src');
|
||||
const { readFileSync } = require('fs');
|
||||
|
||||
// Mocks
|
||||
jest.mock('discord.js');
|
||||
|
||||
describe('Client Tests', () => {
|
||||
let instance;
|
||||
let config;
|
||||
let commandConfig;
|
||||
|
||||
beforeEach(() => {
|
||||
config = JSON.parse(readFileSync('tests/json/config.json'));
|
||||
commandConfig = JSON.parse(readFileSync('tests/json/commandConfig.json'));
|
||||
});
|
||||
|
||||
test('Configure Client (Correct paramaters)', () => {
|
||||
instance = new client(config, commandConfig);
|
||||
|
||||
expect(instance.config).toBe(config);
|
||||
expect(instance.events).toBeDefined();
|
||||
expect(instance.util).toBeDefined();
|
||||
});
|
||||
|
||||
test('Configure Client (Incorrect parameters: token)', () => {
|
||||
expect(() => {
|
||||
delete config.token;
|
||||
instance = new client(config, commandConfig);
|
||||
}).toThrow();
|
||||
});
|
||||
|
||||
test('Configure Client (Incorrect parameters: prefix)', () => {
|
||||
expect(() => {
|
||||
delete config.prefix;
|
||||
instance = new client(config, commandConfig);
|
||||
}).toThrow();
|
||||
});
|
||||
|
||||
test('Configure Client (Incorrect parameters: commands)', () => {
|
||||
expect(() => {
|
||||
delete config.commands;
|
||||
instance = new client(config, commandConfig);
|
||||
}).toThrow();
|
||||
});
|
||||
|
||||
test('Configure Client (Incorrect parameters: events)', () => {
|
||||
expect(() => {
|
||||
delete config.events;
|
||||
instance = new client(config, commandConfig);
|
||||
}).toThrow();
|
||||
});
|
||||
|
||||
test('Start Client', () => {
|
||||
instance = new client(config, commandConfig);
|
||||
instance.start();
|
||||
});
|
||||
});
|
89
tests/src/client/events.test.js
Normal file
89
tests/src/client/events.test.js
Normal file
|
@ -0,0 +1,89 @@
|
|||
const events = require('../../../src/client/events');
|
||||
const { readFileSync } = require('fs');
|
||||
|
||||
// Mocks
|
||||
jest.mock('discord.js');
|
||||
|
||||
describe('events.message', () => {
|
||||
let instance;
|
||||
let message;
|
||||
let config;
|
||||
|
||||
beforeEach(() => {
|
||||
instance = new events();
|
||||
message = JSON.parse(readFileSync('tests/json/message.json'));
|
||||
config = JSON.parse(readFileSync('tests/json/config.json'));
|
||||
|
||||
instance.config = config;
|
||||
|
||||
instance.util = jest.fn();
|
||||
instance.util.loadCommand = jest.fn(() => {
|
||||
return {
|
||||
"valid": true,
|
||||
"message": "No message was set"
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
test('If no message should return', () => {
|
||||
const res = instance.message();
|
||||
|
||||
expect(res).toBe(false);
|
||||
});
|
||||
|
||||
test('If no guild should return', () => {
|
||||
delete message.guild;
|
||||
const res = instance.message(message);
|
||||
|
||||
expect(res).toBe(false);
|
||||
});
|
||||
|
||||
test('If author is a bot should return', () => {
|
||||
message.author.bot = true;
|
||||
const res = instance.message(message);
|
||||
|
||||
expect(res).toBe(false);
|
||||
});
|
||||
|
||||
test('Should loadCommand', () => {
|
||||
const res = instance.message(message);
|
||||
|
||||
expect(instance.util.loadCommand).toHaveBeenCalledTimes(1);
|
||||
expect(instance.util.loadCommand).toHaveBeenCalledWith('testing', ['param1'], message);
|
||||
});
|
||||
|
||||
test('Should return correct values', () => {
|
||||
const res = instance.message(message);
|
||||
|
||||
expect(res.prefix).toBe('d!');
|
||||
expect(res.name).toBe('testing');
|
||||
expect(res.args[0]).toBe('param1');
|
||||
expect(res.message).toBe(message);
|
||||
});
|
||||
|
||||
test('Should throw if response is invalid', () => {
|
||||
instance.util.loadCommand = jest.fn(() => {
|
||||
return {
|
||||
"valid": false,
|
||||
"message": "Invalid"
|
||||
}
|
||||
});
|
||||
|
||||
expect(() => {
|
||||
instance.message(message)
|
||||
}).toThrow('Invalid');
|
||||
});
|
||||
|
||||
test('Should not throw if file does not exist', () => {
|
||||
instance.util.loadCommand = jest.fn(() => {
|
||||
return {
|
||||
"valid": false,
|
||||
"message": "File does not exist"
|
||||
}
|
||||
});
|
||||
|
||||
expect(() => {
|
||||
instance.message(message)
|
||||
}).not.toThrow();
|
||||
});
|
||||
});
|
77
tests/src/client/util.test.js
Normal file
77
tests/src/client/util.test.js
Normal file
|
@ -0,0 +1,77 @@
|
|||
const util = require('../../../src/client/util');
|
||||
const { readFileSync, read } = require('fs');
|
||||
|
||||
// Mocks
|
||||
jest.mock('discord.js');
|
||||
const fs = jest.createMockFromModule('fs');
|
||||
|
||||
fs.stat = jest.fn((path, cb) => {
|
||||
cb(null);
|
||||
});
|
||||
|
||||
describe('util.constructor', () => {
|
||||
let instance;
|
||||
let client;
|
||||
|
||||
beforeEach(() => {
|
||||
client = jest.fn();
|
||||
instance = new util(client);
|
||||
});
|
||||
|
||||
test('Should set client', () => {
|
||||
expect(instance._client).toBeDefined();
|
||||
});
|
||||
});
|
||||
|
||||
describe('util.loadCommand', () => {
|
||||
let instance;
|
||||
let message;
|
||||
let client;
|
||||
|
||||
beforeEach(() => {
|
||||
client = jest.fn();
|
||||
client.config = JSON.parse(readFileSync('tests/json/config.json'));
|
||||
client.commandConfig = JSON.parse(readFileSync('tests/json/commandConfig.json'));
|
||||
|
||||
instance = new util(client);
|
||||
message = JSON.parse(readFileSync('tests/json/message.json'));
|
||||
message.reply = jest.fn();
|
||||
});
|
||||
|
||||
test('Should load command correctly', () => {
|
||||
let res = instance.loadCommand('testing', 'param1', message);
|
||||
|
||||
expect(res.valid).toBe(true);
|
||||
expect(res.message).toBe("loaded command 'testing' with arguments 'param1'");
|
||||
});
|
||||
|
||||
test('Should load command correctly (no arguments)', () => {
|
||||
let res = instance.loadCommand('testing', '', message);
|
||||
|
||||
expect(res.valid).toBe(true);
|
||||
expect(res.message).toBe("loaded command 'testing' with arguments ''");
|
||||
});
|
||||
|
||||
test('Should be invalid if it tries to load an undefined command', () => {
|
||||
let res = instance.loadCommand('testingz', '', message);
|
||||
|
||||
expect(res.valid).toBe(false);
|
||||
expect(res.message).toBe('File does not exist');
|
||||
});
|
||||
|
||||
test('Should be invalid if incorrect configs', () => {
|
||||
delete client.commandConfig.testing;
|
||||
let res = instance.loadCommand('testing', 'param1', message);
|
||||
|
||||
expect(res.valid).toBe(false);
|
||||
expect(res.message).toBe("test requires tester in it's configuration");
|
||||
});
|
||||
|
||||
test('Should be invalid if incorrect configs (single config)', () => {
|
||||
delete client.commandConfig.testing.tester;
|
||||
let res = instance.loadCommand('testing', 'param1', message);
|
||||
|
||||
expect(res.valid).toBe(false);
|
||||
expect(res.message).toBe("test requires tester in it's configuration");
|
||||
});
|
||||
});
|
72
tests/src/client/validation.test.js
Normal file
72
tests/src/client/validation.test.js
Normal file
|
@ -0,0 +1,72 @@
|
|||
const { generateResponse, validateConfig } = require('../../../src/client/validation');
|
||||
const { readFileSync } = require('fs');
|
||||
|
||||
describe('Validation: generateResponse', () => {
|
||||
test('Returns the corect response', () => {
|
||||
const isValidWithMessage = generateResponse(true, "Test Message");
|
||||
const isValidNoMessage = generateResponse(true);
|
||||
const notValidWithMessage = generateResponse(false, "Test Message");
|
||||
const notValidNoMessage = generateResponse(false);
|
||||
|
||||
expect(isValidWithMessage.valid).toBe(true);
|
||||
expect(isValidWithMessage.message).toBe('Test Message');
|
||||
|
||||
expect(isValidNoMessage.valid).toBe(true);
|
||||
expect(isValidNoMessage.message).toBe('No message was given');
|
||||
|
||||
expect(notValidWithMessage.valid).toBe(false);
|
||||
expect(notValidWithMessage.message).toBe('Test Message');
|
||||
|
||||
expect(notValidNoMessage.valid).toBe(false);
|
||||
expect(notValidNoMessage.message).toBe('No message was given');
|
||||
});
|
||||
});
|
||||
|
||||
describe('Validation: validateConfig', () => {
|
||||
let config;
|
||||
let expected;
|
||||
|
||||
beforeEach(() => {
|
||||
config = JSON.parse(readFileSync('tests/json/config.json'));
|
||||
expected = JSON.parse(readFileSync('src/json/expectedConfig.json'));
|
||||
});
|
||||
|
||||
test('Validates expected config as valid', () => {
|
||||
const res = validateConfig(config, expected);
|
||||
|
||||
expect(res.valid).toBe(true);
|
||||
expect(res.message).toBe('No message was given');
|
||||
});
|
||||
|
||||
test('Validates unexpected config as invalid (token)', () => {
|
||||
delete config.token;
|
||||
const res = validateConfig(config, expected);
|
||||
|
||||
expect(res.valid).toBe(false);
|
||||
expect(res.message).toBe("'token' is not defined");
|
||||
});
|
||||
|
||||
test('Validates unexpected config as invalid (prefix)', () => {
|
||||
delete config.prefix;
|
||||
const res = validateConfig(config, expected);
|
||||
|
||||
expect(res.valid).toBe(false);
|
||||
expect(res.message).toBe("'prefix' is not defined");
|
||||
});
|
||||
|
||||
test('Validates unexpected config as invalid (commands)', () => {
|
||||
delete config.commands;
|
||||
const res = validateConfig(config, expected);
|
||||
|
||||
expect(res.valid).toBe(false);
|
||||
expect(res.message).toBe("'commands' is not defined");
|
||||
});
|
||||
|
||||
test('Validates unexpected config as invalid (events)', () => {
|
||||
delete config.events;
|
||||
const res = validateConfig(config, expected);
|
||||
|
||||
expect(res.valid).toBe(false);
|
||||
expect(res.message).toBe("'events' is not defined");
|
||||
});
|
||||
});
|
190
tests/src/type/command.test.js
Normal file
190
tests/src/type/command.test.js
Normal file
|
@ -0,0 +1,190 @@
|
|||
const command = require('../../../src/type/command');
|
||||
|
||||
describe('Command: constructor', () => {
|
||||
let instance;
|
||||
|
||||
beforeEach(() => {
|
||||
instance = new command("test");
|
||||
});
|
||||
|
||||
test('Command run is set correctly', () => {
|
||||
expect(instance.run).toBe("test");
|
||||
});
|
||||
|
||||
test('Command roles is set correctly', () => {
|
||||
expect(typeof instance._roles).toBe('object');
|
||||
expect(instance._roles.length).toBe(0);
|
||||
});
|
||||
|
||||
test('Command configs is set correctly', () => {
|
||||
expect(typeof instance._configs).toBe('object');
|
||||
expect(instance._configs.length).toBe(0);
|
||||
});
|
||||
|
||||
test('Command users is set correctly', () => {
|
||||
expect(typeof instance._users).toBe('object');
|
||||
expect(instance._users.length).toBe(0);
|
||||
});
|
||||
});
|
||||
|
||||
describe('Command: description', () => {
|
||||
let instance;
|
||||
|
||||
beforeEach(() => {
|
||||
instance = new command("test");
|
||||
});
|
||||
|
||||
test('Setting description', () => {
|
||||
instance.description = "desc";
|
||||
expect(instance._description).toBe("desc");
|
||||
});
|
||||
|
||||
test('Getting description', () => {
|
||||
instance.description = "desc";
|
||||
expect(instance.description).toBe("desc");
|
||||
});
|
||||
});
|
||||
|
||||
describe('Command: category', () => {
|
||||
let instance;
|
||||
|
||||
beforeEach(() => {
|
||||
instance = new command("test");
|
||||
});
|
||||
|
||||
test('Setting category', () => {
|
||||
instance.category = "cat";
|
||||
expect(instance._category).toBe("cat");
|
||||
});
|
||||
|
||||
test('Getting category', () => {
|
||||
instance.category = "cat";
|
||||
expect(instance.category).toBe("cat");
|
||||
});
|
||||
});
|
||||
|
||||
describe('Command: usage', () => {
|
||||
let instance;
|
||||
|
||||
beforeEach(() => {
|
||||
instance = new command("test");
|
||||
});
|
||||
|
||||
test('Setting usage', () => {
|
||||
instance.usage = "use";
|
||||
expect(instance._usage).toBe("use");
|
||||
});
|
||||
|
||||
test('Getting usage', () => {
|
||||
instance.usage = "use";
|
||||
expect(instance.usage).toBe("use");
|
||||
});
|
||||
});
|
||||
|
||||
describe('Command: roles', () => {
|
||||
let instance;
|
||||
|
||||
beforeEach(() => {
|
||||
instance = new command("test");
|
||||
});
|
||||
|
||||
test('Setting roles (1 role)', () => {
|
||||
instance.roles = "role0";
|
||||
expect(instance._roles.length).toBe(1);
|
||||
expect(instance._roles[0]).toBe("role0");
|
||||
});
|
||||
|
||||
test('Getting roles (1 role)', () => {
|
||||
instance.roles = "role0";
|
||||
expect(instance.roles.length).toBe(1);
|
||||
expect(instance.roles[0]).toBe("role0");
|
||||
});
|
||||
|
||||
test('Setting roles (2 roles)', () => {
|
||||
instance.roles = "role0";
|
||||
instance.roles = "role1";
|
||||
expect(instance._roles.length).toBe(2);
|
||||
expect(instance._roles[0]).toBe("role0");
|
||||
expect(instance._roles[1]).toBe("role1");
|
||||
});
|
||||
|
||||
test('Getting roles (2 roles)', () => {
|
||||
instance.roles = "role0";
|
||||
instance.roles = "role1";
|
||||
expect(instance.roles.length).toBe(2);
|
||||
expect(instance.roles[0]).toBe("role0");
|
||||
expect(instance.roles[1]).toBe("role1");
|
||||
});
|
||||
});
|
||||
|
||||
describe('Command: configs', () => {
|
||||
let instance;
|
||||
|
||||
beforeEach(() => {
|
||||
instance = new command("test");
|
||||
});
|
||||
|
||||
test('Setting configs (1 config)', () => {
|
||||
instance.configs = "config0";
|
||||
expect(instance._configs.length).toBe(1);
|
||||
expect(instance._configs[0]).toBe("config0");
|
||||
});
|
||||
|
||||
test('Getting configs (1 config)', () => {
|
||||
instance.configs = "config0";
|
||||
expect(instance.configs.length).toBe(1);
|
||||
expect(instance.configs[0]).toBe("config0");
|
||||
});
|
||||
|
||||
test('Setting configs (2 configs)', () => {
|
||||
instance.configs = "config0";
|
||||
instance.configs = "config1";
|
||||
expect(instance._configs.length).toBe(2);
|
||||
expect(instance._configs[0]).toBe("config0");
|
||||
expect(instance._configs[1]).toBe("config1");
|
||||
});
|
||||
|
||||
test('Getting configs (2 configs)', () => {
|
||||
instance.configs = "config0";
|
||||
instance.configs = "config1";
|
||||
expect(instance.configs.length).toBe(2);
|
||||
expect(instance.configs[0]).toBe("config0");
|
||||
expect(instance.configs[1]).toBe("config1");
|
||||
});
|
||||
});
|
||||
|
||||
describe('Command: users', () => {
|
||||
let instance;
|
||||
|
||||
beforeEach(() => {
|
||||
instance = new command("test");
|
||||
});
|
||||
|
||||
test('Setting users (1 user)', () => {
|
||||
instance.users = "user0";
|
||||
expect(instance._users.length).toBe(1);
|
||||
expect(instance._users[0]).toBe("user0");
|
||||
});
|
||||
|
||||
test('Getting users (1 user)', () => {
|
||||
instance.users = "user0";
|
||||
expect(instance.users.length).toBe(1);
|
||||
expect(instance.users[0]).toBe("user0");
|
||||
});
|
||||
|
||||
test('Setting users (2 user)', () => {
|
||||
instance.users = "user0";
|
||||
instance.users = "user1";
|
||||
expect(instance._users.length).toBe(2);
|
||||
expect(instance._users[0]).toBe("user0");
|
||||
expect(instance._users[1]).toBe("user1");
|
||||
});
|
||||
|
||||
test('Getting users (2 users)', () => {
|
||||
instance.users = "user0";
|
||||
instance.users = "user1";
|
||||
expect(instance.users.length).toBe(2);
|
||||
expect(instance.users[0]).toBe("user0");
|
||||
expect(instance.users[1]).toBe("user1");
|
||||
});
|
||||
});
|
13
tests/src/type/event.test.js
Normal file
13
tests/src/type/event.test.js
Normal file
|
@ -0,0 +1,13 @@
|
|||
const event = require('../../../src/type/event');
|
||||
|
||||
describe('Event: constructor', () => {
|
||||
let instance;
|
||||
|
||||
beforeEach(() => {
|
||||
instance = new event("test");
|
||||
});
|
||||
|
||||
test('Event run is set correctly', () => {
|
||||
expect(instance.run).toBe("test");
|
||||
});
|
||||
});
|
Reference in a new issue