Skip to content

Commit

Permalink
Merge pull request fntsrlike#3 from jimmyhillis/feature/shh
Browse files Browse the repository at this point in the history
FEATURE: Add new config setting to keep the bot quiet in IRC fntsrlike#1
  • Loading branch information
jimmyhillis committed Aug 28, 2014
2 parents 183b115 + 5935c78 commit 85e79a0
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 14 deletions.
2 changes: 2 additions & 0 deletions config-example.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ var config = {
users: {
'~irclogin': 'slackuser'
}
// optionals
// silent: false // keep the bot quiet
};

var slackbot = new slackbot.Bot(config);
Expand Down
42 changes: 29 additions & 13 deletions lib/bot.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
var _ = require('underscore');
var IRC = require('irc');
var slack = require('./slacker');

Expand All @@ -11,15 +12,16 @@ var slack = require('./slacker');
* - users: Map of ~login: slack usernames
*/
var Bot = function (config) {
this.config = config || {};
this.config = _.defaults(config, {
silent: false,
nick: 'slckbt',
username: 'slckbt'
});
this._usermap = {
users: this.config.users || {},
nicks: {}
};
this.client = new IRC.Client(
this.config.server,
this.config.nick || 'slackbot',
{
this.client = new IRC.Client(this.config.server, this.config.nick, {
userName: this.config.username,
channels: Object.keys(this.config.channels)
}
Expand All @@ -38,7 +40,7 @@ Bot.prototype._handleErrors = function () {
this.client.addListener('error', function (message) {
var channel = message.args[1];
var error_message = mapPronouns(message.args[2]);
self.client.say(channel, 'I don\'t feel so well... I think it\'s because ' + error_message);
self.speak(channel, 'I don\'t feel so well because ' + error_message);
});
};

Expand All @@ -58,7 +60,7 @@ Bot.prototype._trackUsers = function () {
self._usermap.nicks[nick] = self._usermap.users[whois.user];
});
});
self.client.say(channel, 'i\'m all over you slackers');
self.speak(channel, 'I\'m all over you slackers');
});
// New user has joined, match him up
this.client.addListener('join', function (channel, nick, whois) {
Expand All @@ -68,7 +70,8 @@ Bot.prototype._trackUsers = function () {
else {
self._usermap.nicks[nick] = self._usermap.users[whois.user];
self.giveOps(channel, nick);
self.client.say(channel, 'i\'m watching you slacker @' + self._usermap.nicks[nick]);
self.speak(channel, 'i\'m watching you slacker @' +
self._usermap.nicks[nick]);
}
});
// Existing user has changed nickname
Expand All @@ -79,13 +82,16 @@ Bot.prototype._trackUsers = function () {
self._usermap.nicks[new_nick] = self._usermap.nicks[old_nick];
delete self._usermap.nicks[old_nick];
channels.forEach(function (channel) {
self.client.say(channel, 'don\'t think you can hide slacker @' + self._usermap.nicks[new_nick]);
self.speak(channel, 'don\'t think you can hide slacker @' +
self._usermap.nicks[new_nick]);
});
});
};

/**
* Attempt to give a user op controls
* @param {string} channel IRC channel name
* @param {string} nick User to provide op status to
*/
Bot.prototype.giveOps = function (channel, nick) {
this.client.send('MODE', channel, '+o', nick);
Expand All @@ -109,6 +115,17 @@ Bot.prototype.listen = function () {
});
};

/**
* Push a message to a channel
* @param {string} channel IRC channel name
* @param {string} message Message to push to channel
*/
Bot.prototype.speak = function (channel, message) {
if (!this.config.silent) {
this.client.say(channel, message);
}
};

/**
* Map users with whois to get ~loginname for stability
* @param {string} message Message to replace IRC user with slack @user
Expand All @@ -128,15 +145,14 @@ Bot.prototype.prepareMessage = function (message, users) {
* Try and map error commands (in third person) to first person
* so the bot is more personal.
*/
var mapPronouns = function (error_message) {
var mapPronouns = function (message) {
var map = {
'you': 'i',
'you\'re': 'i\'m',
'you\'re': 'i\'m'
};
error_message = error_message.split(' ').map(function (word) {
return message.split(' ').map(function (word) {
return map[word.toLowerCase()] ? map[word.toLowerCase()] : word;
}).join(' ');
return error_message;
};

exports = module.exports.Bot = Bot;
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@
},
"dependencies": {
"request": "~2.33.0",
"irc": "~0.3.6"
"irc": "~0.3.6",
"underscore": "^1.7.0"
},
"devDependencies": {},
"bin": {
Expand Down

0 comments on commit 85e79a0

Please sign in to comment.