Skip to content

Commit

Permalink
Don't crash when the bot isn't a member of a channel
Browse files Browse the repository at this point in the history
  • Loading branch information
ekmartin committed Jan 12, 2016
1 parent 6f56bff commit 4a34c64
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 3 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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.
Expand Down
2 changes: 1 addition & 1 deletion lib/bot.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
12 changes: 10 additions & 2 deletions test/bot.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down
1 change: 1 addition & 0 deletions test/stubs/channel-stub.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ class ChannelStub extends EventEmitter {
super();
this.name = 'slack';
this.is_channel = true;
this.is_member = true;
this.members = ['testuser'];
}
}
Expand Down

0 comments on commit 4a34c64

Please sign in to comment.