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 4747dd5..c0611ec 100644 --- a/config/development.json +++ b/config/development.json @@ -1,8 +1,8 @@ { "server": "rajaniemi.freenode.net", - "botNick": "ctfbot-test", + "botNick": "l337-bot", "password": "", "channels": [ - "#ctf-bot-test" + "#ctf-br" ] } diff --git a/lib/handlers.js b/lib/handlers.js index ac9259a..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 !== 'ctfbot'){ + if(who !== botNick){ this.send('MODE', channel, '+v', who); - } + } }; const error = function(error) { console.error(error); }; +// const raw = function(command) { +// return console.log('RAW: ', command); +// }; + module.exports = {message, join, 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..2535bed 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}(${rank[i].points})`); + } + }) + .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 + `(${rank[i].points})` + ' '; + } + 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++`); });