|
3 | 3 | const irc = require('irc'); |
4 | 4 | const request = require('request'); |
5 | 5 | const cheerio = require('cheerio'); |
| 6 | +const fs = require('fs'); |
| 7 | +const NODE_ENV = process.env.NODE_ENV || 'development'; |
| 8 | +let config = loadEnv(); |
| 9 | +console.log(config); |
6 | 10 |
|
7 | | -const client = new irc.Client( |
8 | | - 'rajaniemi.freenode.net', |
9 | | - 'ctfbot', |
10 | | - { channels: ['#ctf-br'] } |
11 | | -); |
| 11 | +// const client = new irc.Client( |
| 12 | +// 'rajaniemi.freenode.net', |
| 13 | +// 'ctfbot', |
| 14 | +// { channels: ['#ctf-bot-test'] } |
| 15 | +// ); |
12 | 16 |
|
13 | 17 | const messagesListner = {}; |
14 | 18 |
|
| 19 | +function loadEnv() { |
| 20 | + try { |
| 21 | + const file = NODE_ENV != 'production' ? 'config.json': 'dev-config.json' |
| 22 | + return JSON.parse(fs.readFileSync(file)); |
| 23 | + } catch (err) { |
| 24 | + console.error(err); |
| 25 | + process.exit(); |
| 26 | + } |
| 27 | +} |
| 28 | + |
15 | 29 | //Generic message handler |
16 | 30 | function respondMessage(message, idle, action) { |
17 | 31 | messagesListner[message] = {idle, action}; |
18 | 32 | } |
19 | 33 |
|
20 | | -//Magic happens here |
| 34 | +//Listeners |
| 35 | + |
| 36 | +//Message Listener |
21 | 37 | client.addListener('message', function (from, to, message) { |
22 | 38 | if (messagesListner[message]) { |
23 | 39 | const msg = messagesListner[message]; |
24 | 40 | if (msg.idle > (new Date().getTime() - msg.timer)) { |
25 | | - client.say('#ctf-br', `To ocupado!!!`); |
| 41 | + client.say('#ctf-bot-test', `To ocupado!!!`); |
26 | 42 | return; |
27 | 43 | } |
28 | 44 | msg.timer = new Date().getTime(); |
29 | 45 | msg.action(); |
30 | 46 | } |
31 | 47 | }); |
| 48 | +//Give voice for new members join when they join |
| 49 | +client.addListener('join', function(channel, who){ |
| 50 | + if(who !== 'ctfbot'){ |
| 51 | + client.send('MODE', channel, '+v', who); |
| 52 | + } |
| 53 | +}); |
| 54 | +//Error handler |
| 55 | +client.addListener('error', function(error){ |
| 56 | + console.log(error); |
| 57 | +}); |
| 58 | + |
32 | 59 |
|
33 | 60 | //Message handlers |
34 | 61 | respondMessage('!nextctf', 10000, () => { |
35 | 62 | upcomingCtf() |
36 | 63 | .then(ctf => { |
37 | | - client.say('#ctf-br', `Nome: ${ctf.name}`) |
38 | | - client.say('#ctf-br', `Data: ${ctf.date}`) |
39 | | - client.say('#ctf-br', `Tipo: ${ctf.type}`) |
40 | | - client.say('#ctf-br', `Site: ${ctf.link}`) |
| 64 | + client.say('#ctf-bot-test', `Nome: ${ctf.name}`) |
| 65 | + client.say('#ctf-bot-test', `Data: ${ctf.date}`) |
| 66 | + client.say('#ctf-bot-test', `Tipo: ${ctf.type}`) |
| 67 | + client.say('#ctf-bot-test', `Site: ${ctf.link}`) |
41 | 68 | }) |
42 | 69 | .catch(err => { |
43 | | - client.say('#ctf-br', `${err}`); |
| 70 | + client.say('#ctf-bot-test', `${err}`); |
44 | 71 | }); |
45 | 72 | }); |
46 | 73 |
|
|
0 commit comments