psql.. REEEEE

This commit is contained in:
Flummi 2017-11-28 02:27:25 +01:00
parent a33b4ad92b
commit d91d886fc6
13 changed files with 59 additions and 61 deletions

1
.gitignore vendored
View File

@ -3,3 +3,4 @@ node_modules/
dist/ dist/
.vscode/ .vscode/
logs/ logs/
package-lock.json

View File

@ -14,7 +14,7 @@
"dependencies": { "dependencies": {
"jsdom": "^11.4.0", "jsdom": "^11.4.0",
"node-telegram-bot-api": "^0.29.0", "node-telegram-bot-api": "^0.29.0",
"nodejs-mysql": "github:flummi/nodejs-mysql", "pg-promise": "^7.3.2",
"request": "^2.83.0", "request": "^2.83.0",
"request-promise": "^4.2.2", "request-promise": "^4.2.2",
"stringify-object": "^3.2.1", "stringify-object": "^3.2.1",

View File

@ -49,6 +49,9 @@ read().then(() => {
bot.on("ctcp:version", e => { bot.on("ctcp:version", e => {
e.write(`notice ${e.user.nick} :\u0001VERSION Pimmel 2.1\u0001`); e.write(`notice ${e.user.nick} :\u0001VERSION Pimmel 2.1\u0001`);
}); });
})
.catch(err => {
console.log(err);
}); });
function trigger(args) { function trigger(args) {

View File

@ -3,7 +3,7 @@ import sql from "./sql.js";
export let admins = []; export let admins = [];
export const loadAdmins = () => { export const loadAdmins = () => {
admins = []; admins = [];
sql.exec("select * from `admins`") sql.any(`select * from admins`)
.then(rows => { .then(rows => {
rows.forEach(row => { rows.forEach(row => {
admins.push({ admins.push({

View File

@ -8,31 +8,30 @@ let cfg = {
}; };
const read = () => new Promise((resolve, reject) => { const read = () => new Promise((resolve, reject) => {
sql.exec("select * from `cfg`", (err, rows) => { sql.any("select * from cfg").then(rows => {
if(err || rows.length < 1) for (let row in rows) {
reject({ error: "no cfg" }); cfg[rows[row].class][rows[row].key] = {
else { val: ((type, value) => {
for (let row in rows) { switch (type) {
cfg[rows[row].class][rows[row].key] = { case "string":
val: ((type, value) => { return value;
switch (type) { case "int":
case "string": return parseInt(value);
return value; case "bool":
case "int": return value === "true";
return parseInt(value); case "json":
case "bool": return JSON.parse(value);
return value === "true"; }
case "json": })(rows[row].type, rows[row].value),
return JSON.parse(value); hidden: rows[row].hidden === 1,
} type: rows[row].type
})(rows[row].type, rows[row].value),
hidden: rows[row].hidden === 1,
type: rows[row].type
}
} }
resolve();
} }
}); resolve();
})
.catch(err => {
reject("no cfg");
})
}); });
const write = (kat, key, value, type, cb) => { const write = (kat, key, value, type, cb) => {
@ -40,10 +39,10 @@ const write = (kat, key, value, type, cb) => {
value = JSON.stringify(value); value = JSON.stringify(value);
console.log(kat, key, value, type); console.log(kat, key, value, type);
sql.exec("select * from `cfg` where `class` = ? && `key` = ? limit 1", [kat, key], (err, rows) => { sql.any("select * from cfg where class = ? && key = ? limit 1", [kat, key], (err, rows) => {
console.log(rows); console.log(rows);
if (rows.length > 0) { if (rows.length > 0) {
sql.exec("update `cfg` set `value` = ? where `class` = ? && `key` = ?", [value, kat, key], (err) => { sql.any("update cfg set value = ? where class = ? && key = ?", [value, kat, key], (err) => {
console.log("updated", err); console.log("updated", err);
read().then(() => { read().then(() => {
if(cb) cb(); if(cb) cb();
@ -51,7 +50,7 @@ const write = (kat, key, value, type, cb) => {
}); });
} }
else { else {
sql.exec("insert into `cfg` (`class`,`key`,`value`,`type`) values (?,?,?,?)", [kat, key, value, type], (err) => { sql.any("insert into cfg (class, key, value, type) values (?, ?, ?, ?)", [kat, key, value, type], (err) => {
console.log("inserted", err); console.log("inserted", err);
read().then(() => { read().then(() => {
if (cb) cb(); if (cb) cb();

View File

@ -1,4 +1,4 @@
import mysql from "nodejs-mysql"; const pgp = require("pg-promise")();
const sql = pgp(require(`${__dirname}/../../cfg/sql.json`));
const sql = mysql.getInstance( require(`${__dirname}/../../cfg/mysql.json`) ); //const sql = mysql.getInstance( require(`${__dirname}/../../cfg/mysql.json`) );
export default sql; export default sql;

View File

@ -54,7 +54,7 @@ module.exports = bot => {
const userlevel = getLevel(e.network, user).level; const userlevel = getLevel(e.network, user).level;
if(userlevel > 0) { // check if user is admin if(userlevel > 0) { // check if user is admin
if(level <= 0) { if(level <= 0) {
sql.exec("delete from `admins` where `account` = lower(?) and `network` = lower(?)", [user.account, e.network]) sql.any("delete from admins where account = lower(?) and network = lower(?)", [user.account, e.network])
.then(rows => { .then(rows => {
loadAdmins(); loadAdmins();
return e.reply(`deleted user ${user.nickname}`); return e.reply(`deleted user ${user.nickname}`);
@ -64,7 +64,7 @@ module.exports = bot => {
}) })
} }
else if(level !== userlevel) { else if(level !== userlevel) {
sql.exec("update `admins` set `level` = ? where `account` = ? and `network` = ?", [level, user.account, e.network]) sql.any("update admins set level = ? where account = ? and network = ?", [level, user.account, e.network])
.then(rows => { .then(rows => {
loadAdmins(); loadAdmins();
return e.reply(`changed user [b]${user.nickname}[/b] (${userlevel} -> ${level})`); return e.reply(`changed user [b]${user.nickname}[/b] (${userlevel} -> ${level})`);
@ -78,7 +78,7 @@ module.exports = bot => {
} }
} }
else { else {
sql.exec("insert into `admins` (`prefix`, `account`, `level`, `network`) values (lower(?), lower(?), ?, lower(?))", [user.prefix, user.account, level, e.network]) sql.any("insert into admins (prefix, account, level, network) values (lower(?), lower(?), ?, lower(?))", [user.prefix, user.account, level, e.network])
.then(rows => { .then(rows => {
loadAdmins(); loadAdmins();
return e.reply(`added new user [b]${user.nickname}[/b] with level ${level}`); return e.reply(`added new user [b]${user.nickname}[/b] with level ${level}`);

View File

@ -6,39 +6,30 @@ const data = {
}; };
Object.keys(data).forEach(cur => { Object.keys(data).forEach(cur => {
sql.exec(`select \`data\` from \`useless\` where \`trigger\` = '${cur}' limit 1`, (err, row) => { sql.any("select data from useless where trigger = $1 limit 1", [cur])
data[cur] = JSON.parse(row[0].data); .then(rows => {
}); data[cur] = JSON.parse(rows[0].data);
});
}); });
module.exports = bot => { module.exports = bot => {
bot._trigger.set("dope", new bot.trigger({ bot._trigger.set("dope", new bot.trigger({
call: /^(\.|\/)dope/i, call: /^(\.|\/)dope/i,
f: e => { f: e => {
let args = e.message.trim().split(" ");
args.shift();
args[0] = (args[0] == String.empty || typeof args[0] === "undefined" || args[0] == "") ? e.user.nick : args[0];
const strain_types = Object.keys(data.dope_strains); const strain_types = Object.keys(data.dope_strains);
const action_types = Object.keys(data.dope_actions); const action_types = Object.keys(data.dope_actions);
const strain_type = strain_types[~~(Math.random() * strain_types.length)] const strain_type = strain_types[~~(Math.random() * strain_types.length)]
, consume_type = action_types[~~(Math.random() * action_types.length)] , consume_type = action_types[~~(Math.random() * action_types.length)]
, action = data.dope_actions[consume_type][~~(Math.random() * data.dope_actions[consume_type].length)] , action = data.dope_actions[consume_type][~~(Math.random() * data.dope_actions[consume_type].length)]
, strain = data.dope_strains[strain_type][~~(Math.random() * data.dope_strains[strain_type].length)]; , strain = data.dope_strains[strain_type][~~(Math.random() * data.dope_strains[strain_type].length)];
e.replyAction(`${action[0]} a ${consume_type} of the finest ${strain_type} "${strain}" ${action[1]} [b]${e.args[0] || e.user.nick}[/b]`);
e.replyAction(`${action[0]} a ${consume_type} of the finest ${strain_type} "${strain}" ${action[1]} [b]${args[0]}[/b]`);
} }
})); }));
bot._trigger.set("meth", new bot.trigger({ bot._trigger.set("meth", new bot.trigger({
call: /^(\.|\/)meth/i, call: /^(\.|\/)meth/i,
f: e => { f: e => {
let args = e.message.trim().split(" "); e.replyAction(`legt [b]${e.args[0] || e.user.nick}[/b] eine dicke Line Meth \\________`);
args.shift();
args[0] = (args[0] == String.empty || typeof args[0] === "undefined" || args[0] == "") ? e.user.nick : args[0];
e.replyAction(`legt [b]${args[0]}[/b] eine dicke Line Meth \\________`);
} }
})); }));
}; };

View File

@ -8,7 +8,7 @@ module.exports = bot => {
f: e => { f: e => {
const match = e.message.match(/.*(Mc\S+iaC?).*/)[0]; const match = e.message.match(/.*(Mc\S+iaC?).*/)[0];
console.log(match); console.log(match);
sql.exec("insert into `mcmaniacs` (`item`) values (?)", [match]) sql.any("insert into `mcmaniacs` (`item`) values (?)", [match])
.then() .then()
.catch(err => {}); .catch(err => {});
} }
@ -42,7 +42,7 @@ module.exports = bot => {
query = query query = query
.split("{order}").join(order) .split("{order}").join(order)
.split("{offset}").join(offset); .split("{offset}").join(offset);
sql.exec(query) sql.any(query)
.then(rows => { .then(rows => {
e.reply(`[${rows[0].id}/${rows[0].sum}] ${rows[0].item}`); e.reply(`[${rows[0].id}/${rows[0].sum}] ${rows[0].item}`);
}) })

View File

@ -1,7 +1,7 @@
import sql from "../sql.js"; import sql from "../sql.js";
let _query_get = ` let _query_get = `
set @s := (select count(id) as sum from nxy_quotes where nick like ?); set @s := (select count(id) as sum from nxy_quotes where nick like $1);
select id, nick, item, rank, @s as sum select id, nick, item, rank, @s as sum
from ( from (
select @r:= if(@u = nick, @r {rankorder}) as rank, select @r:= if(@u = nick, @r {rankorder}) as rank,
@ -11,7 +11,7 @@ from (
(select @u:= '') as u (select @u:= '') as u
order by nick, id {order} order by nick, id {order}
) as blah ) as blah
where nick like ? where nick like $1
{rand} {rand}
limit 1 limit 1
offset {offset}; offset {offset};
@ -20,12 +20,13 @@ let _query_add = `
insert into nxy_quotes insert into nxy_quotes
(nick, item, channel, created_by) (nick, item, channel, created_by)
values values
(?, ?, ?, ?); ($1, $2, $3, $4);
`; `;
module.exports = bot => { module.exports = bot => {
bot._trigger.set("quotes", new bot.trigger({ bot._trigger.set("quotes", new bot.trigger({
call: /^(\.|\/)q .*/i, call: /^(\.|\/)q .*/i,
active: false,
f: e => { f: e => {
let args = e.message.trim().substring(3).split(" "); let args = e.message.trim().substring(3).split(" ");
const cmd = args[0].toLowerCase(); const cmd = args[0].toLowerCase();
@ -40,7 +41,7 @@ module.exports = bot => {
args.shift(); args.shift();
let quote = args.join(" "); let quote = args.join(" ");
sql.exec(_query_add, [ nick, quote, `${e.network}.${e.channel}`, e.user.nick ]) sql.any(_query_add, [ nick, quote, `${e.network}.${e.channel}`, e.user.nick ])
.then(rows => { }) .then(rows => { })
.catch(err => { .catch(err => {
e.reply("duplicate!"); e.reply("duplicate!");
@ -70,7 +71,7 @@ module.exports = bot => {
.replace("{rankorder}", rand ? "+ 1,1" : (order === "asc" ? "+ 1,1" : "- 1,@s")) .replace("{rankorder}", rand ? "+ 1,1" : (order === "asc" ? "+ 1,1" : "- 1,@s"))
.replace("{order}", index ? order : "asc") .replace("{order}", index ? order : "asc")
.replace("{offset}", rand ? 0 : offset); .replace("{offset}", rand ? 0 : offset);
sql.exec(query, [nick, nick]) sql.any(query, [nick])
.then(rows => { .then(rows => {
if(!rows[1][0]) if(!rows[1][0])
return e.reply("index nicht vorhanden"); return e.reply("index nicht vorhanden");

View File

@ -6,7 +6,7 @@ const vm = require("vm")
, stringify = require("stringify-object"); , stringify = require("stringify-object");
let _contexts = new Map(); let _contexts = new Map();
sql.exec("select `data` from `useless` where `trigger` = 'sandbox_js'") sql.any("select data from useless where trigger = 'sandbox_js'")
.then(rows => { .then(rows => {
eval("_contexts = new Map([..."+JSON.parse(rows[0].data)+"])"); eval("_contexts = new Map([..."+JSON.parse(rows[0].data)+"])");
}) })
@ -38,7 +38,7 @@ module.exports = bot => {
.replace(/\n/g, "") .replace(/\n/g, "")
.replace(/\t/g, "") .replace(/\t/g, "")
.split("[native code]").join("")); .split("[native code]").join(""));
sql.exec("update `useless` set `data` = ? where `trigger` = 'sandbox_js';", tmp) sql.any("update useless set data = $1 where trigger = 'sandbox_js';", [tmp])
.then(() => { .then(() => {
e.reply(output); e.reply(output);
}) })

View File

@ -10,9 +10,12 @@ const data = {
}; };
Object.keys(data).forEach(cur => { Object.keys(data).forEach(cur => {
sql.exec("select `data` from `useless` where `trigger` = ? limit 1", [cur]) sql.any("select data from useless where trigger = $1 limit 1", [cur])
.then(rows => { .then(rows => {
data[cur] = JSON.parse(rows[0].data); data[cur] = JSON.parse(rows[0].data);
})
.catch(err => {
console.log(err);
}); });
}); });

View File

@ -13,7 +13,7 @@ const data = {
}; };
Object.keys(data).forEach(cur => { Object.keys(data).forEach(cur => {
sql.exec("select `data` from `useless` where `trigger` = ? limit 1", [cur]) sql.any("select data from useless where trigger = $1 limit 1", [cur])
.then(rows => { .then(rows => {
data[cur] = JSON.parse(rows[0].data); data[cur] = JSON.parse(rows[0].data);
}); });