diff --git a/CHANGELOG.md b/CHANGELOG.md index d5de9f0..e89ae58 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,11 @@ # Changelog This project adheres to [Semantic Versioning](http://semver.org/). +## [3.7.3] - 2016-01-12 +### Fixed +- Don't crash when trying to send a message to a Slack channel the bot +isn't a member of. + ## [3.7.2] - 2016-01-12 ### Changed - Remove babel-polyfill, use functions available in Node 0.10 and above instead. diff --git a/lib/bot.js b/lib/bot.js index 90398d9..606a173 100644 --- a/lib/bot.js +++ b/lib/bot.js @@ -206,7 +206,7 @@ class Bot { if (slackChannelName) { const slackChannel = this.slack.getChannelGroupOrDMByName(slackChannelName); - if (!slackChannel) { + if (!slackChannel || !slackChannel.is_member) { logger.info('Tried to send a message to a channel the bot isn\'t in: ', slackChannelName); return; diff --git a/test/bot.test.js b/test/bot.test.js index 2f5c70c..5fba5f1 100644 --- a/test/bot.test.js +++ b/test/bot.test.js @@ -86,8 +86,16 @@ describe('Bot', function() { }); it('should not send messages to slack if the bot isn\'t in the channel', function() { - this.bot.slack.getChannelGroupOrDMByName = function() { - return null; + this.bot.slack.getChannelGroupOrDMByName = () => null; + this.bot.sendToSlack('user', '#irc', 'message'); + ChannelStub.prototype.postMessage.should.not.have.been.called; + }); + + it('should not send messages to slack if the channel\'s is_member is false', function() { + this.bot.slack.getChannelGroupOrDMByName = () => { + const channel = new ChannelStub(); + channel.is_member = false; + return channel; }; this.bot.sendToSlack('user', '#irc', 'message'); diff --git a/test/stubs/channel-stub.js b/test/stubs/channel-stub.js index 8cf7f18..e83da58 100644 --- a/test/stubs/channel-stub.js +++ b/test/stubs/channel-stub.js @@ -6,6 +6,7 @@ class ChannelStub extends EventEmitter { super(); this.name = 'slack'; this.is_channel = true; + this.is_member = true; this.members = ['testuser']; } }