Quotes moved to a seperate file. Made a depressionbot module.

This commit is contained in:
Luke Slater 2011-08-22 13:20:06 +01:00
parent 9d794d0656
commit cbc526f21b
2 changed files with 210 additions and 199 deletions

47
quotes.js Normal file
View File

@ -0,0 +1,47 @@
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] + '\' (' + quotes[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();
}
};
};
exports.fetch = function() {
return quotes;
};

78
run.js
View File

@ -1,5 +1,6 @@
var fs = require('fs');
var jsbot = require('./jsbot');
var quote = require('./quotes');
///////////////////////////
@ -9,6 +10,17 @@ Array.prototype.random = function() {
///////////////////////////
///////////////////////////
var dbot = function(quotes) {
var admin = 'reality';
var waitingForKarma = false;
var name = 'depressionbot';
var db = JSON.parse(fs.readFileSync('db.json', 'utf-8'));
var quotes = quotes(db.quoteArrs);
var adminCommands = {
'join': function(data, params) {
instance.join(params[1]);
@ -96,61 +108,12 @@ var userCommands = {
}
};
///////////////////////////
var admin = 'reality';
var waitingForKarma = false;
var name = 'depressionbot';
var db = JSON.parse(fs.readFileSync('db.json', 'utf-8'));
var instance = jsbot.createJSBot(name, 'elara.ivixor.net', 6667, function() {
instance.join('#realitest');
instance.join('#42');
instance.join('#itonlygetsworse');
}.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) {
if(data.user == 'Lamp') {
instance.say(data.channel, db.quoteArrs.lamp.random());
@ -206,15 +169,11 @@ instance.addListener('PRIVMSG', function(data) {
if(userCommands[params[0]] != undefined) {
userCommands[params[0]](data, params);
} else {
var q = data.message.match(/~([\d\w\s]*)/)
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());
instance.say(data.channel, quotes.get(q[1].trim()));
}
}
}
}
});
@ -228,3 +187,8 @@ instance.addListener('PRIVMSG', function(data) {
}
}
});
return this;
}(quote.fetch());