Skip to content

Commit f13d433

Browse files
committed
Fix webhooks for users with no avatars
Fixes #529.
1 parent eafb0bc commit f13d433

File tree

2 files changed

+16
-1
lines changed

2 files changed

+16
-1
lines changed

lib/bot.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -438,7 +438,8 @@ class Bot {
438438

439439
// No matching user or more than one => default avatar
440440
if (users && users.size === 1) {
441-
return users.first().user.avatarURL.replace(/\?size=\d{1,}$/, '?size=128');
441+
const url = users.first().user.avatarURL;
442+
if (url) return url.replace(/\?size=\d{1,}$/, '?size=128');
442443
}
443444

444445
// If there isn't a URL format, don't send an avatar at all

test/bot.test.js

+14
Original file line numberDiff line numberDiff line change
@@ -1107,6 +1107,13 @@ describe('Bot', function () {
11071107
this.addUser(userObj, memberObj);
11081108
this.bot.getDiscordAvatar('nickless', '#irc').should.equal('/avatars/124/nickless-avatar.png?size=128');
11091109
});
1110+
1111+
it('should handle users without avatars', function () {
1112+
const userObj = { id: 124, username: 'avatarless' };
1113+
const memberObj = {};
1114+
this.addUser(userObj, memberObj);
1115+
expect(this.bot.getDiscordAvatar('avatarless', '#irc')).to.equal(null);
1116+
});
11101117
});
11111118

11121119
context('when matching avatars with fallback URL', function () {
@@ -1134,6 +1141,13 @@ describe('Bot', function () {
11341141
this.bot.getDiscordAvatar('diffNick', '#irc').should.equal('/avatars/124/avatarURL.png?size=128');
11351142
this.bot.getDiscordAvatar('common', '#irc').should.equal('avatarFrom/common');
11361143
});
1144+
1145+
it('should use fallback for users without avatars', function () {
1146+
const userObj = { id: 124, username: 'avatarless' };
1147+
const memberObj = {};
1148+
this.addUser(userObj, memberObj);
1149+
this.bot.getDiscordAvatar('avatarless', '#irc').should.equal('avatarFrom/avatarless');
1150+
});
11371151
});
11381152
});
11391153

0 commit comments

Comments
 (0)