moved cfg from db to json

This commit is contained in:
Flummi 2019-08-19 19:17:51 +00:00
parent cd606aa6eb
commit 4c55065168
7 changed files with 12 additions and 180 deletions

View File

@ -1,15 +1,12 @@
import logger from "./inc/log";
import _fs from "fs"; import _fs from "fs";
import { read, cfg } from "./inc/cfg";
import { default as config } from "../cfg/config.json";
import cuffeo from "cuffeo"; import cuffeo from "cuffeo";
import config from "../cfg/config.json";
import logger from "./inc/log";
const fs = _fs.promises; const fs = _fs.promises;
const timeout = 1000; const timeout = 1000;
(async () => { (async () => {
await read(); // read and parse config from database
const self = { const self = {
_trigger: new Map(), _trigger: new Map(),
trigger: function trigger(args) { trigger: function trigger(args) {

View File

@ -1,68 +0,0 @@
import sql from "./sql";
let cfg = {
client: {},
main: {},
websrv: {},
trigger: {}
};
const read = () => new Promise((resolve, reject) => {
sql.any("select * from cfg").then(rows => {
for (let row in rows) {
cfg[rows[row].class][rows[row].key] = {
val: ((type, value) => {
switch (type) {
case "string":
return value;
case "int":
return parseInt(value);
case "bool":
return value === "true";
case "json":
return JSON.parse(value);
}
})(rows[row].type, rows[row].value),
hidden: rows[row].hidden === 1,
type: rows[row].type
}
}
resolve();
})
.catch(err => {
reject("no cfg");
})
});
const write = (kat, key, value, type, cb) => {
if (type === "json")
value = JSON.stringify(value);
sql.any("select * from cfg where class = $1 && key = $2 limit 1", [kat, key]).then(rows => {
if (rows.length > 0) {
sql.any("update cfg set value = $1 where class = $2 && key = $3", [value, kat, key]).then(rows => {
read().then(() => {
if(cb) cb();
});
})
.catch(err => {
console.log("on update", err);
});
}
else {
sql.any("insert into cfg (class, key, value, type) values ($1, $2, $3, $4)", [kat, key, value, type]).then(rows => {
read().then(() => {
if (cb) cb();
});
})
.catch(err => {
console.log("on insert", err);
});
}
})
.then(err => {
console.log("on select", err);
});
};
export { cfg, read, write };

View File

@ -1,97 +0,0 @@
import { read, write, cfg } from "../cfg";
const _modes = ["get", "set", "rm", "add", "opts"];
const _opts = {
showTypes: { type: "boolean", stdval: false },
showHidden: { type: "boolean", stdval: false }
};
const _debug = false;
export default async bot => {
return [{
name: "cfg",
call: /^\!cfg/i,
active: false,
level: 100,
clients: ["irc"],
f: e => {
let args = e.message.substring(5);
let opts = {};
let value = null;
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 === "set" && e.message.includes("=")) { // get value if mode = set
args = args.join(" "); // omg
value = args.replace(/.*=(.*?)/, "$1").trim();
args = args.replace(/(.*?)=.*/, "$1").trim().split(" ");
e.reply(`value: ${value}, rest: ${args}`);
}
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(mode === "get") {
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`);
}
switch (mode) {
case "get":
e.reply(cfg[cfgstr[0]][cfgstr[1]].val);
break;
case "set":
if (cfg[cfgstr[0]][cfgstr[1]].type === "json")
return e.reply("JSON isn't supported yet");
if(value === null)
return e.reply("Error");
write(cfgstr[0], cfgstr[1], value, cfg[cfgstr[0]][cfgstr[1]].type, () => {
e.reply(cfg[cfgstr[0]][cfgstr[1]].val);
})
break;
}
}
}];
};

View File

@ -1,7 +1,7 @@
import { cfg } from "../../inc/cfg";
import fetch from "flumm-fetch-cookies"; import fetch from "flumm-fetch-cookies";
import config from "../../../cfg/config.json";
const api = `http://ws.audioscrobbler.com/2.0/?method=user.getRecentTracks&limit=1&api_key=${cfg.main.lastfm.val.key}&format=json&user=`; const api = `http://ws.audioscrobbler.com/2.0/?method=user.getRecentTracks&limit=1&api_key=${config.apis.lastfm.key}&format=json&user=`;
export default async bot => { export default async bot => {

View File

@ -1,5 +1,5 @@
import fetch from "flumm-fetch-cookies"; import fetch from "flumm-fetch-cookies";
import { cfg } from "../../../inc/cfg"; import config from "../../../../cfg/config.json";
export default new class cleverbot { export default new class cleverbot {
constructor() { constructor() {
@ -12,7 +12,7 @@ export default new class cleverbot {
const options = { const options = {
method: "POST", method: "POST",
body: { body: {
...cfg.main.chatbot.val, ...config.apis.cleverbot,
...{ nick: "uwibot" } ...{ nick: "uwibot" }
} }
}; };
@ -27,7 +27,7 @@ export default new class cleverbot {
const options = { const options = {
method: "POST", method: "POST",
body: { body: {
...cfg.main.chatbot.val, ...config.apis.cleverbot,
...{ ...{
nick: this.nick, nick: this.nick,
text: msg text: msg

View File

@ -1,5 +1,5 @@
import fetch from "flumm-fetch-cookies"; import fetch from "flumm-fetch-cookies";
import { cfg } from "../../inc/cfg"; import config from "../../../cfg/config.json";
export default async bot => { export default async bot => {
@ -12,7 +12,7 @@ export default async bot => {
usage: "[b].scrnd[/b]" usage: "[b].scrnd[/b]"
}, },
f: async e => { f: async e => {
const res = await (await fetch(`http://api.soundcloud.com/users/${cfg.main.soundcloud.val.user}/favorites?client_id=${cfg.main.soundcloud.val.clientid}`)).json(); const res = await (await fetch(`http://api.soundcloud.com/users/${config.apis.soundcloud.user}/favorites?client_id=${config.apis.soundcloud.clientid}`)).json();
const track = res[~~((Math.random() * res.length) + 1)]; const track = res[~~((Math.random() * res.length) + 1)];
e.reply(`${track.permalink_url}\n[b]${track.title}[/b] - length [b]${track.duration}[/b] - [b]${track.user.username}[/b] on [b]${track.created_at}[/b]`); e.reply(`${track.permalink_url}\n[b]${track.title}[/b] - length [b]${track.duration}[/b] - [b]${track.user.username}[/b] on [b]${track.created_at}[/b]`);
} }

View File

@ -1,5 +1,5 @@
import fetch from "flumm-fetch-cookies"; import fetch from "flumm-fetch-cookies";
import { cfg } from "../../inc/cfg"; import config from "../../../cfg/config.json";
import { conds } from "./lib/wttr"; import { conds } from "./lib/wttr";
export default async bot => { export default async bot => {
@ -74,7 +74,7 @@ export default async bot => {
}, },
f: e => { f: e => {
const loc = encodeURIComponent(e.message.trim().substring(3)); const loc = encodeURIComponent(e.message.trim().substring(3));
const key = cfg.main.owm.val.key; const key = config.apis.owm.key;
const api = `http://api.openweathermap.org/data/2.5/weather?q=${loc}&appid=${key}&units=metric&lang=de`; const api = `http://api.openweathermap.org/data/2.5/weather?q=${loc}&appid=${key}&units=metric&lang=de`;
fetch(api) fetch(api)
@ -121,7 +121,7 @@ export default async bot => {
}, },
f: e => { f: e => {
const loc = encodeURIComponent(e.message.trim().substring(9)); const loc = encodeURIComponent(e.message.trim().substring(9));
const key = cfg.main.owm.val.key; const key = config.apis.owm.key;
const api = `http://api.openweathermap.org/data/2.5/weather?q=${loc}&appid=${key}&units=metric&lang=de`; const api = `http://api.openweathermap.org/data/2.5/weather?q=${loc}&appid=${key}&units=metric&lang=de`;
fetch(api) fetch(api)