3
0
mirror of https://github.com/reality/dbot.git synced 2024-11-27 22:39:26 +01:00

Central command management. Quotes now handles its own commands.

This commit is contained in:
Luke Slater 2011-08-28 15:00:22 +01:00
parent 86a428a5a2
commit 6b0e2de264
3 changed files with 104 additions and 108 deletions

View File

@ -1,47 +1,75 @@
var quotes = function(dbot) { var quotes = function(dbot) {
var quotes = dbot.db.quoteArrs; var quotes = dbot.db.quoteArrs;
return { var commands = {
get: function(key) { '~q': function(data, params) {
key = key.toLowerCase(); var q = data.message.valMatch(/^~q ([\d\w\s]*)/, 2)
if(quotes.hasOwnProperty(key)) { if(q) {
return key + ': ' + quotes[key].random(); key = q[1].trim().toLowerCase();
} else { if(quotes.hasOwnProperty(key)) {
return 'No quotes under ' + key; dbot.say(data.channel, key + ': ' + quotes[key].random());
} else {
dbot.say(data.channel, 'No quotes under ' + key);
}
} }
}, },
count: function(key) { '~qcount': function(data, params) {
key = key.toLowerCase(); var q = data.message.valMatch(/^~qcount ([\d\w\s]*)/, 2);
if(quotes.hasOwnProperty(key)) { if(q) {
return key + ' has ' + quotes[key].length + ' quotes.'; key = key[1].trim().toLowerCase();
} else { if(quotes.hasOwnProperty(key)) {
return 'No quotes under ' + key; dbot.say(data.channel, key + ' has ' + quotes[key].length + ' quotes.');
} else {
dbot.say(data.channel, 'No quotes under ' + key);
}
} }
}, },
add: function(key) { '~qadd': function(data, params) {
key[1] = key[1].toLowerCase(); var q = data.message.valMatch(/^~qadd ([\d\w\s]*)=(.+)$/, 3);
if(!Object.isArray(quotes[key[1]])) { if(q) {
quotes[key[1]] = []; q[1] = q[1].toLowerCase();
} if(!Object.isArray(quotes[q[1]])) {
quotes[key[1]].push(key[2]); quotes[q[1]] = [];
return 'Quote saved in \'' + key[1] + '\' (' + quotes[key[1]].length + ')'; }
}, quotes[q[1]].push(q[2]);
dbot.say(data.channel, 'Quote saved in \'' + q[1] + '\' (' + quotes[q[1]].length + ')');
set: function(key) {
key[1] = key[1].toLowerCase();
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() { '~qset': function(data, params) {
var q = data.message.valMatch(/^~qset ([\d\w\s]*)=(.+)$/, 3);
if(q) {
q[1] = q[1].toLowerCase();
if(!quotes.hasOwnProperty(q[1]) || (quotes.hasOwnProperty(q[1]) &&
quotes[q[1]].length == 1)) {
quotes[q[1]] = [q[2]];
dbot.say(data.channel, 'Quote saved as ' + q[1]);
} else {
dbot.say(data.channel, 'No replacing arrays, you whore.');
}
}
},
'~rq': function(data, params) {
var rQuote = Object.keys(quotes).random(); var rQuote = Object.keys(quotes).random();
return rQuote + ': ' + quotes[rQuote].random(); dbot.say(data.channel, rQuote + ': ' + quotes[rQuote].random());
},
'~reality': function(data, params) {
dbot.say(data.channel, dbot.db.realiPuns.random());
},
'~d': function(data, params) {
dbot.say(data.channel, data.user + ': ' + dbot.db.quoteArrs['depressionbot'].random());
},
};
return {
'onLoad': function() {
return commands;
} }
}; };
}; };

View File

@ -7,50 +7,6 @@ var userCommands = function(dbot) {
dbot.waitingForKarma = data.channel; dbot.waitingForKarma = data.channel;
}, },
'~q': function(data, params) {
var q = data.message.valMatch(/^~q ([\d\w\s]*)/, 2)
if(q) {
dbot.say(data.channel, dbot.quotes.get(q[1].trim()));
}
},
'~qadd': function(data, params) {
var q = data.message.valMatch(/^~qadd ([\d\w\s]*)=(.+)$/, 3);
if(q) {
dbot.say(data.channel, dbot.quotes.add(q));
} else {
dbot.say(data.channel, 'Burn the invalid syntax!');
}
},
'~qset': function(data, params) {
var q = data.message.valMatch(/^~qset ([\d\w\s]*)=(.+)$/, 3);
if(q) {
dbot.say(data.channel, dbot.quotes.set(q));
} else {
dbot.say(data.channel, 'Burn the invalid syntax!');
}
},
'~qcount': function(data, params) {
var q = data.message.valMatch(/^~qcount ([\d\w\s]*)/, 2);
if(q) {
dbot.say(data.channel, dbot.quotes.count(q[1].trim()));
}
},
'~reality': function(data, params) {
dbot.say(data.channel, dbot.db.realiPuns.random());
},
'~d': function(data, params) {
dbot.say(data.channel, data.user + ': ' + dbot.db.quoteArrs['depressionbot'].random());
},
'~rq': function(data, params) {
dbot.say(data.channel, dbot.quotes.random());
},
'~kickcount': function(data, params) { '~kickcount': function(data, params) {
if(!dbot.db.kicks.hasOwnProperty(params[1])) { if(!dbot.db.kicks.hasOwnProperty(params[1])) {
dbot.say(data.channel, params[1] + ' has either never been kicked or does not exist.'); dbot.say(data.channel, params[1] + ' has either never been kicked or does not exist.');
@ -61,22 +17,9 @@ var userCommands = function(dbot) {
}; };
return { return {
'listener': function(data) { 'onLoad': function() {
params = data.message.split(' '); return commands;
if(data.channel == dbot.name) data.channel = data.user; }
if(commands.hasOwnProperty(params[0])) {
commands[params[0]](data, params);
dbot.save();
} else {
var q = data.message.valMatch(/^~([\d\w\s]*)/, 2)
if(q) {
dbot.say(data.channel, dbot.quotes.get(q[1].trim()));
}
}
},
'on': 'PRIVMSG'
}; };
}; };

41
run.js
View File

@ -2,20 +2,17 @@ require('./snippets');
var fs = require('fs'); var fs = require('fs');
var jsbot = require('./jsbot'); var jsbot = require('./jsbot');
var modules = ['user', 'admin', 'puns', 'kick', 'reality', 'karma', 'youare']; var modules = ['user', 'admin', 'puns', 'kick', 'reality', 'karma', 'youare', 'quotes'];
var DBot = function(dModules, quotes) { var DBot = function(dModules, quotes) {
this.admin = 'reality'; this.admin = 'reality';
this.waitingForKarma = false; this.waitingForKarma = false;
this.name = 'depressionbot'; this.name = 'depressionbot';
this.commands = {};
this.db = JSON.parse(fs.readFileSync('db.json', 'utf-8')); this.db = JSON.parse(fs.readFileSync('db.json', 'utf-8'));
this.quotes = require(quotes).fetch(this);
this.instance = jsbot.createJSBot(this.name, 'elara.ivixor.net', 6667, this, function() { this.instance = jsbot.createJSBot(this.name, 'elara.ivixor.net', 6667, this, function() {
this.instance.join('#realitest'); this.instance.join('#realitest');
this.instance.join('#42');
this.instance.join('#fail');
this.instance.join('#itonlygetsworse');
}.bind(this)); }.bind(this));
this.moduleNames = dModules; this.moduleNames = dModules;
@ -34,8 +31,8 @@ DBot.prototype.save = function() {
DBot.prototype.reloadModules = function() { DBot.prototype.reloadModules = function() {
this.rawModules = []; this.rawModules = [];
this.modules = []; this.modules = [];
this.commands = {};
// Reload snippets
var path = require.resolve('./snippets'); var path = require.resolve('./snippets');
require.cache[path] = undefined; require.cache[path] = undefined;
require('./snippets'); require('./snippets');
@ -54,9 +51,37 @@ DBot.prototype.reloadModules = function() {
this.modules = this.rawModules.collect(function(rawModule) { this.modules = this.rawModules.collect(function(rawModule) {
var module = rawModule.fetch(this); var module = rawModule.fetch(this);
this.instance.addListener(module.on, module.listener);
if(module.listener) {
this.instance.addListener(module.on, module.listener);
}
if(module.onLoad) {
var newCommands = module.onLoad();
for(key in newCommands) {
if(newCommands.hasOwnProperty(key) && Object.prototype.isFunction(newCommands[key])) {
this.commands[key] = newCommands[key];
}
}
}
return module; return module;
}.bind(this)); }.bind(this));
this.instance.addListener('PRIVMSG', function(data) {
params = data.message.split(' ');
if(data.channel == this.name) data.channel = data.user;
if(this.commands.hasOwnProperty(params[0])) {
this.commands[params[0]](data, params);
this.save();
} else {
var q = data.message.valMatch(/^~([\d\w\s]*)/, 2)
if(q) {
this.say(data.channel, this.quotes.get(q[1].trim()));
}
}
}.bind(this));
}; };
new DBot(modules, './modules/quotes'); new DBot(modules);