Command map changed round the other way. Modules have name and ignorable properties. Ignore and unignore commands, which currently effect actual commands but not listeners yet.

This commit is contained in:
Luke Slater 2012-04-15 21:43:02 +01:00
parent b232103178
commit 0062dde196
14 changed files with 137 additions and 19 deletions

View File

@ -134,7 +134,11 @@ var adminCommands = function(dbot) {
} }
}, },
'on': 'PRIVMSG' 'on': 'PRIVMSG',
'name': 'admin',
'ignorable': false
}; };
}; };

View File

@ -27,7 +27,11 @@ var autoshorten = function(dbot) {
} }
}, },
'on': 'PRIVMSG' 'on': 'PRIVMSG',
'name': 'autoshorten',
'ignorable': true
}; };
} }

View File

@ -6,7 +6,69 @@ var command = function(dbot) {
return { return {
'onLoad': function() { 'onLoad': function() {
return { return {
'~ignore': null '~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);
}
}
var usageString = 'Usage: ~ignore [module]. Modules you can ignore are: ';
for(var i=0;i<ignorableModules.length;i++) {
usageString += ignorableModules[i] + ", ";
}
usageString = usageString.slice(0, -2) + '.';
if(params[1] == undefined) {
dbot.say(data.channel, data.user + ': ' + usageString);
} else {
if(dbot.moduleNames.include(params[1])) {
if(!dbot.db.ignores.hasOwnProperty(data.user)) {
dbot.db.ignores[data.user] = [];
}
if(dbot.db.ignores[data.user].include(params[1])) {
dbot.say(data.channel, data.user + ': You\'re already ignoring that module.');
} else {
dbot.db.ignores[data.user].push(params[1]);
dbot.say(data.channel, data.user + ': Now ignoring ' + params[1]);
}
} else {
dbot.say(data.channel, data.user + ': That isn\'t a valid module name. ' +
usageString);
}
}
},
'~unignore': function(data, params) {
var ignoredModules = [];
if(dbot.db.ignores.hasOwnProperty(data.user)) {
ignoredModules = dbot.db.ignores[data.user];
}
var usageString = 'Usage: ~unignore [module]. Modules you are currently ignoring: ';
if(ignoredModules.length == 0) {
usageString += 'None.';
} else {
for(var i=0;i<ignoredModules.length;i++) {
usageString += ignoredModules[i] + ", ";
}
usageString = usageString.slice(0, -2) + '.';
}
if(params[1] == undefined) {
dbot.say(data.channel, data.user + ': ' + usageString);
} else {
if(ignoredModules.include(params[1]) == false) {
dbot.say(data.channel, data.user +
': You\'re not ignoring that module or it doesn\'t exist. ' + usageString);
} else {
dbot.db.ignores[data.user].splice(dbot.db.ignores[data.user].indexOf(params[1]), 1);
dbot.say(data.channel, data.user + ': No longer ignoring ' + params[1]);
}
}
}
}; };
}, },
@ -19,10 +81,16 @@ var command = function(dbot) {
dbot.db.bans[params[0]].include(data.user)) || dbot.db.bans['*'].include(data.user)) { dbot.db.bans[params[0]].include(data.user)) || dbot.db.bans['*'].include(data.user)) {
dbot.say(data.channel, data.user + dbot.say(data.channel, data.user +
' is banned from using this command. Commence incineration.'); ' is banned from using this command. Commence incineration.');
} else {
var commandBelongsTo = dbot.commandMap[params[0]];
if(dbot.db.ignores.hasOwnProperty(data.user) &&
dbot.db.ignores[data.user].include(commandBelongsTo)) {
// do nothing
} else { } else {
dbot.commands[params[0]](data, params); dbot.commands[params[0]](data, params);
dbot.save(); dbot.save();
} }
}
} else { } else {
var q = data.message.valMatch(/^~([\d\w\s-]*)/, 2); var q = data.message.valMatch(/^~([\d\w\s-]*)/, 2);
if(q) { if(q) {
@ -61,7 +129,11 @@ var command = function(dbot) {
} }
}, },
'on': 'PRIVMSG' 'on': 'PRIVMSG',
'name': 'command',
'ignorable': false
}; };
}; };

View File

@ -88,7 +88,11 @@ var dice = function(dbot) {
return { return {
'onLoad': function() { 'onLoad': function() {
return commands; return commands;
} },
'name': 'dice',
'ignorable': true
}; };
} }

View File

@ -84,7 +84,11 @@ var drama = function(dbot) {
} }
}, },
'on': 'PRIVMSG' 'on': 'PRIVMSG',
'name': 'drama',
'ignorable': false
}; };
} }

View File

@ -27,7 +27,11 @@ var js = function(dbot) {
return { return {
'onLoad': function() { 'onLoad': function() {
return commands; return commands;
} },
'name': 'js',
'ignorable': true
}; };
}; };

View File

@ -86,8 +86,11 @@ var kick = function(dbot) {
'onLoad': function() { 'onLoad': function() {
return commands; return commands;
} },
'name': 'kick',
'ignorable': false
}; };
}; };

View File

@ -10,7 +10,11 @@ var modehate = function(dbot) {
} }
}, },
'on': 'MODE' 'on': 'MODE',
'name': 'modehate',
'ignorable': false
}; };
}; };

View File

@ -12,7 +12,11 @@ var puns = function(dbot) {
} }
}, },
'on': 'JOIN' 'on': 'JOIN',
'name': 'puns',
'ignorable': true
}; };
} }

View File

@ -299,7 +299,11 @@ var quotes = function(dbot) {
} }
}, },
'on': 'PRIVMSG' 'on': 'PRIVMSG',
'name': 'quotes',
'ignorable': true
}; };
}; };

View File

@ -58,7 +58,11 @@ var spelling = function(dbot) {
} }
}, },
'on': 'PRIVMSG' 'on': 'PRIVMSG',
'name': 'spelling',
'ignorable': true
} }
} }

View File

@ -42,7 +42,11 @@ var webInterface = function(dbot) {
return { return {
'onDestroy': function() { 'onDestroy': function() {
app.close(); app.close();
} },
'name': 'web',
'ignorable': false
}; };
}; };

View File

@ -8,7 +8,11 @@ var youAre = function(dbot) {
} }
}, },
'on': 'PRIVMSG' 'on': 'PRIVMSG',
'name': 'youare',
'ignorable': false
}; };
}; };

5
run.js
View File

@ -40,7 +40,7 @@ var DBot = function(timers) {
this.db.locks = []; this.db.locks = [];
} }
if(!this.db.hasOwnProperty("ignores")) { if(!this.db.hasOwnProperty("ignores")) {
this.db.locks = {}; this.db.ignores = {};
} }
// Load the strings file // Load the strings file
@ -125,7 +125,6 @@ DBot.prototype.reloadModules = function() {
var rawModule = require('./modules/' + name); var rawModule = require('./modules/' + name);
var module = rawModule.fetch(this); var module = rawModule.fetch(this);
this.rawModules.push(rawModule); this.rawModules.push(rawModule);
this.commandMap[name] = [];
if(module.listener) { if(module.listener) {
this.instance.addListener(module.on, module.listener); this.instance.addListener(module.on, module.listener);
@ -136,7 +135,7 @@ DBot.prototype.reloadModules = function() {
for(key in newCommands) { for(key in newCommands) {
if(newCommands.hasOwnProperty(key) && Object.prototype.isFunction(newCommands[key])) { if(newCommands.hasOwnProperty(key) && Object.prototype.isFunction(newCommands[key])) {
this.commands[key] = newCommands[key]; this.commands[key] = newCommands[key];
this.commandMap[name].push(key); this.commandMap[key] = name;
} }
} }
} }