import sql from "../sql"; 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 `; export default async bot => { return [{ name: "mcmaniac_add", call: /Mc.*iaC/, set: "nxy", f: async e => { await sql.query(_query_add, [e.message.match(/(Mc\S+iaC?)/)[0]]); } }, { name: "mcmaniac_get", call: /^(\.|\/)mcmaniac/i, set: "nxy", f: async 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); const rows = (await sql.query(query)).rows; e.reply(`[${rows[0].rank}/${rows[0].total}] ${rows[0].item}`); } }]; };