From a514314a93d6624345402ab4bdd1a01a9434075c Mon Sep 17 00:00:00 2001 From: fernandoGuisso Date: Tue, 27 Sep 2016 20:24:14 -0300 Subject: [PATCH 1/2] rank interno ctf-br --- config/development.json | 5 +++-- lib/handlers.js | 12 ++++++------ lib/helpers.js | 22 +++++++++++++++++++++- lib/messages.js | 28 ++++++++++++++++++++++++++++ 4 files changed, 58 insertions(+), 9 deletions(-) diff --git a/config/development.json b/config/development.json index 4747dd5..2da694b 100644 --- a/config/development.json +++ b/config/development.json @@ -1,8 +1,9 @@ { "server": "rajaniemi.freenode.net", - "botNick": "ctfbot-test", + "botNick": "ctf-br-bot", "password": "", "channels": [ - "#ctf-bot-test" + "#node-bots", + "#ctf-br" ] } diff --git a/lib/handlers.js b/lib/handlers.js index ac9259a..4928308 100644 --- a/lib/handlers.js +++ b/lib/handlers.js @@ -17,14 +17,14 @@ const message = function(from, to, message) { } } -const join = function (channel, who) { - if(who !== 'ctfbot'){ - this.send('MODE', channel, '+v', who); - } -}; +// const join = function (channel, who) { +// if(who !== 'ctf-br-bot'){ +// this.send('MODE', channel, '+v', who); +// } +// }; const error = function(error) { console.error(error); }; -module.exports = {message, join, error}; +module.exports = {message, error}; diff --git a/lib/helpers.js b/lib/helpers.js index ac745eb..f06669b 100644 --- a/lib/helpers.js +++ b/lib/helpers.js @@ -20,6 +20,26 @@ function upcomingCtf() { }) } +function ranking() { + return new Promise((resolve, reject) => { + request('https://ctf.tecland.com.br/ranking/game/scoreboard/table/', (error, response, body) => { + if(error) reject(error) + const $ = cheerio.load(body); + const rank = {}; + $('dd').each(function(i, elem){ + rank[i] = { + 'name': $(this).find('td').eq(1).text(), + 'position': $(this).find('td').eq(3).text(), + 'points': $(this).find('td').eq(5).text(), + 'challs': $(this).find('td').eq(9).text() + }; + }); + resolve(rank); + }); + }) +} + module.exports = { - upcomingCtf + upcomingCtf, + ranking } diff --git a/lib/messages.js b/lib/messages.js index f2a3926..f31f912 100644 --- a/lib/messages.js +++ b/lib/messages.js @@ -21,6 +21,34 @@ respondMessage('nextctf', 10000, function(from, to, message) { }); }); +respondMessage('rank', 10000, function(from, to, message){ + helper.ranking() + .then(rank => { + for(let i = 0; i < 5; i++){ + this.say(to, `${rank[i].position} ${rank[i].name}`); + } + }) + .catch(err => { + console.error(err); + this.say(to, `Houston we have a problem! Try later ${from}`); + }); +}); + +respondMessage('allrank', 10000, function(from, to, message){ + helper.ranking() + .then(rank => { + let text = ''; + for(let i in rank){ + text += rank[i].position + ' ' + rank[i].name + ' '; + } + this.say(to, text); + }) + .catch(err => { + console.error(err); + this.say(to, `Houston we have a problem! Try later ${from}`); + }); +}); + respondMessage('masters', 0, function(from, to, message) { this.say(to, `v3ntur4++ hackaponey++`); }); From 10bf661f0e74531765760f56f44989af713ac54e Mon Sep 17 00:00:00 2001 From: Fernando Guisso Date: Thu, 29 Sep 2016 18:48:57 -0300 Subject: [PATCH 2/2] ping/pong --- app.js | 9 +++++++++ config/development.json | 5 ++--- lib/handlers.js | 35 +++++++++++++++++++++-------------- lib/messages.js | 4 ++-- 4 files changed, 34 insertions(+), 19 deletions(-) diff --git a/app.js b/app.js index e6dbcc7..36e5e7a 100644 --- a/app.js +++ b/app.js @@ -18,6 +18,15 @@ const client = new irc.Client( } ); +// ping / pong for don't timeout +client.on('raw', (message) => { + console.log('RAW:', message); + if(message.command === 'PING'){ + client.send('PONG', message.args[0]); + console.log('Pong: ', message); + } +}); + // Load env variables based on filename (duh~!?) function loadEnv() { try { diff --git a/config/development.json b/config/development.json index 2da694b..c0611ec 100644 --- a/config/development.json +++ b/config/development.json @@ -1,9 +1,8 @@ { "server": "rajaniemi.freenode.net", - "botNick": "ctf-br-bot", + "botNick": "l337-bot", "password": "", "channels": [ - "#node-bots", - "#ctf-br" + "#ctf-br" ] } diff --git a/lib/handlers.js b/lib/handlers.js index 4928308..dacedee 100644 --- a/lib/handlers.js +++ b/lib/handlers.js @@ -2,29 +2,36 @@ const messages = require('./messages'); const PREFIX = "$"; +const botNick = "l337-bot"; const message = function(from, to, message) { - if(message && message[0] != PREFIX) return; + if(from !== botNick){ + if(message && message[0] != PREFIX) return; - const messageHandler = messages[message.substring(1)]; - if (messageHandler) { - if (messageHandler.idle > (new Date().getTime() - messageHandler.timer)) { - this.say(to, `To ocupado!`); - return; + const messageHandler = messages[message.substring(1)]; + if (messageHandler) { + if (messageHandler.idle > (new Date().getTime() - messageHandler.timer)) { + this.say(to, `To ocupado!`); + return; + } + messageHandler.timer = new Date().getTime(); + messageHandler.action.apply(this, arguments); } - messageHandler.timer = new Date().getTime(); - messageHandler.action.apply(this, arguments); } } -// const join = function (channel, who) { -// if(who !== 'ctf-br-bot'){ -// this.send('MODE', channel, '+v', who); -// } -// }; +const join = function (channel, who) { + if(who !== botNick){ + this.send('MODE', channel, '+v', who); + } +}; const error = function(error) { console.error(error); }; -module.exports = {message, error}; +// const raw = function(command) { +// return console.log('RAW: ', command); +// }; + +module.exports = {message, join, error}; diff --git a/lib/messages.js b/lib/messages.js index f31f912..2535bed 100644 --- a/lib/messages.js +++ b/lib/messages.js @@ -25,7 +25,7 @@ respondMessage('rank', 10000, function(from, to, message){ helper.ranking() .then(rank => { for(let i = 0; i < 5; i++){ - this.say(to, `${rank[i].position} ${rank[i].name}`); + this.say(to, `${rank[i].position} ${rank[i].name}(${rank[i].points})`); } }) .catch(err => { @@ -39,7 +39,7 @@ respondMessage('allrank', 10000, function(from, to, message){ .then(rank => { let text = ''; for(let i in rank){ - text += rank[i].position + ' ' + rank[i].name + ' '; + text += rank[i].position + ' ' + rank[i].name + `(${rank[i].points})` + ' '; } this.say(to, text); })