From 94032d268d9d419eb3dc3059b0d6228baea2b3a6 Mon Sep 17 00:00:00 2001 From: Henry Heikkinen Date: Thu, 4 Feb 2016 09:33:24 +0200 Subject: [PATCH 1/2] Mention discord users in messages sent from IRC --- lib/bot.js | 7 ++++++- test/bot.test.js | 36 ++++++++++++++++++++++++++++++++++++ test/stubs/discord-stub.js | 3 +++ 3 files changed, 45 insertions(+), 1 deletion(-) diff --git a/lib/bot.js b/lib/bot.js index aa24c73b..619777e0 100644 --- a/lib/bot.js +++ b/lib/bot.js @@ -170,8 +170,13 @@ class Bot { return; } + const withMentions = text.replace(/@[^\s]+\b/g, match => { + const user = this.discord.users.get('username', match.substring(1)); + return user ? user.mention() : match; + }); + // Add bold formatting: - const withAuthor = `**<${author}>** ${text}`; + const withAuthor = `**<${author}>** ${withMentions}`; logger.debug('Sending message to Discord', withAuthor, channel, '->', discordChannelName); this.discord.sendMessage(discordChannel, withAuthor); } diff --git a/test/bot.test.js b/test/bot.test.js index 5707b036..fd8e63c6 100644 --- a/test/bot.test.js +++ b/test/bot.test.js @@ -169,6 +169,42 @@ describe('Bot', function() { this.bot.parseText(message).should.equal('@testuser hi'); }); + it('should convert user mentions from IRC', function() { + const testuser = new discord.User({ username: 'testuser', id: '123' }, this.bot.discord); + this.bot.discord.users.get.withArgs('username', testuser.username).returns(testuser); + + const username = 'ircuser'; + const text = 'Hello, @testuser!'; + const expected = `**<${username}>** Hello, <@${testuser.id}>!`; + + this.bot.sendToDiscord(username, '#irc', text); + DiscordStub.prototype.sendMessage.should.have.been.calledWith(discordChannel, expected); + }); + + it('should not convert user mentions from IRC if such user does not exist', function() { + const username = 'ircuser'; + const text = 'See you there @5pm'; + const expected = `**<${username}>** See you there @5pm`; + + this.bot.sendToDiscord(username, '#irc', text); + DiscordStub.prototype.sendMessage.should.have.been.calledWith(discordChannel, expected); + }); + + it('should convert multiple user mentions from IRC', function() { + const testuser = new discord.User({ username: 'testuser', id: '123' }, this.bot.discord); + this.bot.discord.users.get.withArgs('username', testuser.username).returns(testuser); + const anotheruser = new discord.User({ username: 'anotheruser', id: '124' }, this.bot.discord); + this.bot.discord.users.get.withArgs('username', anotheruser.username).returns(anotheruser); + + const username = 'ircuser'; + const text = 'Hello, @testuser and @anotheruser, was our meeting scheduled @5pm?'; + const expected = `**<${username}>** Hello, <@${testuser.id}> and <@${anotheruser.id}>,` + + ` was our meeting scheduled @5pm?`; + + this.bot.sendToDiscord(username, '#irc', text); + DiscordStub.prototype.sendMessage.should.have.been.calledWith(discordChannel, expected); + }); + it('should convert newlines from discord', function() { const message = { mentions: [], diff --git a/test/stubs/discord-stub.js b/test/stubs/discord-stub.js index dd8bc41f..fa0c2e14 100644 --- a/test/stubs/discord-stub.js +++ b/test/stubs/discord-stub.js @@ -12,6 +12,9 @@ export function getChannel(key, value) { class DiscordStub extends events.EventEmitter { constructor() { super(); + this.users = { + get: sinon.stub() + }; this.user = { id: 'testid' }; From b41299f6b8cd29438f0db1ad7eac5e686b280d22 Mon Sep 17 00:00:00 2001 From: Henry Heikkinen Date: Tue, 23 Feb 2016 21:51:38 +0200 Subject: [PATCH 2/2] Use Sinon sandbox stub --- test/bot.test.js | 1 + test/stubs/discord-stub.js | 3 --- 2 files changed, 1 insertion(+), 3 deletions(-) diff --git a/test/bot.test.js b/test/bot.test.js index fd8e63c6..3b4f369f 100644 --- a/test/bot.test.js +++ b/test/bot.test.js @@ -27,6 +27,7 @@ describe('Bot', function() { irc.Client = ClientStub; discord.Client = DiscordStub; DiscordStub.prototype.sendMessage = sandbox.stub(); + DiscordStub.prototype.users = { get: sandbox.stub() }; ClientStub.prototype.say = sandbox.stub(); ClientStub.prototype.send = sandbox.stub(); ClientStub.prototype.join = sandbox.stub(); diff --git a/test/stubs/discord-stub.js b/test/stubs/discord-stub.js index fa0c2e14..dd8bc41f 100644 --- a/test/stubs/discord-stub.js +++ b/test/stubs/discord-stub.js @@ -12,9 +12,6 @@ export function getChannel(key, value) { class DiscordStub extends events.EventEmitter { constructor() { super(); - this.users = { - get: sinon.stub() - }; this.user = { id: 'testid' };