Made DBot load listeners in The New Way. Massively simplified Command module, converted to new format. Converted JS module to new format. All working. Moved ~ignore functionality away for now as it needs to be combined with jsbot.

This commit is contained in:
Luke Slater 2012-05-19 19:14:07 +01:00
parent ec34a861f4
commit 1f02e29a85
4 changed files with 64 additions and 114 deletions

View File

@ -1,122 +1,58 @@
// Module which handles the command execution syntax for DBot. Not much is going /**
// to work without this. * Module Name: Command
* Description: An essential module which maps PRIVMSG input to an appropriate
* command and then runs that command, given the user isn't banned from or
* ignoring that command.
*/
var command = function(dbot) { var command = function(dbot) {
var dbot = dbot; var dbot = dbot;
/**
* Is user banned from using command?
*/
var is_banned = function(user, command) {
var banned = false;
if(dbot.db.bans.hasOwnProperty(command)) {
if(dbot.db.bans[command].include(user) || dbot.db.bans['*'].include(user)) {
banned = true;
}
}
return banned;
}
/**
* Is user ignoring command?
*/
var is_ignoring = function(user, command) {
var module = dbot.commandMap[command];
var ignoring = false;
if(dbot.db.ignores.hasOwnProperty(user) && dbot.db.ignores[user].include(module)) {
ignoring = true;
}
return ignoring;
}
return { return {
'onLoad': function() { 'name': 'command',
return {
'~ignore': function(data, params) {
var ignorableModules = [];
for(var i=0;i<dbot.modules.length;i++) {
if(dbot.modules[i].ignorable != null && dbot.modules[i].ignorable == true) {
ignorableModules.push(dbot.modules[i].name);
}
}
if(params[1] == undefined) { /**
dbot.say(data.channel, * Run the appropriate command given the input.
dbot.t('ignore_usage', {'user': data.user, 'modules': ignorableModules.join(', ')})); */
} else { 'listener': function(event) {
if(dbot.moduleNames.include(params[1])) { var command_name = event.params[0];
if(!dbot.db.ignores.hasOwnProperty(data.user)) { if(dbot.commands.hasOwnProperty(command_name)) {
dbot.db.ignores[data.user] = []; if(is_banned(event.user, command_name)) {
} event.reply(dbot.t('command_ban', {'user': event.user}));
if(dbot.db.ignores[data.user].include(params[1])) {
dbot.say(data.channel, dbot.t('already_ignoring', {'user': data.user}));
} else {
dbot.db.ignores[data.user].push(params[1]);
dbot.say(data.channel, dbot.t('ignored', {'user': data.user, 'module': params[1]}));
}
} else {
dbot.say(data.channel, dbot.t('invalid_ignore', {'user': data.user}));
}
}
},
'~unignore': function(data, params) {
var ignoredModules = [];
if(dbot.db.ignores.hasOwnProperty(data.user)) {
ignoredModules = dbot.db.ignores[data.user];
}
if(params[1] == undefined) {
dbot.say(data.channel,
dbot.t('unignore_usage', {'user': data.user, 'modules': ignoredModules.join(', ')}));
} else {
if(ignoredModules.include(params[1]) == false) {
dbot.say(data.channel, dbot.t('invalid_unignore', {'user': data.user}));
} else {
dbot.db.ignores[data.user].splice(dbot.db.ignores[data.user].indexOf(params[1]), 1);
dbot.say(data.channel, dbot.t('unignored', {'user': data.user, 'module': params[1]}));
}
}
}
};
},
'listener': function(data) {
var params = data.message.split(' ');
if(data.channel == dbot.name) data.channel = data.user;
if(dbot.commands.hasOwnProperty(params[0])) {
if((dbot.db.bans.hasOwnProperty(params[0]) &&
dbot.db.bans[params[0]].include(data.user)) || dbot.db.bans['*'].include(data.user)) {
dbot.say(data.channel, dbot.t('command_ban', {'user': data.user}));
} else { } else {
var commandBelongsTo = dbot.commandMap[params[0]]; if(!is_ignoring(event.user, command_name)) {
if(dbot.db.ignores.hasOwnProperty(data.user) && dbot.commands[command_name](event);
dbot.db.ignores[data.user].include(commandBelongsTo)) {
// do nothing
} else {
dbot.commands[params[0]](data, params);
dbot.save(); dbot.save();
} }
} }
} else {
var q = data.message.valMatch(/^~([\d\w\s-]*)/, 2);
if(q) {
if(dbot.db.bans['*'].include(data.user)) {
dbot.say(data.channel, dbot.t('command_ban', {'user': data.user}));
} else {
q[1] = q[1].trim();
key = dbot.cleanNick(q[1])
if(dbot.db.quoteArrs.hasOwnProperty(key) && dbot.moduleNames.include('quotes') &&
(dbot.db.ignores.hasOwnProperty(data.user) &&
dbot.db.ignores[data.user].include('quotes')) == false) {
var params = ['~q'];
key.split(' ').each((function(word) {
this.push(word);
}).bind(params));
data.message = params.join(' ');
dbot.commands[params[0]](data, params);
dbot.save();
} else {
// See if it's similar to anything
var winnerDistance = Infinity;
var winner = false;
for(var commandName in dbot.commands) {
var distance = String.prototype.distance(params[0], commandName);
if(distance < winnerDistance) {
winner = commandName;
winnerDistance = distance;
}
}
if(winnerDistance < 3) {
dbot.say(data.channel, dbot.t('command_typo', {'command': winner}));
}
}
}
}
} }
}, },
'on': 'PRIVMSG', 'on': 'PRIVMSG',
'name': 'command',
'ignorable': false 'ignorable': false
}; };
}; };

View File

@ -6,19 +6,20 @@ var js = function(dbot) {
var s = new sbox(); var s = new sbox();
var commands = { var commands = {
'~js': function(data, params) { '~js': function(event) {
var q = data.message.valMatch(/^~js (.*)/, 2); console.log('hello');
var q = event.message.valMatch(/^~js (.*)/, 2);
s.run(q[1], function(output) { s.run(q[1], function(output) {
dbot.say(data.channel, output.result); event.reply(output.result);
}); }.bind(this));
}, },
'~ajs': function(data, params) { '~ajs': function(event) {
var q = data.message.valMatch(/^~ajs (.*)/, 2); var q = data.message.valMatch(/^~ajs (.*)/, 2);
if(dbot.admin.include(data.user) ) { if(dbot.admin.include(data.user) ) {
var ret = eval(q[1]); var ret = eval(q[1]);
if(ret != undefined) { if(ret != undefined) {
dbot.say(data.channel, ret); event.reply(ret);
} }
} }
} }

View File

@ -46,6 +46,19 @@ var quotes = function(dbot) {
}; };
var commands = { var commands = {
'~': function(data, params) {
var q = data.message.valMatch(/^~([\d\w\s-]*)/, 2);
if(q) {
q[1] = q[1].trim();
key = q[1].toLowerCase();
if(quotes.hasOwnProperty(key)) {
dbot.say(data.channel, q[1] + ': ' + interpolatedQuote(key));
} else {
dbot.say(data.channel, dbot.t('category_not_found', {'category': q[1]}));
}
}
},
'~q': function(data, params) { '~q': function(data, params) {
var q = data.message.valMatch(/^~q ([\d\w\s-]*)/, 2); var q = data.message.valMatch(/^~q ([\d\w\s-]*)/, 2);
if(q) { if(q) {

2
run.js
View File

@ -152,7 +152,7 @@ DBot.prototype.reloadModules = function() {
this.rawModules.push(rawModule); this.rawModules.push(rawModule);
if(module.listener) { if(module.listener) {
this.instance.addListener(module.on, module.listener); this.instance.addListener(module.on, module.name, module.listener);
} }
if(module.onLoad) { if(module.onLoad) {