diff --git a/src/trigger/f0ckrand.js b/src/trigger/f0ckrand.js index 2594c8a..455ab08 100644 --- a/src/trigger/f0ckrand.js +++ b/src/trigger/f0ckrand.js @@ -5,7 +5,7 @@ module.exports = (lib) => { level: 0, active: 1, func: (e) => { - let args = lib.sql.escape(e.message).split(" ").slice(2); + let args = lib.sql.escape(e.message).trim().split(" ").slice(2); let params = { "in": [], "not": [] @@ -19,23 +19,16 @@ module.exports = (lib) => { params.in.push(name); } - let query = "select `id`,`username` from `f0ck`.`items` "; + let vars = params.in.concat(exc); + + params.in = new Array(params.in.length).fill('username LIKE ?'); + params.out = params.in.length > 0 ? [] : new Array(params.out.length).fill('username NOT LIKE ?'); + let where = params.in.concat(params.out); + let query = `SELECT id, username FROM f0ck.items ${where.length > 0 ? `WHERE ${where.join(' || ')}` : ''} ORDER BY rand() LIMIT 1`; - if(args.length > 0) - query += "where "; - if(params.in.length > 0) - query += "`username` like '" + params.in.join("' || `username` like '"); - if(params.not.length > 0) - query += "`username` not like '" + params.not.join("' || `username` not like '"); - - query += " order by rand() limit 1"; - console.log( query ); - lib.sql.query(query, (err, rows, fields) => { - if(!err && rows.length > 0) - e.reply(`f0ckrnd: ${lib.cfg.main.url}/${rows[0].id} by: ${rows[0].username}`); - else - e.reply("nothing found, f0cker"); + lib.sql.query(query, vars, (err, rows) => { + e.reply(!err && rows.length > 0 ? `f0ckrnd: ${lib.cfg.main.url}/${rows[0].id} by: ${rows[0].username}` : 'nothing found, f0cker'); }); }, desc: 'Random-f0ck'