cogs.js
Version:
A file modulization manager, packed into a NPM package w/ a client, just for you.
162 lines (141 loc) • 4.13 kB
JavaScript
'use-strict';
const colors = require('colors');
const EventCog = require('./EventCog.js');
const Cog = require('./Cog.js');
let EventEmitter;
try {
EventEmitter = require("eventemitter3");
} catch(err) {
EventEmitter = require("events");
}
/**
* Class representing a Client.
*
* @extends EventEmitter
*/
class Client extends EventEmitter {
/**
* Create a new `Client`.
*
* @param {ClientOptions} [options] Options for the Client
*
* @arg {Object} [options]
* @arg {Boolean} [options.automaticReload] Automatic reloading of minis, cogs, and event cogs upon changes.
* @arg {String|Url} [options.cogPath] Path to load cogs.
*/
constructor(options = {}) {
super();
this.options = Object.assign({
automaticReload: false,
cogPath = null,
eventPath = null
})
/**
* All cogs loaded by the client
* @type {Array[?Cog]}
*/
this.cogs = [];
/**
* All event cogs loaded by the client
* @type {Array[?EventCog]}
*/
this.eventCogs = [];
/**
* All minis loaded by the client
* @type {Array[?Mini]}
*/
this.minis = [];
/**
* Cog directory
* @type {String|URL}
*/
this.cogPath;
this.cogPath = options.cogPath;
/**
* Event cog directory
* @type {String|URL}
*/
this.eventPath;
this.eventPath = options.eventPath;
/**
* Automatic reloading for cogs and minis. Default is `false`.
* @warn This may increase the usage of the process. Be careful.
* @type {Boolean}
*/
this.automaticReload = options.automaticReload;
/**
* If the client is ready.
*/
this.ready = false;
}
/**
* Boots up the client
* @returns {Promise} Resolves when all types of cogs are loaded.
*/
async start() {
// Setup normal cogs
if (this.cogPath) {
}
if (this.eventCogs) {
}
}
/**
* Sets the path for cogs/eventcogs
* @returns {Promise<Boolean>}
*/
async setPath(type, path) {
switch(type) {
case 0:
try {
this.cogPath = path.toString();
if (this.verbose) console.log("[Client]".blue + " Cog path is now at: " + path);
return true
} catch (err) {
throw Error("Error replacing cog path.");
return false;
}
break;
case 1:
try {
this.eventPath = path.toString();
if (this.verbose) console.log("[Client]".blue + " Event path is now at: " + path);
return true;
} catch (err) {
throw Error("Error replacing event path.");
return false;
}
break;
default:
throw Error("Incorrect value for type.");
return false;
}
}
/**
* Sets automatic reloading.
* @returns {Promise<Boolean>}
*/
async setAutomatic(bool) {
switch(bool) {
case true:
this.automatic = true;
return true;
break;
case false:
this.automatic = false;
return false;
break;
default:
return TypeError("Invalid value. Must be either true or false.");
}
}
/**
* Destroys the client.
* @returns {void}
*/
destroy() {
console.log("[Client]".red + " Destroying...");
super.destroy();
console.log("Destroyed client.".green);
}
}
module.exports = Client