Put user commands in their own file and a lot of work on making the organisation more reasonable.

This commit is contained in:
Luke Slater 2011-08-22 17:54:12 +01:00
parent cbc526f21b
commit 62d9c65a35
2 changed files with 181 additions and 164 deletions

160
run.js
View File

@ -1,6 +1,7 @@
var fs = require('fs');
var jsbot = require('./jsbot');
var quote = require('./quotes');
var userCommands = require('./user');
///////////////////////////
@ -11,17 +12,7 @@ Array.prototype.random = function() {
///////////////////////////
///////////////////////////
var dbot = function(quotes) {
var admin = 'reality';
var waitingForKarma = false;
var name = 'depressionbot';
var db = JSON.parse(fs.readFileSync('db.json', 'utf-8'));
var quotes = quotes(db.quoteArrs);
var adminCommands = {
this.adminCommands = {
'join': function(data, params) {
instance.join(params[1]);
instance.say(admin, 'Joined ' + params[1]);
@ -50,145 +41,88 @@ var dbot = function(quotes) {
}
};
var userCommands = {
'~kc': function(data, params) {
instance.say('aisbot', '.karma ' + data.message.split(' ')[1]);
waitingForKarma = data.channel;
},
'~q': function(data, params) {
var q = data.message.match(/~q ([\d\w\s]*)/)
if(q != undefined) {
instance.say(data.channel, quotes.get(q[1].trim()));
}
},
///////////////////////////
var dbot = Class.create({
initialize: function(quotes, userCommands) {
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);
'~qadd': function(data, params) {
var q = data.message.match(/~qadd ([\d\w\s]*)=(.+)$/);
if(q != null && q.length >= 3) {
instance.say(data.channel, quotes.add(q));
fs.writeFile('db.json', JSON.stringify(db, null, ' '));
} else {
instance.say(data.channel, 'Burn the invalid syntax!');
}
},
'~qset': function(data, params) {
var q = data.message.match(/~qset ([\d\w\s]*)=(.+)$/);
if(q != undefined && q.length >= 3) {
instance.say(data.channel, quotes.set(q));
}
},
'~qcount': function(data, params) {
var q = data.message.match(/~qcount ([\d\w\s]*)/)[1].trim();
if(q != undefined) {
instance.say(data.channel, quotes.count(q));
}
},
'~reality': function(data, params) {
instance.say(data.channel, db.realiPuns.random());
},
'~d': function(data, params) {
instance.say(data.channel, data.user + ': ' + db.quoteArrs['depressionbot'].random());
},
'~rq': function(data, params) {
instance.say(data.channel, quotes.random());
},
'~kickcount': function(data, params) {
if(db.kicks[params[1]] == undefined) {
instance.say(data.channel, params[1] + ' has either never been kicked or does not exist.');
} else {
instance.say(data.channel, params[1] + ' has been kicked ' + db.kicks[params[1]] + ' times.');
}
}
};
var instance = jsbot.createJSBot(name, 'elara.ivixor.net', 6667, function() {
instance.join('#realitest');
instance.join('#42');
instance.join('#itonlygetsworse');
this.instance = jsbot.createJSBot(this.name, 'elara.ivixor.net', 6667, this, function() {
this.instance.join('#realitest');
}.bind(this));
instance.addListener('JOIN', function(data) {
this.instance.addListener(this.userCommands.on, this.userCommands.listener);
this.instance.addListener('JOIN', function(data) {
if(data.user == 'Lamp') {
instance.say(data.channel, db.quoteArrs.lamp.random());
this.instance.say(data.channel, db.quoteArrs.lamp.random());
} else if(data.user == 'reality') {
instance.say(data.channel, db.realiPuns.random());
this.instance.say(data.channel, db.realiPuns.random());
} else if(instance.inChannel(data.channel)) {
instance.say('aisbot', '.karma ' + data.user);
waitingForKarma = data.channel;
this.instance.say('aisbot', '.karma ' + data.user);
this.waitingForKarma = data.channel;
}
});
instance.addListener('KICK', function(data) {
this.instance.addListener('KICK', function(data) {
if(data.kickee == name) {
instance.join(data.channel);
instance.say(data.channel, 'Thou shalt not kick ' + name);
db.kicks[name] += 1;
this.instance.join(data.channel);
this.instance.say(data.channel, 'Thou shalt not kick ' + name);
this.db.kicks[name] += 1;
} else {
if(db.kicks[data.kickee] == undefined) {
db.kicks[data.kickee] = 1;
if(this.db.kicks[data.kickee] == undefined) {
this.db.kicks[data.kickee] = 1;
} else {
db.kicks[data.kickee] += 1;
this.db.kicks[data.kickee] += 1;
}
instance.say(data.channel, data.kickee + '-- (' + data.kickee + ' has been kicked ' + db.kicks[data.kickee] + ' times)');
instance.say(data.channel, data.kickee + '-- (' + data.kickee + ' has been kicked ' + this.db.kicks[data.kickee] + ' times)');
}
fs.writeFile('db.json', JSON.stringify(db, null, ' '));
this.save();
});
instance.addListener('PRIVMSG', function(data) {
this.instance.addListener('PRIVMSG', function(data) {
if(data.user == 'aisbot' && data.channel == name && waitingForKarma != false && data.message.match(/is at/)) {
var split = data.message.split(' ');
var target = split[0];
var karma = split[3];
if(karma.startsWith('-')) {
instance.say(waitingForKarma, target + db.hatedPhrases.random() + ' (' + karma + ')');
this.instance.say(waitingForKarma, target + this.db.hatedPhrases.random() + ' (' + karma + ')');
} else if(karma == '0') {
instance.say(waitingForKarma, 'All ' + target + ' knows is that their gut says \'maybe.\' (0)');
this.instance.say(waitingForKarma, target + this.db.neutralPhrases.random() + ' (0)');
} else {
instance.say(waitingForKarma, target + db.lovedPhrases.random() + ' (' + karma + ')');
this.instance.say(waitingForKarma, target + this.db.lovedPhrases.random() + ' (' + karma + ')');
}
waitingForKarma = false;
}
});
instance.addListener('PRIVMSG', function(data) {
params = data.message.split(' ');
if(data.user == admin && data.channel == name && adminCommands[params[0]] != undefined) {
adminCommands[params[0]](data, params);
} else {
if(data.channel == name) data.channel = data.user;
if(userCommands[params[0]] != undefined) {
userCommands[params[0]](data, params);
} else {
var q = data.message.match(/^~([\d\w\s]*)/)
if(q != undefined) {
instance.say(data.channel, quotes.get(q[1].trim()));
}
}
}
});
instance.addListener('PRIVMSG', function(data) {
this.instance.addListener('PRIVMSG', function(data) {
if(data.user == 'reality') {
var once = data.message.match(/I ([\d\w\s]* once.)/);
if(once != null) {
db.realiPuns.push('reality ' + once[1]);
instance.say(data.channel, '\'reality ' + once[1] + '\' saved.');
fs.writeFile('db.json', JSON.stringify(db, null, ' '));
this.db.realiPuns.push('reality ' + once[1]);
this.instance.say(data.channel, '\'reality ' + once[1] + '\' saved.');
this.save();
}
}
});
},
return this;
}(quote.fetch());
say: function(channel, data) {
this.instance.say(channel, data);
},
save: function() {
fs.writeFile('db.json', JSON.stringify(this.db, null, ' '));
}
});
new dbot(quote.fetch(), userCommands);

83
user.js Normal file
View File

@ -0,0 +1,83 @@
var userCommands = function(dbot) {
var dbot = dbot;
var commands = {
'~kc': function(data, params) {
dbot.say('aisbot', '.karma ' + data.message.split(' ')[1]);
dbot.waitingForKarma = data.channel;
},
'~q': function(data, params) {
var q = data.message.match(/~q ([\d\w\s]*)/)
if(q != undefined) {
dbot.say(data.channel, dbot.quotes.get(q[1].trim()));
}
},
'~qadd': function(data, params) {
var q = data.message.match(/~qadd ([\d\w\s]*)=(.+)$/);
if(q != null && q.length >= 3) {
dbot.say(data.channel, dbot.quotes.add(q));
dbot.save();
} else {
dbot.say(data.channel, 'Burn the invalid syntax!');
}
},
'~qset': function(data, params) {
var q = data.message.match(/~qset ([\d\w\s]*)=(.+)$/);
if(q != undefined && q.length >= 3) {
dbot.say(data.channel, dbot.quotes.set(q));
}
},
'~qcount': function(data, params) {
var q = data.message.match(/~qcount ([\d\w\s]*)/)[1].trim();
if(q != undefined) {
dbot.say(data.channel, dbot.quotes.count(q));
}
},
'~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[params[1]] == undefined) {
dbot.say(data.channel, params[1] + ' has either never been kicked or does not exist.');
} else {
dbot.say(data.channel, params[1] + ' has been kicked ' + dbot.db.kicks[params[1]] + ' times.');
}
}
};
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);
} else {
var q = data.message.match(/^~([\d\w\s]*)/)
if(q != undefined) {
dbot.say(data.channel, dbot.quotes.get(q[1].trim()));
}
}
},
'on': 'PRIVMSG'
}
};
exports.fetch = function(dbot) {
return userCommands(dbot);
};