Skip to content

Commit 983e95e

Browse files
committed
extract configs
extract configs Conflicts: app.js
1 parent 2231556 commit 983e95e

File tree

3 files changed

+53
-12
lines changed

3 files changed

+53
-12
lines changed

app.js

Lines changed: 39 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,44 +3,71 @@
33
const irc = require('irc');
44
const request = require('request');
55
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);
610

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+
// );
1216

1317
const messagesListner = {};
1418

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+
1529
//Generic message handler
1630
function respondMessage(message, idle, action) {
1731
messagesListner[message] = {idle, action};
1832
}
1933

20-
//Magic happens here
34+
//Listeners
35+
36+
//Message Listener
2137
client.addListener('message', function (from, to, message) {
2238
if (messagesListner[message]) {
2339
const msg = messagesListner[message];
2440
if (msg.idle > (new Date().getTime() - msg.timer)) {
25-
client.say('#ctf-br', `To ocupado!!!`);
41+
client.say('#ctf-bot-test', `To ocupado!!!`);
2642
return;
2743
}
2844
msg.timer = new Date().getTime();
2945
msg.action();
3046
}
3147
});
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+
3259

3360
//Message handlers
3461
respondMessage('!nextctf', 10000, () => {
3562
upcomingCtf()
3663
.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}`)
4168
})
4269
.catch(err => {
43-
client.say('#ctf-br', `${err}`);
70+
client.say('#ctf-bot-test', `${err}`);
4471
});
4572
});
4673

config.json

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"server": "rajaniemi.freenode.net",
3+
"bot_nick": "ctfbot",
4+
"channels": [
5+
"#ctf-br"
6+
]
7+
}

dev-config.json

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"server": "rajaniemi.freenode.net",
3+
"bot_nick": "ctfbot",
4+
"channels": [
5+
"#ctf-bot-test"
6+
]
7+
}

0 commit comments

Comments
 (0)