Converted admin.js. Added a few more translations. Removed modehate functionality. Changed some stuff in run.js to facilitate changes.

This commit is contained in:
Luke Slater 2012-05-23 19:38:10 +01:00
parent aa72af3867
commit 464e0f11e3
4 changed files with 91 additions and 119 deletions

View File

@ -1,147 +1,136 @@
/**
* Module Name: Admin
* Description: Set of commands which only one who is a DepressionBot
* administrator can run - as such, it has its own command execution listener.
*/
var fs = require('fs'); var fs = require('fs');
var sys = require('sys') var sys = require('sys')
var exec = require('child_process').exec; var exec = require('child_process').exec;
var adminCommands = function(dbot) { var admin = function(dbot) {
var dbot = dbot;
var commands = { var commands = {
'join': function(data, params) { // Join a channel
dbot.instance.join(params[1]); 'join': function(event) {
dbot.say(data.channel, 'Joined ' + params[1]); var channel = event.params[1];
dbot.instance.join(event, channel);
event.reply(dbot.t('join', {'channel': channel}));
}, },
'opme': function(data, params) { // Leave a channel
dbot.instance.send('MODE ' + params[1] + ' +o ', data.user); 'part': function(event) {
var channel = event.params[1];
event.instance.part(event, channel);
event.reply(dbot.t('part', {'channel': channel}));
}, },
'part': function(data, params) { // Op admin caller in given channel
dbot.instance.part(params[1]); 'opme': function(event) {
event.channel = event.params[1];
dbot.instance.mode(event, '+o ' + event.user);
}, },
// Do a git pull and reload // Do a git pull and reload
'greload': function(data, params) { 'greload': function(event) {
var child; var child = exec("git pull", function (error, stdout, stderr) {
event.reply(dbot.t('gpull'));
child = exec("git pull", function (error, stdout, stderr) { commands.reload(event);
console.log(stderr);
dbot.say(data.channel, dbot.t('gpull'));
commands.reload(data, params);
}.bind(this)); }.bind(this));
}, },
'reload': function(data, params) { // Reload DB, translations and modules.
'reload': function(event) {
dbot.db = JSON.parse(fs.readFileSync('db.json', 'utf-8')); dbot.db = JSON.parse(fs.readFileSync('db.json', 'utf-8'));
dbot.strings = JSON.parse(fs.readFileSync('strings.json', 'utf-8')); dbot.strings = JSON.parse(fs.readFileSync('strings.json', 'utf-8'));
dbot.reloadModules(); dbot.reloadModules();
dbot.say(data.channel, dbot.t('reload')); event.reply(dbot.t('reload'));
}, },
'say': function(data, params) { // Say something in a channel (TODO probably doesn't work.)
if (params[1] === "@") { 'say': function(event) {
var c = data.channel; var channel = event.params[1];
} else { if(event.params[1] === "@") {
var c = params[1]; var channel = event.channel;
} }
var m = params.slice(2).join(' '); var message = event.params.slice(2).join(' ');
dbot.say(c, m); dbot.say(event.server, channel, message);
}, },
'act': function(data, params) { // Load new module
if (params[1] === "@") { 'load': function(event) {
var c = data.channel; var moduleName = event.params[1];
} else { dbot.moduleNames.push(moduleName);
var c = params[1];
}
var m = params.slice(2).join(' ');
dbot.act(c, m);
},
'load': function(data, params) {
dbot.moduleNames.push(params[1]);
dbot.reloadModules(); dbot.reloadModules();
dbot.say(data.channel, dbot.t('load_module', {'moduleName': params[1]})); event.reply(dbot.t('load_module', {'moduleName': moduleName}));
}, },
'unload': function(data, params) { // Unload a loaded module
if(dbot.moduleNames.include(params[1])) { 'unload': function(event) {
var cacheKey = require.resolve('../modules/' + params[1]); var moduleName = event.params[1];
if(dbot.moduleNames.include(moduleName)) {
var cacheKey = require.resolve('../modules/' + moduleName);
delete require.cache[cacheKey]; delete require.cache[cacheKey];
var moduleIndex = dbot.moduleNames.indexOf(params[1]); var moduleIndex = dbot.moduleNames.indexOf(moduleName);
dbot.moduleNames.splice(moduleIndex, 1); dbot.moduleNames.splice(moduleIndex, 1);
dbot.reloadModules(); dbot.reloadModules();
dbot.say(data.channel, dbot.t('unload_module', {'moduleName': params[1]}));
event.reply(dbot.t('unload_module', {'moduleName': moduleName}));
} else { } else {
dbot.say(data.channel, dbot.t('unload_error', {'moduleName': params[1]})); event.reply(dbot.t('unload_error', {'moduleName': moduleName}));
} }
}, },
'ban': function(data, params) { // Ban user from command or *
if(dbot.db.bans.hasOwnProperty(params[2])) { 'ban': function(event) {
dbot.db.bans[params[2]].push(params[1]); var username = event.params[1];
} else { var command = event.params[2];
dbot.db.bans[params[2]] = [ params[1] ];
if(!dbot.db.bans.hasOwnProperty(command)) {
dbot.db.bans[command] = [ ];
} }
dbot.say(data.channel, dbot.t('banned', {'user': params[1], 'command': params[2]})); dbot.db.bans[command].push(username);
event.reply(dbot.t('banned', {'user': username, 'command': command}));
}, },
'unban': function(data, params) { // Unban a user from command or *
if(dbot.db.bans.hasOwnProperty(params[2]) && dbot.db.bans[params[2]].include(params[1])) { 'unban': function(event) {
dbot.db.bans[params[2]].splice(dbot.db.bans[params[2]].indexOf(params[1]), 1); var username = event.params[1];
dbot.say(data.channel, dbot.t('unbanned', {'user': params[1], 'command': params[2]})); var command = event.params[2];
if(dbot.db.bans.hasOwnProperty(command) && dbot.db.bans[command].include(username)) {
dbot.db.bans[command].splice(dbot.db.bans[command].indexOf(username), 1);
event.reply(dbot.t('unbanned', {'user': username, 'command': command}));
} else { } else {
dbot.say(data.channel, dbot.t('unban_error', {'user': params[1]})); event.reply(dbot.t('unban_error', {'user': username}));
} }
}, },
'modehate': function(data, params) { // Lock quote category so quotes can't be removed
dbot.db.modehate.push(params[1]); 'lock': function(event) {
dbot.say(data.channel, dbot.t('modehate', {'user': params[1]})); var category = event.params[1];
}, dbot.db.locks.push(category);
event.reply(dbot.t('qlock', {'category': category}));
'unmodehate': function(data, params) {
dbot.db.modehate.splice(dbot.db.modehate.indexOf(params[1]), 1);
dbot.say(data.channel, dbot.t('unmodehate', {'user': params[1]}));
},
'lock': function(data, params) {
dbot.db.locks.push(params[1]);
dbot.say(data.channel, dbot.t('qlock', {'category': params[1]}));
} }
}; };
return { return {
// These commands are implemented as their own listener so it can easily 'name': 'admin',
// check whether the user is the admin, and so that the admin can't ban
// themselves and then not be able to rectify it...
'listener': function(data) {
if(data.channel == dbot.name) data.channel = data.user;
params = data.message.split(' '); /**
if(commands.hasOwnProperty(params[0]) && dbot.admin.include(data.user)) { * Run the appropriate admin command given the input (and user).
commands[params[0]](data, params); */
'listener': function(event) {
var commandName = event.params[0];
if(commands.hasOwnProperty(commandName) && dbot.admin.include(event.user)) {
commands[commandName](event);
dbot.save(); dbot.save();
} }
}, },
'onLoad': function() {
return {
'~resetadmin': function(data, params) {
dbot.admin = dbot.config.admin;
}
}
},
'on': 'PRIVMSG', 'on': 'PRIVMSG',
'name': 'admin',
'ignorable': false 'ignorable': false
}; };
}; };
exports.fetch = function(dbot) { exports.fetch = function(dbot) {
return adminCommands(dbot); return admin(dbot);
}; };

View File

@ -1,23 +0,0 @@
var modehate = function(dbot) {
var dbot = dbot;
return {
'listener': function(data, params) {
if(data.raw[0].indexOf('-oooo') != -1) {
dbot.instance.send('KICK #42 ' + data.user + ' :gtfo - mass deop protection');
} else if(dbot.db.modehate.include(data.user) && data.raw[0].indexOf('-o') != -1) {
dbot.instance.send('KICK ' + data.channel + ' ' + data.user + ' :gtfo');
}
},
'on': 'MODE',
'name': 'modehate',
'ignorable': false
};
};
exports.fetch = function(dbot) {
return modehate(dbot);
};

10
run.js
View File

@ -53,7 +53,7 @@ var DBot = function(timers) {
// Populate bot properties with config data // Populate bot properties with config data
this.name = this.config.name || 'dbox'; this.name = this.config.name || 'dbox';
this.admin = this.config.admin || [ 'reality' ]; this.admin = this.config.admin || [ 'reality' ];
this.moduleNames = this.config.modules || [ 'command', 'js', 'quotes' ]; this.moduleNames = this.config.modules || [ 'command', 'js', 'quotes', 'admin' ];
this.language = this.config.language || 'english'; this.language = this.config.language || 'english';
// It's the user's responsibility to fill this data structure up properly in // It's the user's responsibility to fill this data structure up properly in
@ -89,8 +89,8 @@ var DBot = function(timers) {
}; };
// Say something in a channel // Say something in a channel
DBot.prototype.say = function(channel, data) { DBot.prototype.say = function(server, channel, message) {
this.instance.say(channel, data); this.instance.say(server, channel, message);
}; };
// Format given stored string in config language // Format given stored string in config language
@ -103,9 +103,9 @@ DBot.prototype.t = function(string, formatData) {
return this.strings[string][lang].format(formatData); return this.strings[string][lang].format(formatData);
}; };
DBot.prototype.act = function(channel, data) { /*DBot.prototype.act = function(channel, data) {
this.instance.send('PRIVMSG', channel, ':\001ACTION ' + data + '\001'); this.instance.send('PRIVMSG', channel, ':\001ACTION ' + data + '\001');
} }*/
// Save the database file // Save the database file
DBot.prototype.save = function() { DBot.prototype.save = function() {

View File

@ -189,5 +189,11 @@
}, },
"search_results": { "search_results": {
"english": "{category} ({needle}): '{quote}' [{matches} results]" "english": "{category} ({needle}): '{quote}' [{matches} results]"
},
"join": {
"english": "Joined {channel}"
},
"part": {
"english": "Left {channel}"
} }
} }