Permalink
Browse files

Add a help system.

Help items are defined in commands, not hard coded.
  • Loading branch information...
mythmon authored and rehandalal committed Aug 23, 2012
1 parent 53bda64 commit f31baa185fd683d6e472c42f14806c498c8bd0d1
Showing with 125 additions and 81 deletions.
  1. +2 −2 api.js
  2. +123 −79 standup-irc.js
View
4 api.js
@@ -43,8 +43,8 @@ exports.user = {
var data = {
user: nick,
api_key: config.standup.api_key
- }
+ };
data[key] = value;
return utils.request('/api/v1/user/' + user + '/', 'POST', data);
}
-}
+};
View
@@ -121,14 +121,14 @@ client.on('message', function(user, channel, message) {
var cmd_name = message.split(' ')[0].slice(1);
var args = message.split(' ').slice(1);
var cmd = commands[cmd_name] || commands['default'];
- cmd(user, channel, message, args);
+ cmd.func(user, channel, message, args);
} else {
if (message.toLowerCase() === 'botsnack') {
// Special case for botsnack
- commands.botsnack(user, channel, message, []);
+ commands.botsnack.func(user, channel, message, []);
} else {
// If they didn't ask for a specific command, post a status.
- commands.status(user, channel, message, [channel, message]);
+ commands.status.func(user, channel, message, [channel, message]);
}
}
}
@@ -150,107 +150,147 @@ client.on('notice', function(from, to, text) {
var commands = {
/* Simple presence check. */
- ping: function(user, channel, message, args) {
- client.say(channel, "Pong!");
+ ping: {
+ help: "A simple presence check.",
+ usage: undefined,
+ func: function(user, channel, message, args) {
+ client.say(channel, "Pong!");
+ }
},
/* Create a status. */
- status: function(user, channel, message, args) {
- utils.ifAuthorized(user, channel, function() {
- var project = args[0];
- if (project[0] === '#') {
- project = project.slice(1);
- }
+ status: {
+ help: undefined, // Don't tell the user about this.
+ usage: "<project> status message",
+ func: function(user, channel, message, args) {
+ utils.ifAuthorized(user, channel, function() {
+ var project = args[0];
+ if (project[0] === '#') {
+ project = project.slice(1);
+ }
- var status = args.slice(1).join(' ');
- var response = api.status.create(user, project, status);
+ var status = args.slice(1).join(' ');
+ var response = api.status.create(user, project, status);
- response.once('ok', function(data) {
- client.say(channel, 'Ok, submitted status #' + data.id);
- });
+ response.once('ok', function(data) {
+ client.say(channel, 'Ok, submitted status #' + data.id);
+ });
- response.once('error', function(err, data) {
- client.say(channel, 'Uh oh, something went wrong.');
+ response.once('error', function(err, data) {
+ client.say(channel, 'Uh oh, something went wrong.');
+ });
});
- });
+ }
},
/* Delete a status by id number. */
- 'delete': function(user, channel, message, args) {
- utils.ifAuthorized(user, channel, function() {
- var id = args[0];
- if (id[0] === '#') {
- id = id.slice(1);
- }
- id = parseInt(id, 10);
- if (isNaN(id)) {
- client.say(channel, '"' + args[0] + '" ' +
- 'is not a valid status ID.');
- return;
- }
+ 'delete': {
+ help: "Delete a status by id.",
+ usage: "<id>",
+ func: function(user, channel, message, args) {
+ utils.ifAuthorized(user, channel, function() {
+ var id = args[0];
+ if (id[0] === '#') {
+ id = id.slice(1);
+ }
+ id = parseInt(id, 10);
+ if (isNaN(id)) {
+ client.say(channel, '"' + args[0] + '" ' +
+ 'is not a valid status ID.');
+ return;
+ }
- var response = api.status.delete(id, user);
+ var response = api.status.delete(id, user);
- response.once('ok', function(data) {
- client.say(channel, 'Ok, status #' + id + ' is no more!');
- });
+ response.once('ok', function(data) {
+ client.say(channel, 'Ok, status #' + id + ' is no more!');
+ });
- response.once('error', function(code, data) {
- data = JSON.parse(data);
- if (code === 403) {
- client.say(channel, "You don't have permission to do " +
- "that. Did you post that status?");
- } else {
- var error = "I'm a failure, I couldn't do it.";
- if (data.error) {
- error += ' The server said: "' + data.error + '"';
+ response.once('error', function(code, data) {
+ data = JSON.parse(data);
+ if (code === 403) {
+ client.say(channel, "You don't have permission to do " +
+ "that. Did you post that status?");
+ } else {
+ var error = "I'm a failure, I couldn't do it.";
+ if (data.error) {
+ error += ' The server said: "' + data.error + '"';
+ }
+ client.say(channel, error);
}
- client.say(channel, error);
- }
+ });
});
- });
+ }
},
/* Every bot loves botsnacks. */
- 'botsnack': function(user, channel, message, args) {
- var replies = [
- 'Yummy!',
- 'Thanks, ' + user + '!',
- 'My favorite!',
- 'Can I have another?',
- 'Tasty!'
- ];
- client.say(channel, _.shuffle(replies)[0]);
+ 'botsnack': {
+ func: function(user, channel, message, args) {
+ var replies = [
+ 'Yummy!',
+ 'Thanks, ' + user + '!',
+ 'My favorite!',
+ 'Can I have another?',
+ 'Tasty!'
+ ];
+ client.say(channel, _.shuffle(replies)[0]);
+ }
},
/* Check a user's authorization status. */
- 'trust': function(user, channel, message, args) {
- var a = authman.checkUser(args);
- a.once('authorization', function(trust) {
- if (trust) {
- client.say(channel, 'I trust ' + args);
- } else {
- client.say(channel, "I don't trust " + args);
- }
- });
+ 'trust': {
+ help: "Check a user's authorization status.",
+ usage: "<user>",
+ func: function(user, channel, message, args) {
+ var a = authman.checkUser(args);
+ a.once('authorization', function(trust) {
+ if (trust) {
+ client.say(channel, 'I trust ' + args);
+ } else {
+ client.say(channel, "I don't trust " + args);
+ }
+ });
+ }
},
/* Leave the channel */
- 'bye': function(user, channel) {
- client.say(channel, 'Bye!');
- client.part(channel);
- config.irc.channels = _(config.irc.channels).without(channel);
+ 'bye': {
+ help: "Ask the bot to leave the channel.",
+ func: function(user, channel) {
+ client.say(channel, 'Bye!');
+ client.part(channel);
+ }
},
- 'help': function(user, channel) {
- function say(message) {
- client.say(channel, message);
+ /* Provide a help system.
+ *
+ * To give a command a help message, define a 'help' field in it's object.
+ * If the help field is not present, the command will not be listed.
+ * To give a comand an arguments list, define a 'usage' field in it's object.
+ * If the usage field is not present, the command will not list a usage it's help message.
+ */
+ 'help': {
+ help: "This help message.",
+ func: function(user, channel) {
+ var command;
+ var help;
+ var usage;
+
+ say('Available commands:');
+
+ for (command in commands) {
+ help = commands[command].help;
+ usage = commands[command].usage;
+
+ if (help !== undefined) {
+ if (usage === undefined) {
+ client.say(channel, '!' + command + ' - ' + help);
+ } else {
+ client.say(channel, '!' + command + ' ' + usage + ' - ' + help);
+ }
+ }
+ }
}
- say('Available commands:');
- say('botsnack - Feed the bot!');
- say('!bye - Ask to leave the channel.');
- say('!delete <id> - Delete a previously posted status.');
- say("!update <name|email|github_handle> <value> [<user>] - Updates the user's setting.");
},
/* Update a user's settings */
@@ -298,7 +338,11 @@ var commands = {
},
/* The default action. Return an error. */
- 'default': function(user, channel, message) {
- client.say(channel, user + ": Huh? Try !help.");
+ 'default': {
+ help: undefined,
+ args: undefined,
+ func: function(user, channel, message) {
+ client.say(channel, user + ": Huh? Try !help.");
+ }
}
};

0 comments on commit f31baa1

Please sign in to comment.