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;
};

114
run.js
View File

@ -1,5 +1,6 @@
var fs = require('fs');
var jsbot = require('./jsbot');
var quote = require('./quotes');
///////////////////////////
@ -9,7 +10,18 @@ Array.prototype.random = function() {
///////////////////////////
var adminCommands = {
///////////////////////////
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]);
instance.say(admin, 'Joined ' + params[1]);
@ -36,9 +48,9 @@ var adminCommands = {
var m = params.slice(2).join(' ');
instance.say(c, m);
}
};
};
var userCommands = {
var userCommands = {
'~kc': function(data, params) {
instance.say('aisbot', '.karma ' + data.message.split(' ')[1]);
waitingForKarma = data.channel;
@ -94,64 +106,15 @@ var userCommands = {
instance.say(data.channel, params[1] + ' has been kicked ' + db.kicks[params[1]] + ' times.');
}
}
};
///////////////////////////
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');
}.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) {
var instance = jsbot.createJSBot(name, 'elara.ivixor.net', 6667, function() {
instance.join('#realitest');
instance.join('#42');
instance.join('#itonlygetsworse');
}.bind(this));
instance.addListener('JOIN', function(data) {
if(data.user == 'Lamp') {
instance.say(data.channel, db.quoteArrs.lamp.random());
} else if(data.user == 'reality') {
@ -160,9 +123,9 @@ instance.addListener('JOIN', function(data) {
instance.say('aisbot', '.karma ' + data.user);
waitingForKarma = data.channel;
}
});
});
instance.addListener('KICK', function(data) {
instance.addListener('KICK', function(data) {
if(data.kickee == name) {
instance.join(data.channel);
instance.say(data.channel, 'Thou shalt not kick ' + name);
@ -176,9 +139,9 @@ instance.addListener('KICK', function(data) {
instance.say(data.channel, data.kickee + '-- (' + data.kickee + ' has been kicked ' + db.kicks[data.kickee] + ' times)');
}
fs.writeFile('db.json', JSON.stringify(db, null, ' '));
});
});
instance.addListener('PRIVMSG', function(data) {
instance.addListener('PRIVMSG', function(data) {
if(data.user == 'aisbot' && data.channel == name && waitingForKarma != false && data.message.match(/is at/)) {
var split = data.message.split(' ');
var target = split[0];
@ -194,9 +157,9 @@ instance.addListener('PRIVMSG', function(data) {
waitingForKarma = false;
}
});
});
instance.addListener('PRIVMSG', function(data) {
instance.addListener('PRIVMSG', function(data) {
params = data.message.split(' ');
if(data.user == admin && data.channel == name && adminCommands[params[0]] != undefined) {
adminCommands[params[0]](data, params);
@ -206,19 +169,15 @@ 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()));
}
}
}
});
}
}
});
instance.addListener('PRIVMSG', function(data) {
instance.addListener('PRIVMSG', function(data) {
if(data.user == 'reality') {
var once = data.message.match(/I ([\d\w\s]* once.)/);
if(once != null) {
@ -227,4 +186,9 @@ instance.addListener('PRIVMSG', function(data) {
fs.writeFile('db.json', JSON.stringify(db, null, ' '));
}
}
});
});
return this;
}(quote.fetch());