import sql from "../sql.js"; let _query_get = ` select item, rank() over (order by id), count(*) over (rows between unbounded preceding and unbounded following) as total from mcmaniacs order by {order} limit 1 `; let _query_add = ` insert into mcmaniacs (item) values ($1) on conflict do nothing `; module.exports = bot => { bot._trigger.set("mcmaniac_add", new bot.trigger({ call: /Mc.*iaC/, active: true, clients: ["irc", "tg"], f: e => { const match = e.message.match(/.*(Mc\S+iaC?).*/)[0]; sql.any(_query_add, [match]) .then() .catch(err => {}); } })); bot._trigger.set("mcmaniac_get", new bot.trigger({ call: /^(\.|\/)mcmaniac/i, active: true, clients: ["irc", "tg"], f: e => { const args = e.message.trim().substring(10); let order = "id asc" , offset , query = _query_get; if (args.length > 0) { if (isNaN(parseInt(args))) return e.reply("Mayonnaise ist keine Zahl du Pfosten."); const index = parseInt(args); if (index < 0) order = "id desc"; else if(index === 0) offset = 1; else offset = index; offset = Math.abs(index) - 1; query += " offset {offset}"; } else order = "random()"; query = query .split("{order}").join(order) .split("{offset}").join(offset); sql.any(query) .then(rows => { e.reply(`[${rows[0].rank}/${rows[0].total}] ${rows[0].item}`); }) .catch(err => { console.log(err); }); } })); };