This commit is contained in:
Flummi 2017-11-28 11:40:58 +01:00
parent 5c4745fea6
commit ec595827b3

View File

@ -10,9 +10,9 @@ let _query_get = `
)
select *
from ranked_quotes
where lower(nick) like lower($1)
where nick ilike lower($1) {where}
order by {order}
limit 1
{limit}
{offset}
`;
let _query_add = `
@ -49,33 +49,58 @@ module.exports = bot => {
default: // get quote
let nick = cmd;
let index = 0
, params = [nick]
, offset = 0
, order = "rank asc"
, rand = false
, where = ""
, limit = "limit 1"
, query = _query_get;
if(args.length === 2) {
if(isNaN(args[1]))
return e.reply("Mayonnaise ist keine Zahl du Pfosten.");
if(args.length >= 2) {
if(isNaN(args[1])) {
where = "and item ilike $2";
params.push(`%${args[1]}%`);
limit = "";
}
else {
index = parseInt(args[1]);
offset = index;
order = (index < 0 ? "rank desc" : "rank asc");
offset = Math.abs(offset) - 1;
offset = Math.abs(index) - 1;
}
}
else
rand = true;
query = query
.replace("{where}", where)
.replace("{limit}", limit)
.replace("{order}", rand ? "random()" : order)
.replace("{offset}", rand ? "" : `offset ${offset}`);
sql.any(query, [nick])
sql.any(query, params)
.then(rows => {
if(!rows[0])
return e.reply("index nicht vorhanden");
if(rows.length > 1) {
let rank = 1;
let index = rank - 1;
if (args[2] && !isNaN(args[2])) {
rank = parseInt(args[2]);
index = rank - 1;
}
if(rank < 0) {
index = rows.length - Math.abs(rank);
rank = index + 1;
}
if (!rows[index])
return e.reply("index nicht vorhanden");
else
e.reply(`[${rank}/${rows.length}] <${rows[index].nick}> ${rows[index].item}`)
}
else
e.reply(`[${rows[0].rank}/${rows[0].total}] <${rows[0].nick}> ${rows[0].item}`)
})
.catch(err => console.log(err));
break;
}
}