3
0
mirror of https://github.com/reality/dbot.git synced 2024-11-23 20:39:25 +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 = dbot.db.quoteArrs;
var commands = {
'~q': function(data, params) {
var q = data.message.valMatch(/^~q ([\d\w\s]*)/, 2)
if(q) {
key = q[1].trim().toLowerCase();
if(quotes.hasOwnProperty(key)) {
dbot.say(data.channel, key + ': ' + quotes[key].random());
} else {
dbot.say(data.channel, 'No quotes under ' + key);
}
}
},
'~qcount': function(data, params) {
var q = data.message.valMatch(/^~qcount ([\d\w\s]*)/, 2);
if(q) {
key = key[1].trim().toLowerCase();
if(quotes.hasOwnProperty(key)) {
dbot.say(data.channel, key + ' has ' + quotes[key].length + ' quotes.');
} else {
dbot.say(data.channel, 'No quotes under ' + key);
}
}
},
'~qadd': function(data, params) {
var q = data.message.valMatch(/^~qadd ([\d\w\s]*)=(.+)$/, 3);
if(q) {
q[1] = q[1].toLowerCase();
if(!Object.isArray(quotes[q[1]])) {
quotes[q[1]] = [];
}
quotes[q[1]].push(q[2]);
dbot.say(data.channel, 'Quote saved in \'' + q[1] + '\' (' + quotes[q[1]].length + ')');
}
},
'~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();
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 {
get: function(key) {
key = key.toLowerCase();
if(quotes.hasOwnProperty(key)) {
return key + ': ' + quotes[key].random();
} else {
return 'No quotes under ' + key;
}
},
count: function(key) {
key = key.toLowerCase();
if(quotes.hasOwnProperty(key)) {
return key + ' has ' + quotes[key].length + ' quotes.';
} else {
return 'No quotes under ' + key;
}
},
add: function(key) {
key[1] = key[1].toLowerCase();
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) {
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() {
var rQuote = Object.keys(quotes).random();
return rQuote + ': ' + quotes[rQuote].random();
'onLoad': function() {
return commands;
}
};
};

View File

@ -7,50 +7,6 @@ var userCommands = function(dbot) {
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) {
if(!dbot.db.kicks.hasOwnProperty(params[1])) {
dbot.say(data.channel, params[1] + ' has either never been kicked or does not exist.');
@ -61,22 +17,9 @@ var userCommands = function(dbot) {
};
return {
'listener': function(data) {
params = data.message.split(' ');
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'
'onLoad': function() {
return commands;
}
};
};

41
run.js
View File

@ -2,20 +2,17 @@ require('./snippets');
var fs = require('fs');
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) {
this.admin = 'reality';
this.waitingForKarma = false;
this.name = 'depressionbot';
this.commands = {};
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.join('#realitest');
this.instance.join('#42');
this.instance.join('#fail');
this.instance.join('#itonlygetsworse');
}.bind(this));
this.moduleNames = dModules;
@ -34,8 +31,8 @@ DBot.prototype.save = function() {
DBot.prototype.reloadModules = function() {
this.rawModules = [];
this.modules = [];
this.commands = {};
// Reload snippets
var path = require.resolve('./snippets');
require.cache[path] = undefined;
require('./snippets');
@ -54,9 +51,37 @@ DBot.prototype.reloadModules = function() {
this.modules = this.rawModules.collect(function(rawModule) {
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;
}.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);