-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbot.js
More file actions
130 lines (123 loc) · 3.99 KB
/
bot.js
File metadata and controls
130 lines (123 loc) · 3.99 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
var chall= require("./challenges.js")
// Get the lib
var irc = require("irc");
var fs = require("fs");
Object.prototype.isEmpty = function() {
for(var key in this) {
if(this.hasOwnProperty(key))
return false;
}
return true;
}
var config = JSON.parse(fs.readFileSync("config.json"));
function printObject(o) {
var out = '';
for (var p in o) {
out += p + ': ' + o[p] + '\n';
}
return out;
}
// Create the bot name
var bot = new irc.Client(config.server, config.botName, {
channels: config.channels
});
var tasks={};
var cleanTasks = function(to, ob) {
delete tasks[to][ob.def];
if (isEmpty(tasks[to]))
delete tasks[to];
}
var endTimeout = function(to, ob) {
bot.say(to,"Ouch! challenge not solved!");
cleanTasks(to, ob);
}
// Listen for any message
//nick=nickname, to=channel; text=content
bot.addListener("message", function(nick, to, text, message) {
var arrText = text.split(" ");
if (arrText[0][0]!= '@')
return;
if (arrText[0]=="@challenge") {
if (arrText.length==1) {
bot.say (to, "give me the name of a challenge!");
return;
}
console.log("received challenge " + arrText[1]);
if (!(arrText[1] in chall.challenges)) {
bot.say (to, "not a challenge, you dumbass");
return;
}
var ch_container = chall.challenges[arrText[1]];
if (! tasks.hasOwnProperty(to)) {
tasks[to]={}
}
if (tasks[to].hasOwnProperty(ch_container.def)) {
bot.say(to, "Challenge already Launched\n"+ tasks[to][ch_container.def].ch.chall);
return;
}
else {
var ob = ch_container.chall();
bot.say(to, "Challenge "+arrText[1]+"\n"+ch_container.rules+"\n"+ob.chall);
var timeoutID = setTimeout(endTimeout, ch_container.delay,to,ob);
tasks[to][ch_container.def]={"timeout":timeoutID, "ch":ob, "base": ch_container};
return;
}
}
else if (arrText[0]==="@sol") {
if (arrText.length!=3) {
bot.say(to,"What you asked doesn't make sense");
return;
}
if (tasks.hasOwnProperty(to)) {
if (tasks[to].hasOwnProperty(arrText[1])) {
var ch_try = tasks[to][arrText[1]];
var ch_container = ch_try.base;
if (arrText.length != 2+ch_container.num_sol) {
bot.say(to, "What you say does not make sense");
return;
}
var test = arrText.splice(2);
var res = ch_try["ch"].res;
if (ch_container.solver(test, res)) {
bot.say(to, "Good job "+ nick + " you win!");
clearTimeout(ch_try.timeout);
cleanTasks(to, ch_container);
//reward.give_reward();
return;
}
else {
bot.say(to, "Stop embarassing yourself "+ nick);
return;
}
}
else {
bot.say(to, "Challenge not active");
return;
}
}
else {
bot.say(to, "Challenge not active");
return;
}
}
else if (arrText[0] == "@help") {
var st = "I am the awesomeBot\nI accept the following commands:";
st += "\nChallenges";
for (c in chall.challenges) {
st +="\n\t- "+ c +": "+chall.challenges[c]["description"];
}
st+="\nsay @sol <def> <solution> to give me a solution to a challenge";
st+="\nsay @help for display this message";
bot.say(to, st);
}
else if (arrText[0] == "@hex") {
var x = parseInt(arrText[1], 16);
if (isNaN(x))
x="Are you stupid?";
bot.say(to, x);
}
else if (arrText[0] == "@emdel" || arrText[0] == "@emd3l") {
bot.say(to, arrText[0]+ " gay");
}
//add an else if for doing something else
});