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/
.vscode/
logs/
package-lock.json

View File

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

View File

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

View File

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

View File

@ -8,10 +8,7 @@ let cfg = {
};
const read = () => new Promise((resolve, reject) => {
sql.exec("select * from `cfg`", (err, rows) => {
if(err || rows.length < 1)
reject({ error: "no cfg" });
else {
sql.any("select * from cfg").then(rows => {
for (let row in rows) {
cfg[rows[row].class][rows[row].key] = {
val: ((type, value) => {
@ -31,8 +28,10 @@ const read = () => new Promise((resolve, reject) => {
}
}
resolve();
}
});
})
.catch(err => {
reject("no cfg");
})
});
const write = (kat, key, value, type, cb) => {
@ -40,10 +39,10 @@ const write = (kat, key, value, type, cb) => {
value = JSON.stringify(value);
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);
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);
read().then(() => {
if(cb) cb();
@ -51,7 +50,7 @@ const write = (kat, key, value, type, cb) => {
});
}
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);
read().then(() => {
if (cb) cb();

View File

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

View File

@ -54,7 +54,7 @@ module.exports = bot => {
const userlevel = getLevel(e.network, user).level;
if(userlevel > 0) { // check if user is admin
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 => {
loadAdmins();
return e.reply(`deleted user ${user.nickname}`);
@ -64,7 +64,7 @@ module.exports = bot => {
})
}
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 => {
loadAdmins();
return e.reply(`changed user [b]${user.nickname}[/b] (${userlevel} -> ${level})`);
@ -78,7 +78,7 @@ module.exports = bot => {
}
}
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 => {
loadAdmins();
return e.reply(`added new user [b]${user.nickname}[/b] with level ${level}`);

View File

@ -6,8 +6,9 @@ const data = {
};
Object.keys(data).forEach(cur => {
sql.exec(`select \`data\` from \`useless\` where \`trigger\` = '${cur}' limit 1`, (err, row) => {
data[cur] = JSON.parse(row[0].data);
sql.any("select data from useless where trigger = $1 limit 1", [cur])
.then(rows => {
data[cur] = JSON.parse(rows[0].data);
});
});
@ -15,30 +16,20 @@ module.exports = bot => {
bot._trigger.set("dope", new bot.trigger({
call: /^(\.|\/)dope/i,
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 action_types = Object.keys(data.dope_actions);
const strain_type = strain_types[~~(Math.random() * strain_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)]
, 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]${args[0]}[/b]`);
e.replyAction(`${action[0]} a ${consume_type} of the finest ${strain_type} "${strain}" ${action[1]} [b]${e.args[0] || e.user.nick}[/b]`);
}
}));
bot._trigger.set("meth", new bot.trigger({
call: /^(\.|\/)meth/i,
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];
e.replyAction(`legt [b]${args[0]}[/b] eine dicke Line Meth \\________`);
e.replyAction(`legt [b]${e.args[0] || e.user.nick}[/b] eine dicke Line Meth \\________`);
}
}));
};

View File

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

View File

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

View File

@ -6,7 +6,7 @@ const vm = require("vm")
, stringify = require("stringify-object");
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 => {
eval("_contexts = new Map([..."+JSON.parse(rows[0].data)+"])");
})
@ -38,7 +38,7 @@ module.exports = bot => {
.replace(/\n/g, "")
.replace(/\t/g, "")
.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(() => {
e.reply(output);
})

View File

@ -10,9 +10,12 @@ const data = {
};
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 => {
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 => {
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 => {
data[cur] = JSON.parse(rows[0].data);
});