diff --git a/src/inc/admin.mjs b/src/inc/admin.mjs index 33b2f4a..f2170c2 100644 --- a/src/inc/admin.mjs +++ b/src/inc/admin.mjs @@ -1,25 +1,28 @@ import sql from "./sql"; export let admins = []; -export const loadAdmins = () => { + +export const loadAdmins = async () => { + const db = await sql; admins = []; - sql.exec(`select * from admins`) - .then(rows => { - rows.forEach(row => { - admins.push({ - id: row.id, - prefix: row.prefix, - account: row.account, - network: row.network, - level: row.level - }); + db.query(`select * from user`) + .then(rows => rows.forEach(row => { + admins.push({ + id: row.id, + prefix: row.prefix, + account: row.account, + network: row.network, + level: row.level }); - }) + })) .catch(err => { - console.log("keine Admins vorhanden"); + console.log("keine Admins vorhanden", err); }); }; -loadAdmins(); + +(async () => { + await loadAdmins(); +})(); export const getLevel = (network, user) => { let ret = { diff --git a/src/inc/cfg.mjs b/src/inc/cfg.mjs index f49b9e7..322342e 100644 --- a/src/inc/cfg.mjs +++ b/src/inc/cfg.mjs @@ -9,7 +9,7 @@ let cfg = { let admins = []; const read = () => new Promise((resolve, reject) => { - sql.exec("select * from cfg").then(rows => { + sql.query("select * from cfg").then(rows => { for (let row in rows) { cfg[rows[row].class][rows[row].key] = { val: ((type, value) => { @@ -37,7 +37,7 @@ const read = () => new Promise((resolve, reject) => { export const loadAdmins = () => new Promise((resolve, reject) => { admins = []; - sql.exec(`select * from admins`) + sql.query(`select * from admins`) .then(rows => { rows.forEach(row => { admins.push({ diff --git a/src/inc/events/ctcp.mjs b/src/inc/events/ctcp.mjs index e3d5c0c..f894dd5 100644 --- a/src/inc/events/ctcp.mjs +++ b/src/inc/events/ctcp.mjs @@ -1,5 +1,3 @@ -import { logger } from "../log"; - const versions = [ "AmIRC.1 (8 Bit) for Commodore Amiga 500", "HexChat 0.72 [x86] / Windows 95c [500MHz]" diff --git a/src/inc/events/message.mjs b/src/inc/events/message.mjs index 553ffdf..dd99245 100644 --- a/src/inc/events/message.mjs +++ b/src/inc/events/message.mjs @@ -1,5 +1,4 @@ -import { logger } from "../log"; -import { cfg } from "../cfg"; +import { getLevel } from "../admin"; const parseArgs = msg => { let args = msg.trim().split(" "); @@ -32,7 +31,7 @@ export default self => { if ((e.self.set !== "all" && e.self.set !== trigger.set) && trigger.set !== "all") continue; - if (trigger.level > e.user.level.level) { + if (trigger.level > getLevel(e.network, e.user)) { e.reply(`no permission, min level ${trigger.level} required`); break; } @@ -40,6 +39,6 @@ export default self => { e = Object.assign(e, parseArgs(e.message)); trigger.f(e); } - logger.info(`${e.network} -> ${e.channel} -> ${e.user.nick}: ${e.message}`); + console.info(`${e.network} -> ${e.channel} -> ${e.user.nick}: ${e.message}`); }); }; \ No newline at end of file diff --git a/src/inc/log.mjs b/src/inc/log.mjs deleted file mode 100644 index e9a0662..0000000 --- a/src/inc/log.mjs +++ /dev/null @@ -1,26 +0,0 @@ -import winston from "winston"; - -const logger = new (winston.Logger)({ - transports: [ - new (winston.transports.File)({ - name: "debug-file", - filename: `./logs/${~~(Date.now() / 1000)}_debug.log`, - level: "debug" - }), - new (winston.transports.File)({ - name: "info-file", - filename: `./logs/${~~(Date.now() / 1000)}_info.log`, - level: "info" - }), - new (winston.transports.File)({ - name: "error-file", - filename: `./logs/${~~(Date.now() / 1000)}_error.log`, - level: "error" - }), - new (winston.transports.Console)({ - level: "info" - }) - ] -}); - -export { logger }; diff --git a/src/inc/sql.mjs b/src/inc/sql.mjs index 90a1d17..6ed19fd 100644 --- a/src/inc/sql.mjs +++ b/src/inc/sql.mjs @@ -1,5 +1,4 @@ -import mysql from "nodejs-mysql2"; -import { default as cfg } from "../../cfg/sql.json"; +import mariadb from "mariadb"; +import cfg from "../../config.json"; -const sql = mysql.default.getInstance(cfg); -export default sql; +export default mariadb.createPool(cfg.sql).getConnection(); diff --git a/src/inc/trigger/inc/parser.mjs b/src/inc/trigger/inc/parser.mjs deleted file mode 100644 index 76c3f80..0000000 --- a/src/inc/trigger/inc/parser.mjs +++ /dev/null @@ -1,8 +0,0 @@ -const formatSize = (size, i = ~~(Math.log(size) / Math.log(1024))) => (size / Math.pow(1024, i)).toFixed(2) * 1 + ' ' + ['B', 'kB', 'MB', 'GB', 'TB'][i]; - -const getUUID = () => Math.random().toString(36).slice(-10) + (new Date()).getTime().toString(36).slice(-3); - -const calcSpeed = (bytes, seconds) => `${Math.round((bytes * 8 / seconds / 1000000) * 10000) / 10000} Mbit/s`; - - -export { calcSpeed, formatSize, getUUID }; \ No newline at end of file diff --git a/src/index.mjs b/src/index.mjs index 31d052a..0871b03 100644 --- a/src/index.mjs +++ b/src/index.mjs @@ -1,11 +1,12 @@ -import { logger } from "./inc/log"; -import { read, cfg, admins } from "./inc/cfg"; +import cfg from "../config.json"; +import sql from "./inc/sql"; import { cuffeo } from "cuffeo"; import triggers from "./inc/trigger"; import events from "./inc/events"; -read().then(() => { +(async () => { + // Chatbots const self = { _trigger: new Map(), trigger: function trigger(args) { @@ -14,11 +15,13 @@ read().then(() => { this.level = args.level || 0; this.active = args.hasOwnProperty("active") ? args.active : true; this.set = args.set || "all"; // uwe, nxy, f0ck, all - this.clients = args.clients || ["irc", "tg", "discord"]; + this.clients = args.clients || ["irc", "tg"]; this.f = args.f; }, - bot: new cuffeo(cfg.client, admins) + bot: new cuffeo(cfg.clients) }; triggers.forEach(mod => mod(self)); events.forEach(event => event(self)); -}).catch(err => logger.error(`(main) ${err.message}`)); + + // +})();