Split more into seperate files and automatic loading of modules.

This commit is contained in:
Luke Slater 2011-08-22 18:56:36 +01:00
parent 62d9c65a35
commit a32b011b7f
5 changed files with 86 additions and 54 deletions

47
admin.js Normal file
View File

@ -0,0 +1,47 @@
var adminCommands = function(dbot) {
var dbot = dbot;
var commands = {
'join': function(data, params) {
dbot.join(params[1]);
dbot.say(admin, 'Joined ' + params[1]);
},
'part': function(data, params) {
dbot.part(params[1]);
dbot.say(admin);
},
'reload': function(data, params) {
dbot.say(admin, 'Reloading DB.');
try {
dbot.db = JSON.parse(fs.readFileSync('db.json', 'utf-8'));
} catch(err) {
dbot.say(admin, 'DB reload failed.');
} finally {
dbot.say(admin, 'DB Reload successful.');
}
},
'say': function(data, params) {
var c = params[1];
var m = params.slice(2).join(' ');
dbot.say(c, m);
}
};
return {
'listener': function(data) {
params = data.message.split(' ');
if(commands.hasOwnProperty(params[0]))
commands[params[0]](data, params);
},
'on': 'PRIVMSG'
};
};
exports.fetch = function(dbot) {
return adminCommands(dbot);
};

22
puns.js Normal file
View File

@ -0,0 +1,22 @@
var puns = function(dbot) {
var dbot = dbot;
return {
'listener': function(data) {
if(data.user == 'Lamp') {
dbot.say(data.channel, dbot.db.quoteArrs.lamp.random());
} else if(data.user == 'reality') {
dbot.instance.say(data.channel, dbot.db.realiPuns.random());
} else if(instance.inChannel(data.channel)) {
dbot.instance.say('aisbot', '.karma ' + data.user);
dbot.waitingForKarma = data.channel;
}
},
'on': 'JOIN'
};
}
exports.fetch = function(dbot) {
return puns(dbot);
};

View File

@ -1,5 +1,5 @@
var quotes = function(quotes) {
var qArrs = quotes;
var quotes = function(dbot) {
var qArrs = dbot.db.quoteArrs;
return {
get: function(key) {
@ -42,6 +42,6 @@ var quotes = function(quotes) {
};
};
exports.fetch = function() {
return quotes;
exports.fetch = function(dbot) {
return quotes(dbot);
};

61
run.js
View File

@ -2,6 +2,8 @@ var fs = require('fs');
var jsbot = require('./jsbot');
var quote = require('./quotes');
var userCommands = require('./user');
var adminCommands = require('./admin');
var puns = require('./puns');
///////////////////////////
@ -9,65 +11,24 @@ Array.prototype.random = function() {
return this[Math.floor((Math.random()*this.length))];
};
///////////////////////////
this.adminCommands = {
'join': function(data, params) {
instance.join(params[1]);
instance.say(admin, 'Joined ' + params[1]);
},
'part': function(data, params) {
instance.part(params[1]);
instance.say(admin);
},
'reload': function(data, params) {
instance.say(admin, 'Reloading DB.');
try {
db = JSON.parse(fs.readFileSync('db.json', 'utf-8'));
} catch(err) {
instance.say(admin, 'DB reload failed.');
} finally {
instance.say(admin, 'DB Reload successful.');
}
},
'say': function(data, params) {
var c = params[1];
var m = params.slice(2).join(' ');
instance.say(c, m);
}
};
///////////////////////////
var dbot = Class.create({
initialize: function(quotes, userCommands) {
initialize: function(dModules, quotes) {
this.admin = 'reality';
this.waitingForKarma = false;
this.name = 'depressionbot';
this.db = JSON.parse(fs.readFileSync('db.json', 'utf-8'));
this.quotes = quotes(this.db.quoteArrs);
this.userCommands = userCommands.fetch(this);
this.quotes = quotes.fetch(this);
this.instance = jsbot.createJSBot(this.name, 'elara.ivixor.net', 6667, this, function() {
this.instance.join('#realitest');
}.bind(this));
this.instance.addListener(this.userCommands.on, this.userCommands.listener);
this.instance.addListener('JOIN', function(data) {
if(data.user == 'Lamp') {
this.instance.say(data.channel, db.quoteArrs.lamp.random());
} else if(data.user == 'reality') {
this.instance.say(data.channel, db.realiPuns.random());
} else if(instance.inChannel(data.channel)) {
this.instance.say('aisbot', '.karma ' + data.user);
this.waitingForKarma = data.channel;
}
});
this.modules = dModules.collect(function(n) {
var module = n.fetch(this);
this.instance.addListener(module.on, module.listener);
return n.fetch(this);
}.bind(this));
this.instance.addListener('KICK', function(data) {
if(data.kickee == name) {
@ -114,6 +75,8 @@ var dbot = Class.create({
}
}
});
this.instance.connect();
},
say: function(channel, data) {
@ -125,4 +88,4 @@ var dbot = Class.create({
}
});
new dbot(quote.fetch(), userCommands);
new dbot([userCommands, adminCommands, puns], quote);

View File

@ -75,7 +75,7 @@ var userCommands = function(dbot) {
},
'on': 'PRIVMSG'
}
};
};
exports.fetch = function(dbot) {