configsystem
This commit is contained in:
68
src/inc/trigger/cfg.js
Normal file
68
src/inc/trigger/cfg.js
Normal file
@ -0,0 +1,68 @@
|
||||
import { read, write, cfg } from "../cfg.js";
|
||||
|
||||
const _modes = ["get", "set", "rm", "add", "opts"];
|
||||
const _opts = {
|
||||
showTypes: { type: "boolean", stdval: false },
|
||||
showHidden: { type: "boolean", stdval: false }
|
||||
};
|
||||
const _debug = true;
|
||||
|
||||
module.exports = bot => {
|
||||
bot._trigger.set("cfg", {
|
||||
call: /^\!cfg/i,
|
||||
level: 100,
|
||||
active: true,
|
||||
clients: ["irc"],
|
||||
f: e => {
|
||||
let args = e.message.substring(5);
|
||||
let opts = {}; // clone
|
||||
|
||||
if (args.includes("{") && args.includes(":") && args.charAt(args.length - 1) === "}") { // check if options available
|
||||
try {
|
||||
opts = JSON.parse(args.replace(/.*\{(.*?)\}.*/, "{$1}")); // parse options
|
||||
}
|
||||
catch(err) {
|
||||
return e.reply(err.message); // throw errormessage
|
||||
}
|
||||
args = args.replace(/(.*?)\{.*\}(.*?)/, "$1$2"); // remove options from arguments
|
||||
}
|
||||
for (let _opt in _opts) // check options
|
||||
if (!opts.hasOwnProperty(_opt))
|
||||
opts[_opt] = _opts[_opt].stdval;
|
||||
else
|
||||
if (typeof opts[_opt] !== _opts[_opt].type)
|
||||
opts[_opt] = _opts[_opt].stdval;
|
||||
|
||||
if(_debug) e.reply(`opts: ${JSON.stringify(opts)}`);
|
||||
|
||||
args = args.trim().split(" ");
|
||||
let mode = args.shift();
|
||||
|
||||
if (mode === "opts") // maybe replaced by switch in the future
|
||||
return e.reply(`options: ${Object.keys(_opts).map(el => `[b]${el}[/b]: [i]${_opts[el].stdval} (${_opts[el].type})[/i]`).join(", ")}`)
|
||||
|
||||
if (!_modes.includes(mode)) // if unknown mode
|
||||
return e.reply(`wrong mode! (${_modes.join(", ")})`);
|
||||
if (args.length === 0) // no mode given, get classes
|
||||
return e.reply(`classes: ${Object.keys(cfg).join(", ")}`);
|
||||
|
||||
let cfgstr = args[0].split(".");
|
||||
|
||||
if (cfgstr.length === 1) { // get keys in class
|
||||
if (!Object.keys(cfg).includes(cfgstr[0]))
|
||||
return e.reply(`class [b]${cfgstr[0]}[/b] not found`);
|
||||
let keys = { // split between hidden and public keys
|
||||
hidden: Object.keys(cfg[cfgstr[0]]).filter(el => cfg[cfgstr[0]][el].hidden),
|
||||
public: Object.keys(cfg[cfgstr[0]]).filter(el => !cfg[cfgstr[0]][el].hidden)
|
||||
};
|
||||
return e.reply(`keys in class [b]${cfgstr[0]}[/b]: ${keys.public.length > 0 ? keys.public.map(el => `${el}${opts.showTypes ? ` [i](${cfg[cfgstr[0]][el].type})[/i]` : ""}`).join(", ") : "none"} (${keys.hidden.length} hidden)`);
|
||||
}
|
||||
|
||||
if (!opts.showHidden)
|
||||
if (cfg[cfgstr[0]][cfgstr[1]].hidden || opts.showHidden) // catch hidden key
|
||||
return e.reply(`key [b]${cfgstr[1]}[/b] in class [b]${cfgstr[0]}[/b] is hidden, kek`);
|
||||
|
||||
e.reply(cfg[cfgstr[0]][cfgstr[1]].val);
|
||||
}
|
||||
});
|
||||
};
|
Reference in New Issue
Block a user