Sweet sweet closure

This commit is contained in:
Luke Slater 2011-08-21 15:55:36 +01:00
parent f6ad566d4d
commit 9d794d0656

104
run.js
View File

@ -35,14 +35,6 @@ var adminCommands = {
var c = params[1]; var c = params[1];
var m = params.slice(2).join(' '); var m = params.slice(2).join(' ');
instance.say(c, m); instance.say(c, m);
},
'add': function(data, params) {
var c = params[1];
var m = params.slice(2).join(' ');
db[c].push(m);
fs.writeFile('db.json', JSON.stringify(db, null, ' '));
instance.say(admin, 'Added.');
} }
}; };
@ -55,39 +47,32 @@ var userCommands = {
'~q': function(data, params) { '~q': function(data, params) {
var q = data.message.match(/~q ([\d\w\s]*)/) var q = data.message.match(/~q ([\d\w\s]*)/)
if(q != undefined) { if(q != undefined) {
q = q[1].trim(); instance.say(data.channel, quotes.get(q[1].trim()));
if(db.quoteArrs[q] != undefined) {
instance.say(data.channel, q + ': ' + db.quoteArrs[q].random());
}
} }
}, },
'~qadd': function(data, params) { '~qadd': function(data, params) {
var qadd = data.message.match(/~qadd ([\d\w\s]*)=(.+)$/); var q = data.message.match(/~qadd ([\d\w\s]*)=(.+)$/);
if(qadd != null && qadd.length >= 3) { if(q != null && q.length >= 3) {
if(Object.isArray(db.quoteArrs[qadd[1]])) { instance.say(data.channel, quotes.add(q));
db.quoteArrs[qadd[1]].push(qadd[2]);
} else {
db.quoteArrs[qadd[1]] = [qadd[2]];
}
instance.say(data.channel, 'Quote saved in \'' + qadd[1] + '\' (' + db.quoteArrs[qadd[1]].length + ')');
fs.writeFile('db.json', JSON.stringify(db, null, ' ')); fs.writeFile('db.json', JSON.stringify(db, null, ' '));
} else { } else {
instance.say(data.channel, 'Burn the invalid syntax!'); instance.say(data.channel, 'Burn the invalid syntax!');
} }
}, },
'~qcount': function(data, params) { '~qset': function(data, params) {
var qcount = data.message.match(/~qcount ([\d\w\s]*)/)[1].trim(); var q = data.message.match(/~qset ([\d\w\s]*)=(.+)$/);
if(db.quoteArrs[qcount] != undefined) { if(q != undefined && q.length >= 3) {
instance.say(data.channel, qcount + ' has ' + db.quoteArrs[qcount].length + ' quotes.'); instance.say(data.channel, quotes.set(q));
} else {
instance.say(data.channel, qcount + ' doesn\'t exist.');
} }
}, },
'~lamp': function(data, params) { '~qcount': function(data, params) {
instance.say(data.channel, db.quoteArrs.lamp.random()); var q = data.message.match(/~qcount ([\d\w\s]*)/)[1].trim();
if(q != undefined) {
instance.say(data.channel, quotes.count(q));
}
}, },
'~reality': function(data, params) { '~reality': function(data, params) {
@ -99,8 +84,7 @@ var userCommands = {
}, },
'~rq': function(data, params) { '~rq': function(data, params) {
var rQuote = Object.keys(db.quoteArrs).random(); instance.say(data.channel, quotes.random());
instance.say(data.channel, rQuote + ': ' + db.quoteArrs[rQuote].random());
}, },
'~kickcount': function(data, params) { '~kickcount': function(data, params) {
@ -120,9 +104,53 @@ var name = 'depressionbot';
var db = JSON.parse(fs.readFileSync('db.json', 'utf-8')); var db = JSON.parse(fs.readFileSync('db.json', 'utf-8'));
var instance = jsbot.createJSBot(name, 'elara.ivixor.net', 6667, function() { var instance = jsbot.createJSBot(name, 'elara.ivixor.net', 6667, function() {
instance.join('#itonlygetsworse'); instance.join('#realitest');
}.bind(this)); }.bind(this));
var quotes = function(quotes) {
var qArrs = quotes;
return {
get: function(key) {
if(quotes.hasOwnProperty(key)) {
return key + ': ' + qArrs[key].random();
} else {
return 'No quotes under ' + key;
}
},
count: function(key) {
if(quotes.hasOwnProperty(key)) {
return key + ' has ' + quotes[key].length + ' quotes.';
} else {
return 'No quotes under ' + key;
}
},
add: function(key) {
if(!Object.isArray(quotes[key[1]])) {
quotes[key[1]] = [];
}
quotes[key[1]].push(key[2]);
return 'Quote saved in \'' + key[1] + '\' (' + db.quoteArrs[key[1]].length + ')';
},
set: function(key) {
if(!quotes.hasOwnProperty(key[1]) || (quotes.hasOwnProperty(key[1]) && quotes[key[1]].length == 1)) {
quotes[key[1]] = [key[2]];
return 'Quote saved as ' + key[1];
} else {
return 'No replacing arrays, you whore.';
}
},
random: function() {
var rQuote = Object.keys(quotes).random();
return rQuote + ': ' + quotes[rQuote].random();
}
};
}(db.quoteArrs);
instance.addListener('JOIN', function(data) { instance.addListener('JOIN', function(data) {
if(data.user == 'Lamp') { if(data.user == 'Lamp') {
instance.say(data.channel, db.quoteArrs.lamp.random()); instance.say(data.channel, db.quoteArrs.lamp.random());
@ -172,9 +200,21 @@ instance.addListener('PRIVMSG', function(data) {
params = data.message.split(' '); params = data.message.split(' ');
if(data.user == admin && data.channel == name && adminCommands[params[0]] != undefined) { if(data.user == admin && data.channel == name && adminCommands[params[0]] != undefined) {
adminCommands[params[0]](data, params); adminCommands[params[0]](data, params);
} else if(userCommands[params[0]] != undefined) { } else {
if(data.channel == name) data.channel = data.user; if(data.channel == name) data.channel = data.user;
if(userCommands[params[0]] != undefined) {
userCommands[params[0]](data, params); userCommands[params[0]](data, params);
} else {
var q = data.message.match(/~([\d\w\s]*)/)
if(q != undefined) {
q = q[1].trim();
if(db.quoteArrs[q] != undefined) {
instance.say(data.channel, q + ': ' + db.quoteArrs[q].random());
}
}
}
} }
}); });