Skip to content

Commit 4a34c64

Browse files
committed
Don't crash when the bot isn't a member of a channel
1 parent 6f56bff commit 4a34c64

File tree

4 files changed

+17
-3
lines changed

4 files changed

+17
-3
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
# Changelog
22
This project adheres to [Semantic Versioning](http://semver.org/).
33

4+
## [3.7.3] - 2016-01-12
5+
### Fixed
6+
- Don't crash when trying to send a message to a Slack channel the bot
7+
isn't a member of.
8+
49
## [3.7.2] - 2016-01-12
510
### Changed
611
- Remove babel-polyfill, use functions available in Node 0.10 and above instead.

lib/bot.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,7 @@ class Bot {
206206
if (slackChannelName) {
207207
const slackChannel = this.slack.getChannelGroupOrDMByName(slackChannelName);
208208

209-
if (!slackChannel) {
209+
if (!slackChannel || !slackChannel.is_member) {
210210
logger.info('Tried to send a message to a channel the bot isn\'t in: ',
211211
slackChannelName);
212212
return;

test/bot.test.js

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,8 +86,16 @@ describe('Bot', function() {
8686
});
8787

8888
it('should not send messages to slack if the bot isn\'t in the channel', function() {
89-
this.bot.slack.getChannelGroupOrDMByName = function() {
90-
return null;
89+
this.bot.slack.getChannelGroupOrDMByName = () => null;
90+
this.bot.sendToSlack('user', '#irc', 'message');
91+
ChannelStub.prototype.postMessage.should.not.have.been.called;
92+
});
93+
94+
it('should not send messages to slack if the channel\'s is_member is false', function() {
95+
this.bot.slack.getChannelGroupOrDMByName = () => {
96+
const channel = new ChannelStub();
97+
channel.is_member = false;
98+
return channel;
9199
};
92100

93101
this.bot.sendToSlack('user', '#irc', 'message');

test/stubs/channel-stub.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ class ChannelStub extends EventEmitter {
66
super();
77
this.name = 'slack';
88
this.is_channel = true;
9+
this.is_member = true;
910
this.members = ['testuser'];
1011
}
1112
}

0 commit comments

Comments
 (0)