Skip to content
This repository has been archived by the owner on Aug 27, 2018. It is now read-only.

Commit

Permalink
MergeOptions/Config (#348)
Browse files Browse the repository at this point in the history
* create initial mergeConfig

* more changes

* remove eslint

* remove more

* fix error

* k

* final fixes

* Fix all instances of pieces failing to load and not updating (#347)

* Fix all instances of pieces failing to load and not updating

* Final commit

* Docs build: ce01b45

* Docs build: f76f969

* chore(package): update eslint-config-airbnb-base to version 12.0.0 (#344)

* Eslint Update, D.JS Latest, Chalk 2.1.0. Use FORCE_COLOR=1 envvar if colour does not show up

* Docs build: 8d48b57

* Fix the help command once more

* Auto stash before rebase of "dirigeants/master"
  • Loading branch information
UnseenFaith authored and CyberiumShadow committed Sep 5, 2017
1 parent db1403b commit 232ca4d
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 17 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).

## [Unreleased] - Classbased | Staged for 0.20.8
### Added
- [[#347](https://github.com/dirigeants/komada/pull/347)] Merge Function for options.
- [[#343](https://github.com/dirigeants/komada/pull/343)] Komada extendables are now a thing.
- [[#323](https://github.com/dirigeants/komada/pull/323)] Documentation to all pieces.
- [[#323](https://github.com/dirigeants/komada/pull/323)] Added the types `TextChannel` and `VoiceChannel` to the
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "komada",
"version": "0.20.9",
"version": "0.20.10",
"description": "Komada: Croatian for 'pieces', is a modular bot system including reloading modules and easy to use custom commands.",
"homepage": "https://github.com/dirigeants/komada#readme",
"bugs": {
Expand Down
20 changes: 4 additions & 16 deletions src/classes/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ const Loader = require("./loader");
const ArgResolver = require("./argResolver");
const PermLevels = require("./permLevels");
const Settings = require("./settingsCache");
const merge = require("../functions/mergeConfig");

const defaultPermStructure = new PermLevels()
.addLevel(0, false, () => true)
Expand Down Expand Up @@ -84,21 +85,8 @@ class Komada extends Discord.Client {
* The configuration used to create Komada
* @type {Komada.Options}
*/
/* Remove all of this and replace with a mergeConfig method instead before v1 release */
this.config = config;
if (!("prefix" in config)) this.config.prefix = "?";
this.config.provider = config.provider || {};
if (!config.disabled) config.disabled = {};
this.config.disabled = {
commands: config.disabled.commands || [],
events: config.disabled.events || [],
functions: config.disabled.functions || [],
inhibitors: config.disabled.inhibitors || [],
finalizers: config.disabled.finalizers || [],
monitors: config.disabled.monitors || [],
providers: config.disabled.providers || [],
extendables: config.disabled.extendables || [],
};

this.config = merge(config);

/**
* The location of where the core files of Komada rely in, typically inside node_modules
Expand Down Expand Up @@ -322,7 +310,7 @@ class Komada extends Discord.Client {
}));
this.setInterval(this.sweepCommandMessages.bind(this), this.commandMessageLifetime);
this.ready = true;
this.emit("log", this.config.readyMessage || `Successfully initialized. Ready to serve ${this.guilds.size} guilds.`);
this.emit("log", this.config.readyMessage(this));
}

/**
Expand Down
82 changes: 82 additions & 0 deletions src/functions/mergeConfig.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
const PermLevels = require("../classes/permLevels");

const defaultPermStructure = new PermLevels()
.addLevel(0, false, () => true)
.addLevel(2, false, (client, msg) => {
if (!msg.guild || !msg.guild.settings.modRole) return false;
const modRole = msg.guild.roles.get(msg.guild.settings.modRole);
return modRole && msg.member.roles.has(modRole.id);
})
.addLevel(3, false, (client, msg) => {
if (!msg.guild || !msg.guild.settings.adminRole) return false;
const adminRole = msg.guild.roles.get(msg.guild.settings.adminRole);
return adminRole && msg.member.roles.has(adminRole.id);
})
.addLevel(4, false, (client, msg) => msg.guild && msg.author.id === msg.guild.owner.id)
.addLevel(9, true, (client, msg) => msg.author.id === client.config.ownerID)
.addLevel(10, false, (client, msg) => msg.author.id === client.config.ownerID);


module.exports = (options) => {
for (const key in this.DEFAULT_OPTIONS) { // eslint-disable-line
if (!(key in options)) options[key] = this.DEFAULT_OPTIONS[key];
}
this.validate(options);
return options;
};

exports.DEFAULT_OPTIONS = {
prefix: "?",
ownerID: null,
disabled: {
commands: [],
events: [],
functions: [],
inhibitors: [],
finalizers: [],
monitors: [],
providers: [],
extendables: [],
},
permStructure: defaultPermStructure,
selfbot: false,
readyMessage: client => `Successfully initialized. Ready to serve ${client.guilds.size} guilds.`,
commandMessageLifetime: 1800,
commandMessageSweep: 900,
disableLogTimestamps: false,
disableLogColor: false,
cmdEditing: false,
cmdPrompt: false,
provider: {
engine: "json",
cache: "js",
},
};

exports.validate = (options) => {
const pieces = Object.keys(this.DEFAULT_OPTIONS.disabled);
if ("prefix" in options && typeof options.prefix !== "string") throw new TypeError("Prefix must be a string value.");
if ("ownerID" in options && typeof options.ownerID !== "string" && options.ownerID !== null) throw new TypeError("OwnerID must be a string (user id) if provided.");
if ("disabled" in options) {
if (typeof options.disabled !== "object" || Array.isArray(options.disabled)) throw new TypeError("Disabled must be a valid object");
for (const key of Object.keys(options.disabled)) { // eslint-disable-line
if (!pieces.includes(key)) throw new Error("Invalid piece name in the disabled array");
if (!Array.isArray(options.disabled[key])) throw new TypeError(`${key} must be an array.`);
}
}
if ("permStructure" in options) {
if (options.permStructure.constructor.name !== "PermissionLevels" && !Array.isArray(options.permStructure)) throw new TypeError("PermStructure must be a valid array with 11 entries, or a instance of Komada.PermLevels");
}
if ("selfbot" in options && typeof options.selfbot !== "boolean") throw new TypeError("Selfbot must be true or false.");
if ("readyMessage" in options && typeof options.readyMessage !== "function") throw new TypeError("ReadyMessage must be a function.");
if ("commandMessageLifetime" in options && typeof options.commandMessageLifetime !== "number") throw new TypeError("CommandMessageLifetime must be a number.");
if ("commandMessageSweep" in options && typeof options.commandMessageSweep !== "number") throw new TypeError("CommandMessageSweep must be a number.");
if ("disableLogTimestamps" in options && typeof options.disableLogTimestamps !== "boolean") throw new TypeError("DisableLogTimestamps must be true or false.");
if ("disableLogColor" in options && typeof options.disableLogColor !== "boolean") throw new TypeError("DisableLogColor must be true or false.");
if ("cmdEditing" in options && typeof options.cmdEditing !== "boolean") throw new TypeError("CmdEditing must be true or false.");
if ("cmdPrompt" in options && typeof options.cmdPrompt !== "boolean") throw new TypeError("CmdPrompt must be true or false.");
if ("provider" in options) {
if ("engine" in options.provider && typeof options.provider.engine !== "string") throw new TypeError("Engine must be a string.");
if ("cache" in options.provider && typeof options.provider.cache !== "string") throw new TypeError("Cache must be a string.");
}
};

0 comments on commit 232ca4d

Please sign in to comment.