configsystem
This commit is contained in:
parent
0906a4a0b4
commit
12a794f47f
|
@ -26,7 +26,7 @@ read().then(() => {
|
|||
continue;
|
||||
|
||||
let active = false;
|
||||
if (e.type === "irc" && cfg.trigger[e.network + e.channel] !== undefined) {
|
||||
if (e.type === "irc" && cfg.trigger[e.network + e.channel]) {
|
||||
if (cfg.trigger[e.network + e.channel][trigger.name])
|
||||
active = true;
|
||||
}
|
||||
|
|
|
@ -13,7 +13,8 @@ const read = () => new Promise((resolve, reject) => {
|
|||
reject({ error: "no cfg" });
|
||||
else {
|
||||
for (let row in rows) {
|
||||
cfg[rows[row].class][rows[row].key] = ((type, value) => {
|
||||
cfg[rows[row].class][rows[row].key] = {
|
||||
val: ((type, value) => {
|
||||
switch (type) {
|
||||
case "string":
|
||||
return value;
|
||||
|
@ -24,11 +25,39 @@ const read = () => new Promise((resolve, reject) => {
|
|||
case "json":
|
||||
return JSON.parse(value);
|
||||
}
|
||||
})(rows[row].type, rows[row].value);
|
||||
})(rows[row].type, rows[row].value),
|
||||
hidden: rows[row].hidden === 1,
|
||||
type: rows[row].type
|
||||
}
|
||||
}
|
||||
resolve();
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
export { cfg, read };
|
||||
const write = (kat, key, value, type, cb) => {
|
||||
if (type === "json")
|
||||
value = JSON.stringify(value);
|
||||
|
||||
self.sql.query("select * from `cfg` where `class` = ? && `key` = ? limit 1", [kat, key], (err, rows) => {
|
||||
if (rows.length > 0) {
|
||||
self.sql.query("update `cfg` set `value` = ? where `class` = ? && `key` = ?", [value, kat, key], (err) => {
|
||||
self.read(cfg => {
|
||||
self.cfg = cfg
|
||||
if (cb) cb(err);
|
||||
});
|
||||
});
|
||||
}
|
||||
else {
|
||||
self.sql.query("insert into `cfg` (`class`,`key`,`value`,`type`) values (?,?,?,?)", [kat, key, value, type], (err) => {
|
||||
self.read(cfg => {
|
||||
self.cfg = cfg
|
||||
if (cb) cb(err);
|
||||
});
|
||||
});
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
|
||||
export { cfg, read, write };
|
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);
|
||||
}
|
||||
});
|
||||
};
|
|
@ -9,20 +9,20 @@ const clients = [];
|
|||
|
||||
const wrapper = function () {
|
||||
for (let srv in cfg.client) {
|
||||
if(cfg.client[srv].enabled) {
|
||||
switch (cfg.client[srv].type) {
|
||||
if(cfg.client[srv].val.enabled) {
|
||||
switch (cfg.client[srv].val.type) {
|
||||
case "irc":
|
||||
clients.push({
|
||||
name: cfg.client[srv].network,
|
||||
name: cfg.client[srv].val.network,
|
||||
type: "irc",
|
||||
client: new irclib(cfg.client[srv])
|
||||
client: new irclib(cfg.client[srv].val)
|
||||
});
|
||||
break;
|
||||
case "tg":
|
||||
clients.push({
|
||||
name: "tg",
|
||||
type: "tg",
|
||||
client: new tglib(cfg.client[srv])
|
||||
client: new tglib(cfg.client[srv].val)
|
||||
});
|
||||
break;
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue
Block a user