Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Added reporting of errors when loading a plugin, fixed error when unl…

…oading plugin that doesn't exist
  • Loading branch information...
commit 6be81c42581f599636015d7d6b0ddd3df0741741 1 parent fc96fda
@draggor authored
View
1  example/bot.js
@@ -10,6 +10,7 @@ b = new bot('irc.freenode.net', 'nodeboy1184', {
port: 6667,
userName: 'nodeboy',
realName: 'nodeboy',
+ autorejoin: true,
pluginsPath: '../plugins/',
debug: true,
secure: false,
View
23 example/furnet.js
@@ -0,0 +1,23 @@
+var bot = require('../')
+ , repl = require('repl')
+ ;
+
+process.on('uncaughtException', function(err) {
+ console.log('Uncaught Exception: ' + err);
+});
+
+b = new bot('irc.furnet.org', 'nodeboy1184', {
+ port: 6667,
+ userName: 'nodeboy',
+ realName: 'nodeboy',
+ pluginsPath: '../plugins/',
+ debug: true,
+ secure: false,
+ channels: ['#turtleshell']
+});
+
+b.loadPlugin('admin', {nick: ['draggor', 'turtle']});
+//b.loadPlugin('dice');
+
+var r = repl.start();
+r.context.b = b;
View
17 index.js
@@ -20,6 +20,9 @@ function bot(server, nick, options) {
bot.nick = newNick;
}
});
+ this.client.addListener('raw', function(msg) {
+ console.log(msg);
+ });
this.say = util.burstThrottle(irc.Client.prototype.say, this.burstCount, this.burstLimit, this.throttle, this.client);
this.listeners = {};
}
@@ -175,15 +178,15 @@ bot.prototype.unloadPlugin = function(name, options) {
var cleanName = this.pluginsPath + sanitize(name)
, pl = this.plugins[cleanName]
;
+
+ if (pl) {
+ if (pl.teardownPlugin) {
+ pl.teardownPlugin();
+ }
- if (pl.teardownPlugin) {
- pl.teardownPlugin();
- }
-
- pl.options = options || {};
- pl.options.prefix = name;
+ pl.options = options || {};
+ pl.options.prefix = name;
- if(pl) {
this.removeListeners(pl);
delete this.plugins[cleanName];
}
View
2  package.json
@@ -1,6 +1,6 @@
{
"name" : "ircbot",
- "version" : "0.0.1",
+ "version" : "0.0.2",
"description" : "A simple irc bot with a reloadable plugin system",
"main" : "./index.js",
"homepage" : "http://github.com/draggor/node-ircbot/",
View
19 plugins/admin/cmd.js
@@ -1,4 +1,6 @@
-var util = require('../../util');
+var util = require('../../util')
+ , format = require('util').format
+ ;
exports.run = function(info) {
var cmd = cmds[info.cmdstr];
@@ -7,7 +9,7 @@ exports.run = function(info) {
}
};
-function listArgsCommand(func, info) {
+function listArgsCommand(func, info, formatStr) {
var sp = info.rest.toLowerCase().split(' ')
, name = sp.shift()
, args = {}
@@ -24,16 +26,19 @@ function listArgsCommand(func, info) {
}
}
- info.bot[func](name, args);
+ try {
+ info.bot[func](name, args);
+ info.bot.respond(info, format(formatStr, info.name));
+ } catch (err) {
+ info.bot.respond(info, err);
+ }
}
var cmds = {
load: function(info) {
- listArgsCommand('loadPlugin', info);
- info.bot.respond(info, 'Plugin loaded: ' + info.name);
+ listArgsCommand('loadPlugin', info, 'Plugin loaded: %s');
},
unload: function(info) {
- listArgsCommand('unloadPlugin', info);
- info.bot.respond(info, 'Plugin unloaded: ' + info.name);
+ listArgsCommand('unloadPlugin', info, 'Plugin unloaded: %s');
}
};
View
7 plugins/dice/index.js
@@ -3,12 +3,7 @@ var dice = require('./diceParser');
var regex = /\[[\dd\+\*\-\/\.\(\)\s]*\]/g;
function formatRoll(roll) {
- return roll[0]
- + ' => '
- + roll[1][1]
- + ' => '
- + roll[1][0]
- ;
+ return roll[0] + ' => ' + (typeof roll === 'number' ? roll : roll[1][1]) + ' => ' + (typeof roll === 'number' ? roll : roll[1][0]);
}
function parseLine(from, to, msg) {
View
4 plugins/farkle/index.js
@@ -1,4 +1,6 @@
-var util = require('../../util');
+var util = require('../../util')
+ , db = require('chaos')('./db/')
+ ;
Array.prototype.max = function(c) {
c = c || function(a, b) { return a > b ? a : b; };
View
33 plugins/ping/cmd.js
@@ -0,0 +1,33 @@
+var util = require('../../util');
+
+exports.run = function(info) {
+ var cmd = cmds[info.cmdstr];
+ if(cmd) {
+ cmd(info);
+ }
+};
+
+var pingsFrom = exports.pingsFrom = {};
+var pingsTo = exports.pingsTo = {};
+
+var cmds = {
+ ping: function(info) {
+ var sp = util.split(info.rest, ' ', 2)
+ , to = sp[0]
+ , msg = sp[1]
+ ;
+
+ pingsFrom[info.from] = pingsFrom[info.from] || {};
+ pingsFrom[info.from][to] = msg;
+
+ pingsTo[to] = pingsTo[to] || {};
+ pingsTo[to][info.from] = msg;
+ },
+ clear: function(info) {
+ var pings = pingsTo[info.from];
+ for(var k in pings) {
+ delete pingsFrom[k][info.from];
+ }
+ delete pingsTo[info.from];
+ }
+};
View
66 plugins/ping/index.js
@@ -0,0 +1,66 @@
+var commands = require('./cmd')
+ , util = require('../../util')
+ ;
+
+function parseLine(from, to, msg) {
+ if(msg[0] === '!') {
+ var sp = util.split(msg, ' ', 2)
+ , info = {
+ from: from,
+ to: to,
+ msg: msg,
+ cmdstr: sp[0].substr(1),
+ rest: sp[1],
+ bot: pingp.bot
+ }
+ ;
+
+ commands.run(info);
+ }
+}
+
+function activity(from, to, msg) {
+
+}
+
+// TODO: Fix this crap
+function updateNick(oldNick, newNick, channels) {
+ for(var k in commands.pingFrom[oldNick]) {
+ commands.pingFrom[oldNick][k].from = newNick;
+ }
+ for(var k in commands.PingTo[oldNick]) {
+ commands.pingTo[oldNick][k].to = newNick;
+ }
+ updateKey(commands.pingFrom, oldNick, newNick, pingMerge);
+ updateKey(commands.pingTo, oldNick, newNick, pingMerge);
+}
+
+function pingMerge(o, n) {
+ for(var k in o) {
+ n[k] = o[k].concat(n[k]);
+ }
+}
+
+function updateKey(m, o, n, merge) {
+ merge = merge || function(a, b) { return a; };
+ if(m[o]) {
+ if(m[n]) {
+ m[n] = merge(m[o], m[n]);
+ } else {
+ m[n] = m[o];
+ }
+ delete m[o];
+ }
+ return m;
+}
+
+var pingp = {
+ listeners: {
+ message: [parseLine, activity]
+ , nick: updateNick
+ },
+ reload: ['./cmd'].map(require.resolve)
+};
+
+module.exports = pingp;
+module.exports.u = updateKey;
View
61 plugins/sw/cmd.js
@@ -0,0 +1,61 @@
+var util = require('../../util')
+ ;
+
+var cmds = {};
+
+exports.run = function(info) {
+ var cmd = cmds[info.cmdstr];
+ if(cmd) {
+ cmd(info);
+ }
+};
+
+var cardValues = [ 2,3,4,5,6,7,8,9,'T','J','Q','K','A' ];
+var cardSuits = [ 'D', 'C', 'H', 'S' ];
+var cardSuitSymbols = {
+ 'D': '♦'
+, 'C': '♣'
+, 'H': '♥'
+, 'S': '♠'
+, 'R': 'R'
+, 'B': 'B'
+};
+var cards = [ 'JR', 'JB' ];
+for(var val in cardValues) {
+ for(var suit in cardSuits) {
+ cards.push(cardValues[val] + cardSuits[suit]);
+ }
+}
+
+var deck = util.randomize(cards.slice());
+
+cmds.card = function(info) {
+ var c = cards[Math.floor(Math.random() * 54)];
+ info.bot.say(info.to, info.from + '\'s random card is: ' + c);
+};
+
+cmds.draw = function(info) {
+ var c = '';
+ if(info.rest) {
+ var num = parseInt(info.rest);
+ if(num > deck.length) {
+ deck = util.randomize(cards.slice());
+ info.bot.say(info.to, 'Not enough cards left, shuffling!');
+ }
+ for(var i = 0; i < num; i++) {
+ c += deck.pop() + ' ';
+ }
+ } else {
+ c = deck.pop();
+ }
+ info.bot.say(info.to, info.from + ' drew: ' + c);
+ if(deck.length == 0) {
+ deck = util.randomize(cards.slice());
+ info.bot.say(info.to, 'Last card drawn, shuffling!');
+ }
+};
+
+cmds.shuffle = function(info) {
+ deck = util.randomize(cards.slice());
+ info.bot.say(info.to, 'Deck shuffled.');
+};
View
29 plugins/sw/index.js
@@ -0,0 +1,29 @@
+var commands = require('./cmd')
+ , util = require('../../util')
+ ;
+
+function parseLine(from, to, msg) {
+ if(msg[0] === '!') {
+ var sp = util.split(msg, ' ', 2)
+ , info = {
+ from: from,
+ to: to,
+ msg: msg,
+ cmdstr: sp[0].substr(1),
+ rest: sp[1],
+ bot: swp.bot
+ }
+ ;
+
+ commands.run(info);
+ }
+}
+
+var swp = {
+ listeners: {
+ message: parseLine
+ },
+ reload: ['./cmd'].map(require.resolve)
+};
+
+module.exports = swp;
View
12 util.js
@@ -18,6 +18,13 @@ exports.latch = function (actions, callback) {
});
};
+/*
+ * A unique ID counter starting from 0.
+ * reset is anything that could be boolean true,
+ * will return 0, and set the internal count to 1
+ * just as iff getUniqueId() had been called the
+ * first time.
+ */
exports.getUniqueId = (function() {
var id = 0;
return function(reset) {
@@ -30,6 +37,11 @@ exports.getUniqueId = (function() {
};
})();
+/*
+ * Same as getUniqueId, except it maintains a
+ * map of counters via a string prefix. Resets
+ * only happen for the given prefix.
+ */
exports.getUniquePrefixId = (function() {
var ids = {};
return function(prefix, reset) {
Please sign in to comment.
Something went wrong with that request. Please try again.