Skip to content

Commit 2bf761b

Browse files
committed
getDiscordAvatar refactoring
Added test
1 parent fa9ef71 commit 2bf761b

File tree

5 files changed

+48
-9
lines changed

5 files changed

+48
-9
lines changed

lib/bot.js

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -323,11 +323,9 @@ class Bot {
323323
}
324324

325325
getDiscordAvatar(nick) {
326-
const user = this.discord.guilds.first().members.find(
327-
member => member.user.username.toLowerCase() === nick.toLowerCase()
328-
);
326+
const user = this.discord.users.find('username', nick);
329327
if (user) {
330-
return user.user.avatarURL;
328+
return user.avatarURL;
331329
}
332330
return null;
333331
}

test/bot.test.js

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import discord from 'discord.js';
88
import Bot from '../lib/bot';
99
import createDiscordStub from './stubs/discord-stub';
1010
import ClientStub from './stubs/irc-client-stub';
11+
import createWebhookStub from './stubs/webhook-stub';
1112
import config from './fixtures/single-test-config.json';
1213
import configMsgFormatDefault from './fixtures/msg-formats-default.json';
1314

@@ -32,6 +33,8 @@ describe('Bot', function () {
3233
ClientStub.prototype.say = sandbox.stub();
3334
ClientStub.prototype.send = sandbox.stub();
3435
ClientStub.prototype.join = sandbox.stub();
36+
this.sendWebhookMessageStub = sandbox.stub();
37+
discord.WebhookClient = createWebhookStub(this.sendWebhookMessageStub);
3538
this.bot = new Bot(config);
3639
this.bot.connect();
3740
});
@@ -793,11 +796,29 @@ describe('Bot', function () {
793796
});
794797

795798
it('should create webhooks clients for each webhook url in the config', function () {
796-
this.bot.webhooks.should.have.property('#irc');
799+
this.bot.webhooks.should.have.property('#withwebhook');
797800
});
798801

799802
it('should extract id and token from webhook urls', function () {
800-
this.bot.webhooks['#irc'].id.should.equal('id');
801-
this.bot.webhooks['#irc'].token.should.equal('token');
803+
this.bot.webhooks['#withwebhook'].id.should.equal('id');
804+
this.bot.webhooks['#withwebhook'].token.should.equal('token');
805+
});
806+
807+
it('should find the matching webhook when it exists', function () {
808+
this.bot.findWebhook('#ircwebhook').should.not.equal(null);
809+
});
810+
811+
it('should prefer webhooks to send a message when possible', function () {
812+
const newConfig = { ...config, webhooks: { '#discord': 'https://discordapp.com/api/webhooks/id/token' } };
813+
const bot = new Bot(newConfig);
814+
bot.connect();
815+
bot.sendToDiscord('nick', '#irc', 'text');
816+
this.sendWebhookMessageStub.should.have.been.called;
817+
});
818+
819+
it('should find the user\'s avatar when the irc nick is also a discord username', function () {
820+
const testUser = new discord.User(this.bot.discord, { username: 'avatarUser', id: '123', avatar: '123' });
821+
this.findUserStub.withArgs('username', testUser.username).returns(testUser);
822+
this.bot.getDiscordAvatar('avatarUser').should.not.equal(null);
802823
});
803824
});

test/fixtures/single-test-config.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,10 @@
1010
"channelMapping": {
1111
"#discord": "#irc channelKey",
1212
"#notinchannel": "#otherIRC",
13-
"1234": "#channelforid"
13+
"1234": "#channelforid",
14+
"#withwebhook": "#ircwebhook"
1415
},
1516
"webhooks": {
16-
"#irc": "https://discordapp.com/api/webhooks/id/token"
17+
"#withwebhook": "https://discordapp.com/api/webhooks/id/token"
1718
}
1819
}

test/stubs/discord-stub.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,11 @@ export default function createDiscordStub(sendMessageStub, findUserStub, findRol
1919
this.users = {
2020
find: findUserStub
2121
};
22+
this.options = {
23+
http: {
24+
host: 'host'
25+
}
26+
};
2227
}
2328

2429
getChannel(key, value) {

test/stubs/webhook-stub.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
/* eslint-disable class-methods-use-this */
2+
export default function createWebhookStub(sendWebhookMessage) {
3+
return class WebhookStub {
4+
constructor(id, token) {
5+
this.id = id;
6+
this.token = token;
7+
}
8+
9+
sendMessage() {
10+
sendWebhookMessage();
11+
return new Promise(() => {});
12+
}
13+
};
14+
}

0 commit comments

Comments
 (0)