Tests
This commit is contained in:
parent
7840bc9be8
commit
80e9daa8dc
24 changed files with 5435 additions and 111 deletions
|
@ -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"
|
||||
}
|
||||
}
|
Reference in a new issue