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