replace pg-promise with pg

This commit is contained in:
Flummi 2019-08-22 09:56:16 +00:00
parent 1f194e8267
commit 8c5fb99cd3
10 changed files with 60 additions and 73 deletions

View File

@ -11,9 +11,9 @@
"author": "Flummi & jkhsjdhjs", "author": "Flummi & jkhsjdhjs",
"license": "WTFPL", "license": "WTFPL",
"dependencies": { "dependencies": {
"flumm-fetch-cookies": "git+https://gitfap.de/keinBot/flumm-fetch-cookies",
"cuffeo": "git+https://gitfap.de/keinBot/cuffeo", "cuffeo": "git+https://gitfap.de/keinBot/cuffeo",
"pg-promise": "^7.3.2", "flumm-fetch-cookies": "git+https://gitfap.de/keinBot/flumm-fetch-cookies",
"pg": "^7.12.1",
"stringify-object": "^3.3.0" "stringify-object": "^3.3.0"
} }
} }

View File

@ -1,7 +1,4 @@
import PG from "pg-promise"; import PG from "pg";
import { default as config } from "../../cfg/config.json"; import config from "../../cfg/config.json";
//const pgp = new PG(); export default new PG.Pool(config.sql);
const sql = (new PG())(config.sql);
export default sql;

View File

@ -9,12 +9,10 @@ const data = {
}; };
export default async bot => { export default async bot => {
Object.keys(data).forEach(cur => { for(const cur in data) {
sql.any("select data from useless where trigger = $1 limit 1", [cur]) const rows = (await sql.query("select data from useless where trigger = $1 limit 1", [cur])).rows[0];
.then(rows => { data[cur] = JSON.parse(rows.data);
data[cur] = JSON.parse(rows[0].data); }
});
});
return [{ return [{
name: "cookie", name: "cookie",

View File

@ -6,12 +6,10 @@ const data = {
}; };
export default async bot => { export default async bot => {
Object.keys(data).forEach(cur => { for(const cur in data) {
sql.any("select data from useless where trigger = $1 limit 1", [cur]) const rows = (await sql.query("select data from useless where trigger = $1 limit 1", [cur])).rows[0];
.then(rows => { data[cur] = JSON.parse(rows.data);
data[cur] = JSON.parse(rows[0].data); }
});
});
return [{ return [{
name: "dope", name: "dope",

View File

@ -19,7 +19,7 @@ export default async bot => {
call: /Mc.*iaC/, call: /Mc.*iaC/,
set: "nxy", set: "nxy",
f: async e => { f: async e => {
await sql.any(_query_add, [e.message.match(/(Mc\S+iaC?)/)[0]]); await sql.query(_query_add, [e.message.match(/(Mc\S+iaC?)/)[0]]);
} }
}, { }, {
name: "mcmaniac_get", name: "mcmaniac_get",
@ -50,7 +50,7 @@ export default async bot => {
.split("{order}").join(order) .split("{order}").join(order)
.split("{offset}").join(offset); .split("{offset}").join(offset);
const rows = await sql.any(query); const rows = (await sql.query(query)).rows;
e.reply(`[${rows[0].rank}/${rows[0].total}] ${rows[0].item}`); e.reply(`[${rows[0].rank}/${rows[0].total}] ${rows[0].item}`);
} }
}]; }];

View File

@ -29,19 +29,16 @@ export default async bot => {
name: "qrnd", name: "qrnd",
call: /^(\.|\/)q(rnd)?$/i, call: /^(\.|\/)q(rnd)?$/i,
set: "all", set: "all",
f: e => { f: async e => {
fetch("https://nxy.totally.rip/api/quotes.php?c=nick,item") const res = await (await fetch("https://nxy.totally.rip/api/quotes.php?c=nick,item")).json();
.then(res => res.json()) const quote = res.data.quotes[~~(Math.random() * res.data.quotes.length)];
.then(res => { e.reply(`<[b]${quote.nick}[/b]> [i]${quote.item}[/i]`);
const quote = res.data.quotes[~~(Math.random() * res.data.quotes.length)];
e.reply(`<[b]${quote.nick}[/b]> [i]${quote.item}[/i]`);
}).catch(console.error);
} }
}, { }, {
name: "quotes", name: "quotes",
call: /^(\.|\/)q .*/i, call: /^(\.|\/)q .*/i,
set: "nxy", set: "nxy",
f: e => { f: async 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();
let nick = ""; let nick = "";
@ -55,12 +52,11 @@ export default async bot => {
args.shift(); args.shift();
let quote = args.join(" "); let quote = args.join(" ");
sql.any(_query_add, [ nick, quote, `${e.network}.${e.channel}`, e.user.nick ]) try {
.then(rows => { }) await sql.query(_query_add, [ nick, quote, `${e.network}.${e.channel}`, e.user.nick ]);
.catch(err => { } catch(e) {
console.log(err); return e.reply("duplicate!");
e.reply("duplicate!"); }
});
break; break;
default: // get quote default: // get quote
nick = cmd; nick = cmd;
@ -93,31 +89,27 @@ export default async bot => {
.replace("{limit}", limit) .replace("{limit}", limit)
.replace("{order}", rand ? "random()" : order) .replace("{order}", rand ? "random()" : order)
.replace("{offset}", rand ? "" : `offset ${offset}`); .replace("{offset}", rand ? "" : `offset ${offset}`);
sql.any(query, params)
.then(rows => { const rows = (await sql.query(query, params)).rows;
if(!rows[0])
return e.reply("index nicht vorhanden"); if (!rows || rows.length === 0)
if(rows.length > 1) { return e.reply("index nicht vorhanden");
let rank = 1;
let index = rank - 1; let rank = 1;
if (args[2] && !isNaN(args[2])) { index = rank - 1;
rank = parseInt(args[2]); if (args[2] && !isNaN(args[2])) {
index = rank - 1; rank = parseInt(args[2]);
} index = rank - 1;
if(rank < 0) { }
index = rows.length - Math.abs(rank); if (rank < 0) {
rank = index + 1; index = rows.length - Math.abs(rank);
} rank = index + 1;
if (!rows[index]) }
return e.reply("index nicht vorhanden"); if (!rows[index])
else return e.reply("index nicht vorhanden");
e.reply(`[${rank}/${rows.length}] <${rows[index].nick}> ${rows[index].item}`)
} e.reply(`[${rows[index].rank}/${rows[index].total}] <${rows[index].nick}> ${rows[index].item}`);
else break;
e.reply(`[${rows[0].rank}/${rows[0].total}] <${rows[0].nick}> ${rows[0].item}`)
})
.catch(err => console.log(err));
break;
} }
} }
}]; }];

View File

@ -6,6 +6,7 @@ export default async bot => {
name: "rape", name: "rape",
call: /^(\.|\/)rape/i, call: /^(\.|\/)rape/i,
set: "nxy", set: "nxy",
active: false,
help: { help: {
text: "Rapes a nick and eventually charge for it", text: "Rapes a nick and eventually charge for it",
usage: "[b].rape[/b] [i](<nick>)[/i]" usage: "[b].rape[/b] [i](<nick>)[/i]"
@ -21,7 +22,7 @@ export default async bot => {
const fined = [e.user.nick, nick][rand]; const fined = [e.user.nick, nick][rand];
const reason = ["raping", "being too lewd and getting raped"][rand]; const reason = ["raping", "being too lewd and getting raped"][rand];
sql.any("insert into nxy_users (nick, fines) values (lower($1), $2) on conflict (nick) do update set fines = nxy_users.fines + excluded.fines returning fines", [fined, fine]) sql.query("insert into nxy_users (nick, fines) values (lower($1), $2) on conflict (nick) do update set fines = nxy_users.fines + excluded.fines returning fines", [fined, fine])
.then(rows => { .then(rows => {
e.replyAction(`fines [b]${nick}[/b] [b]$${fine}[/b] for ${reason}. You owe: [b]$${rows[0].fines}[/b].`); e.replyAction(`fines [b]${nick}[/b] [b]$${fine}[/b] for ${reason}. You owe: [b]$${rows[0].fines}[/b].`);
}) })
@ -34,13 +35,14 @@ export default async bot => {
name: "owe", name: "owe",
call: /^(\.|\/)owe/i, call: /^(\.|\/)owe/i,
set: "nxy", set: "nxy",
active: false,
help: { help: {
text: "Shows how much a nick owes", text: "Shows how much a nick owes",
usage: "[b].owe[/b] [i](<nick>)[/i]" usage: "[b].owe[/b] [i](<nick>)[/i]"
}, },
f: e => { f: e => {
const nick = e.args[0] || e.user.nick; const nick = e.args[0] || e.user.nick;
sql.any("select fines from nxy_users where lower(nick) = lower($1) limit 1", [nick]) sql.query("select fines from nxy_users where lower(nick) = lower($1) limit 1", [nick])
.then(rows => { .then(rows => {
if(!rows[0]) if(!rows[0])
return e.reply("user not found"); return e.reply("user not found");

View File

@ -8,7 +8,7 @@ import stringify from "stringify-object";
let _contexts = new Map(); let _contexts = new Map();
export default async bot => { export default async bot => {
sql.any("select prefix, sandbox from nxy_users where sandbox != 'NULL'") sql.query("select prefix, sandbox from nxy_users where sandbox != 'NULL'")
.then(rows => rows.forEach(row => eval(`_contexts.set(row.prefix, ${JSON.parse(row.sandbox)});`))) .then(rows => rows.forEach(row => eval(`_contexts.set(row.prefix, ${JSON.parse(row.sandbox)});`)))
.catch(err => console.log("nichts vorhanden lol", err)); .catch(err => console.log("nichts vorhanden lol", err));
@ -37,7 +37,7 @@ export default async bot => {
.replace(/\n/g, "") .replace(/\n/g, "")
.replace(/\t/g, "") .replace(/\t/g, "")
.split("[native code]").join("")); .split("[native code]").join(""));
sql.any("insert into nxy_users (nick, prefix, sandbox) values (lower($1), $3, $2) on conflict (prefix) do update set sandbox = $2, nick = lower($1)", [ sql.query("insert into nxy_users (nick, prefix, sandbox) values (lower($1), $3, $2) on conflict (prefix) do update set sandbox = $2, nick = lower($1)", [
(e.type === "tg" ? e.user.username || e.user.nick : e.user.nick), tmp, e.user.prefix (e.type === "tg" ? e.user.username || e.user.nick : e.user.nick), tmp, e.user.prefix
]).then(() => e.reply(e.user.nick + ": " + output)) ]).then(() => e.reply(e.user.nick + ": " + output))
.catch(err => console.log(err)); .catch(err => console.log(err));

View File

@ -11,10 +11,10 @@ const data = {
}; };
export default async bot => { export default async bot => {
for(const cur in data) {
Object.keys(data).forEach(cur => sql.any("select data from useless where trigger = $1 limit 1", [cur]) const rows = (await sql.query("select data from useless where trigger = $1 limit 1", [cur])).rows[0];
.then(rows => data[cur] = JSON.parse(rows[0].data)) data[cur] = JSON.parse(rows.data);
.catch(console.error)); }
return [{ return [{
name: "kiss", name: "kiss",

View File

@ -13,10 +13,10 @@ const data = {
}; };
export default async bot => { export default async bot => {
for(const cur in data) {
Object.keys(data).forEach(cur => sql.any("select data from useless where trigger = $1 limit 1", [cur]) const rows = (await sql.query("select data from useless where trigger = $1 limit 1", [cur])).rows[0];
.then(rows => data[cur] = JSON.parse(rows[0].data)) data[cur] = JSON.parse(rows.data);
.catch(console.error)); }
return [{ return [{
name: "abschieben", name: "abschieben",