This commit is contained in:
Flummi 2017-11-23 07:00:01 +01:00
parent 964301d148
commit 8185feb261
3 changed files with 116 additions and 13 deletions

31
package-lock.json generated
View File

@ -4,6 +4,12 @@
"lockfileVersion": 1, "lockfileVersion": 1,
"requires": true, "requires": true,
"dependencies": { "dependencies": {
"@types/node": {
"version": "7.0.48",
"resolved": "https://registry.npmjs.org/@types/node/-/node-7.0.48.tgz",
"integrity": "sha512-LLlXafM3BD52MH056tHxTXO8JFCnpJJQkdzIU3+m8ew+CXJY/5zIXgDNb4TK/QFvlI8QexLS5tL+sE0Qhegr1w==",
"dev": true
},
"ajv": { "ajv": {
"version": "5.4.0", "version": "5.4.0",
"resolved": "https://registry.npmjs.org/ajv/-/ajv-5.4.0.tgz", "resolved": "https://registry.npmjs.org/ajv/-/ajv-5.4.0.tgz",
@ -119,7 +125,7 @@
"babel-register": "6.26.0", "babel-register": "6.26.0",
"babel-runtime": "6.26.0", "babel-runtime": "6.26.0",
"chokidar": "1.7.0", "chokidar": "1.7.0",
"commander": "2.11.0", "commander": "2.12.0",
"convert-source-map": "1.5.0", "convert-source-map": "1.5.0",
"fs-readdir-recursive": "1.1.0", "fs-readdir-recursive": "1.1.0",
"glob": "7.1.2", "glob": "7.1.2",
@ -909,10 +915,13 @@
} }
}, },
"commander": { "commander": {
"version": "2.11.0", "version": "2.12.0",
"resolved": "https://registry.npmjs.org/commander/-/commander-2.11.0.tgz", "resolved": "https://registry.npmjs.org/commander/-/commander-2.12.0.tgz",
"integrity": "sha512-b0553uYA5YAEGgyYIGYROzKQ7X5RAqedkfjiZxwi0kL1g3bOaBNNZfYkzt/CL0umgD5wc9Jec2FbB98CjkMRvQ==", "integrity": "sha512-0FAmW4svUhnHJzjJHrg0vHi8+3Wp5mqvZTOui03Tc0515CToaw1BD7WC8ROcY08UnTJJOr4essVYvXBSPYeV2w==",
"dev": true "dev": true,
"requires": {
"@types/node": "7.0.48"
}
}, },
"concat-map": { "concat-map": {
"version": "0.0.1", "version": "0.0.1",
@ -1594,9 +1603,9 @@
} }
}, },
"mime": { "mime": {
"version": "1.4.1", "version": "1.5.0",
"resolved": "https://registry.npmjs.org/mime/-/mime-1.4.1.tgz", "resolved": "https://registry.npmjs.org/mime/-/mime-1.5.0.tgz",
"integrity": "sha512-KI1+qOZu5DcW6wayYHSzR/tXKCDC5Om4s1z2QJjDULzLcmf3DvzS7oluY4HCTrc+9FiKmWUgeNLg7W3uIQvxtQ==" "integrity": "sha512-v/jMDoK/qKptnTuC3YUNbIj8uUYvTCIHzVu9BHldKSWja48wusAtfjlcBlqnFrqClu3yf69ScDxBPrIyFnF51g=="
}, },
"mime-db": { "mime-db": {
"version": "1.30.0", "version": "1.30.0",
@ -1661,16 +1670,14 @@
"depd": "1.1.1", "depd": "1.1.1",
"eventemitter3": "2.0.3", "eventemitter3": "2.0.3",
"file-type": "3.9.0", "file-type": "3.9.0",
"mime": "1.4.1", "mime": "1.5.0",
"pump": "1.0.3", "pump": "1.0.3",
"request": "2.83.0", "request": "2.83.0",
"request-promise": "4.2.2" "request-promise": "4.2.2"
} }
}, },
"nodejs-mysql": { "nodejs-mysql": {
"version": "0.1.3", "version": "github:flummi/nodejs-mysql#8b1cbb379951f7dd891000462f3beb398a9783a3",
"resolved": "https://registry.npmjs.org/nodejs-mysql/-/nodejs-mysql-0.1.3.tgz",
"integrity": "sha1-AuncQmi1HT7AvFlDX19JLPkI/Ek=",
"requires": { "requires": {
"bluebird": "3.5.1", "bluebird": "3.5.1",
"mysql": "2.15.0" "mysql": "2.15.0"

View File

@ -13,7 +13,7 @@
"license": "WTFPL", "license": "WTFPL",
"dependencies": { "dependencies": {
"node-telegram-bot-api": "^0.29.0", "node-telegram-bot-api": "^0.29.0",
"nodejs-mysql": "^0.1.3", "nodejs-mysql": "github:flummi/nodejs-mysql",
"request": "^2.83.0", "request": "^2.83.0",
"winston": "^2.4.0", "winston": "^2.4.0",
"xml2js": "^0.4.19", "xml2js": "^0.4.19",

96
src/inc/trigger/quotes.js Normal file
View File

@ -0,0 +1,96 @@
import sql from "../sql.js";
let _query_get = `
set @s := (select count(id) as sum from nxy_quotes where nick like ?);
select id, nick, item, rank, @s as sum
from (
select @r:= if(@u = nick, @r {rankorder}) as rank,
id, nick, item, @u:= nick
from nxy_quotes,
(select @r:= 1) as r,
(select @u:= '') as u
order by nick, id {order}
) as blah
where nick like ?
{rand}
limit 1
offset {offset};
`;
let _query_add = `
insert into nxy_quotes
(nick, item, channel, created_by)
values
(?, ?, ?, ?);
`;
module.exports = bot => {
bot._trigger.set("quotes", {
call: /^(\.|\/)q .*/i,
level: 0,
active: true,
clients: ["irc", "tg"],
f: e => {
/* <cmd> <nick> <quote>...
* <nick> [<index>]
*/
let args = e.message.trim().substring(3).split(" ");
const cmd = args[0].toLowerCase();
let nick;
switch(cmd) {
case "add": // add user quote
if(args.length < 3)
return e.reply("ob du behindert bist.");
args.shift();
nick = args[0].match(/<?[~&@%+]?([a-zA-Z0-9_\-^`|\\\[\]{}]+)>?/)[1];
args.shift();
let quote = args.join(" ");
try {
sql.exec(_query_add, [ nick, quote, `${e.network}.${e.channel}`, e.user.nick ])
.then(rows => { })
.catch(err => {
e.reply("duplicate!");
});
}
catch(err) {
e.reply("duplicate!");
}
break;
default: // get quote
let nick = cmd;
let index = 0
, offset = 0
, order = "asc"
, rand = false
, query = _query_get;
if(args.length === 2) {
if(isNaN(args[1]))
return e.reply("Mayonnaise ist keine Zahl du Pfosten.");
index = parseInt(args[1]);
offset = index;
order = (index < 0 ? "desc" : "asc");
offset = Math.abs(offset) - 1;
}
else
rand = true;
query = query
.replace("{rand}", rand ? "order by rand()" : "")
.replace("{rankorder}", rand ? "+ 1,1" : (order === "asc" ? "+ 1,1" : "- 1,@s"))
.replace("{order}", index ? order : "asc")
.replace("{offset}", rand ? 0 : offset);
sql.exec(query, [nick, nick])
.then(rows => {
if(!rows[1][0])
return e.reply("index nicht vorhanden");
e.reply(`[${rows[1][0].rank}/${rows[1][0].sum}] <${rows[1][0].nick}> ${rows[1][0].item}`)
})
.catch(err => console.log(err));
break;
}
}
});
};