Initial work

This commit is contained in:
Vylpes 2020-10-20 10:45:24 +01:00
parent 6e298c9552
commit 19c4e1df3f
11 changed files with 443 additions and 0 deletions

46
src/client/client.js Normal file
View file

@ -0,0 +1,46 @@
const { Client } = require('discord.js');
const { existsSync } = require('fs');
const events = require('./events');
const util = require('./util');
class client extends Client {
constructor(config) {
super();
this.config = config;
this.events = new events();
this.util = new util(this);
}
start() {
super.on("message", this.events.message);
super.on("ready", this.events.ready);
super.login(this._config.token);
this.util.loadEvents();
}
// Config
get config() {
return this._config;
}
set config(config) {
// Validate the config
if (this._config) throw "Config has already been set";
if (typeof config != "object") throw "Config is not a JSON object";
if (typeof config.token != "string") throw "Token is not a string";
if (typeof config.prefix != "string") throw "Prefix is not a string";
if (typeof config.commands != "string") throw "Commands is not a string";
if (!existsSync(`${process.cwd()}/${config.commands}`)) throw "Commands folder doesn't exist";
if (typeof config.events != "string") throw "Events is not a string";
if (!existsSync(`${process.cwd()}/${config.events}`)) throw "Events folder doesn't exist";
this._config = config;
}
}
module.exports = client;

21
src/client/events.js Normal file
View file

@ -0,0 +1,21 @@
class event {
message(message) {
if (!message.guild) return;
if (message.author.bot) return;
let prefix = this.config.prefix;
if (message.content.substring(0, prefix.length).toLowerCase() == prefix.toLowerCase()) {
let args = message.content.substring(prefix.length).split(" ");
let name = args.shift();
this.util.loadCommand(name, args, message);
}
}
ready() {
console.log("Ready");
}
}
module.exports = event;

58
src/client/util.js Normal file
View file

@ -0,0 +1,58 @@
const { stat, readdirSync } = require('fs');
const { config } = require('process');
class util {
constructor(client) {
this._client = client;
}
loadCommand(name, args, message) {
stat(`${process.cwd()}/${this._client.config.commands}/${name}.js`, (err, stat) => {
if (err == null) {
let commandFile = require(`${process.cwd()}/${this._client.config.commands}/${name}.js`);
let command = new commandFile();
let requiredConfigs = command.requiredConfigs;
for (let i = 0; i < requiredConfigs.length; i++) {
if (!this._client.config[name]) throw `${commandFile.name} requires ${requiredConfigs[i]} in it's configuration`;
if (!this._client.config[name][requiredConfigs[i]]) throw `${commandFile.name} requires ${requiredConfigs[i]} in it's configuration`;
}
let requiredRoles = command.roles;
for (let i = 0; i < requiredRoles.length; i++) {
if (!message.member.roles.cache.find(role => role.name == requiredRoles[i])) {
message.reply(`You require the \`${requiredRoles[i]}\` role to run this command`);
return;
}
}
command[command.run]({
"command": name,
"arguments": args,
"client": this._client,
"message": message,
"config": config
});
} else if (err.code === 'ENOENT') {
// FILE DOESN'T EXIST
}
});
}
loadEvents() {
let eventFiles = readdirSync(`${process.cwd()}/${this._client.config.events}/`);
for (let i = 0; i < eventFiles.length; i++) {
let eventName = eventFiles[i].split('.')[0];
let file = require(`${process.cwd()}/${this._client.config.events}/${eventName}.js`);
let event = new file;
this._client.on(eventName, event[event.run]);
}
}
}
module.exports = util;