From 232ca4dd16f0269614ccd9af98f19c3ec974854f Mon Sep 17 00:00:00 2001 From: Faith Date: Tue, 5 Sep 2017 09:26:14 +0200 Subject: [PATCH] MergeOptions/Config (#348) * 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: ce01b45a3de396f60299115dfd7f5bf7a5221890 * Docs build: f76f969ae20a80fdd09f093ee903fa245ecb6dfe * 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: 8d48b572e4a6f24940211b1c7060d4e8699ed3d1 * Fix the help command once more * Auto stash before rebase of "dirigeants/master" --- CHANGELOG.md | 1 + package.json | 2 +- src/classes/client.js | 20 ++------- src/functions/mergeConfig.js | 82 ++++++++++++++++++++++++++++++++++++ 4 files changed, 88 insertions(+), 17 deletions(-) create mode 100644 src/functions/mergeConfig.js diff --git a/CHANGELOG.md b/CHANGELOG.md index 29c88602..3304b260 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/package.json b/package.json index d7c674df..644c05b1 100644 --- a/package.json +++ b/package.json @@ -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": { diff --git a/src/classes/client.js b/src/classes/client.js index 1e5866d7..a2eb37d4 100644 --- a/src/classes/client.js +++ b/src/classes/client.js @@ -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) @@ -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 @@ -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)); } /** diff --git a/src/functions/mergeConfig.js b/src/functions/mergeConfig.js new file mode 100644 index 00000000..540283b4 --- /dev/null +++ b/src/functions/mergeConfig.js @@ -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."); + } +};