Repository: reactiflux/discord-irc
Pull request #147 (2016-11-09 13:59:52)

Mentioning by guild nickname
resolves issue #137

coveralls: 
[![Coverage Status](https://coveralls.io/builds/8742264/badge)](https://coveralls.io/builds/8742264)

Coverage decreased (-1.3%) to 97.222% when pulling **6770cf7d24a81bda1bf3c029a73d3a49dcf7ac10 on DarkSpyro003:server-nicknames** into **0d75a01ab1cd1e913117e3d2ae1e66127d28993b on reactiflux:master**.

coveralls: 
[![Coverage Status](https://coveralls.io/builds/8742696/badge)](https://coveralls.io/builds/8742696)

Coverage decreased (-1.3%) to 97.222% when pulling **e5efeb8098ee3880753562b8e9951d2b84b8b947 on DarkSpyro003:server-nicknames** into **0d75a01ab1cd1e913117e3d2ae1e66127d28993b on reactiflux:master**.

coveralls: 
[![Coverage Status](https://coveralls.io/builds/8742997/badge)](https://coveralls.io/builds/8742997)

Coverage decreased (-1.3%) to 97.222% when pulling **dcd8f4fce92c03f87f58b8b77cf6532338307605 on DarkSpyro003:server-nicknames** into **0d75a01ab1cd1e913117e3d2ae1e66127d28993b on reactiflux:master**.

coveralls: 
[![Coverage Status](https://coveralls.io/builds/8808333/badge)](https://coveralls.io/builds/8808333)

Coverage decreased (-0.5%) to 97.931% when pulling **0d9177c6a570ae03969fa080b725ab9dca1861d6 on DarkSpyro003:server-nicknames** into **0d75a01ab1cd1e913117e3d2ae1e66127d28993b on reactiflux:master**.


Patch:
diff --git a/lib/bot.js b/lib/bot.js
index c115627..f4e7581 100644
--- a/lib/bot.js
+++ b/lib/bot.js
@@ -108,11 +108,21 @@ class Bot {
     });
   }
 
+  static getDiscordNicknameOnServer(user, guild) {
+    const userDetails = guild.members.find('id', user.id);
+    if (userDetails) {
+      return userDetails.nickname || user.username;
+    }
+    return user.username;
+  }
+
   parseText(message) {
-    const text = message.mentions.users.reduce((content, mention) => (
-      content.replace(`<@${mention.id}>`, `@${mention.username}`)
-             .replace(`<@!${mention.id}>`, `@${mention.username}`)
-    ), message.content);
+    const text = message.mentions.users.reduce((content, mention) => {
+      const displayName = Bot.getDiscordNicknameOnServer(mention, message.guild);
+      return content.replace(`<@${mention.id}>`, `@${displayName}`)
+             .replace(`<@!${mention.id}>`, `@${displayName}`)
+             .replace(`<@&${mention.id}>`, `@${displayName}`);
+    }, message.content);
 
     return text
       .replace(/\n|\r\n|\r/g, ' ')
@@ -137,8 +147,8 @@ class Bot {
 
     logger.debug('Channel Mapping', channelName, this.channelMapping[channelName]);
     if (ircChannel) {
-      const userDetails = message.guild.members.find('id', author.id);
-      const nickname = userDetails.nickname || author.username;
+      const fromGuild = message.guild;
+      const nickname = Bot.getDiscordNicknameOnServer(author, fromGuild);
       let text = this.parseText(message);
       let displayUsername = nickname;
       if (this.ircNickColor) {
@@ -183,8 +193,22 @@ class Bot {
       }
 
       const withMentions = text.replace(/@[^\s]+\b/g, (match) => {
-        const user = this.discord.users.find('username', match.substring(1));
-        return user || match;
+        const search = match.substring(1);
+        const guild = discordChannel.guild;
+        const nickUser = guild.members.find('nickname', search);
+        if (nickUser) {
+          return nickUser;
+        }
+
+        const user = this.discord.users.find('username', search);
+        if (user) {
+          const nickname = guild.members.find('id', user.id).nickname;
+          if (!nickname || nickname === search) {
+            return user;
+          }
+        }
+
+        return match;
       });
 
       // Add bold formatting:


Test patch:
diff --git a/test/bot.test.js b/test/bot.test.js
index 19bce46..ca07db1 100644
--- a/test/bot.test.js
+++ b/test/bot.test.js
@@ -250,6 +250,7 @@ describe('Bot', function () {
   });
 
   it('should convert user mentions from discord', function () {
+    const guild = createGuildStub();
     const message = {
       mentions: {
         users: [{
@@ -257,13 +258,15 @@ describe('Bot', function () {
           username: 'testuser'
         }],
       },
-      content: '<@123> hi'
+      content: '<@123> hi',
+      guild
     };
 
     this.bot.parseText(message).should.equal('@testuser hi');
   });
 
   it('should convert user nickname mentions from discord', function () {
+    const guild = createGuildStub();
     const message = {
       mentions: {
         users: [{
@@ -271,7 +274,8 @@ describe('Bot', function () {
           username: 'testuser'
         }],
       },
-      content: '<@!123> hi'
+      content: '<@!123> hi',
+      guild
     };
 
     this.bot.parseText(message).should.equal('@testuser hi');
@@ -289,6 +293,7 @@ describe('Bot', function () {
   it('should convert user mentions from IRC', function () {
     const testUser = new discord.User(this.bot.discord, { username: 'testuser', id: '123' });
     this.findUserStub.withArgs('username', testUser.username).returns(testUser);
+    this.findUserStub.withArgs('id', testUser.id).returns(testUser);
 
     const username = 'ircuser';
     const text = 'Hello, @testuser!';
@@ -310,8 +315,10 @@ describe('Bot', function () {
   it('should convert multiple user mentions from IRC', function () {
     const testUser = new discord.User(this.bot.discord, { username: 'testuser', id: '123' });
     this.findUserStub.withArgs('username', testUser.username).returns(testUser);
+    this.findUserStub.withArgs('id', testUser.id).returns(testUser);
     const anotherUser = new discord.User(this.bot.discord, { username: 'anotheruser', id: '124' });
     this.findUserStub.withArgs('username', anotherUser.username).returns(anotherUser);
+    this.findUserStub.withArgs('id', anotherUser.id).returns(anotherUser);
 
     const username = 'ircuser';
     const text = 'Hello, @testuser and @anotheruser, was our meeting scheduled @5pm?';
@@ -378,4 +385,32 @@ describe('Bot', function () {
     const expected = `<${nickname}> ${text}`;
     ClientStub.prototype.say.should.have.been.calledWith('#irc', expected);
   });
+
+  it('should convert user nickname mentions from IRC', function () {
+    const testUser = new discord.User(this.bot.discord, { username: 'testuser', id: '123', nickname: 'somenickname' });
+    this.findUserStub.withArgs('username', testUser.username).returns(testUser);
+    this.findUserStub.withArgs('nickname', 'somenickname').returns(testUser);
+    this.findUserStub.withArgs('id', testUser.id).returns(testUser);
+
+    const username = 'ircuser';
+    const text = 'Hello, @somenickname!';
+    const expected = `**<${username}>** Hello, <@${testUser.id}>!`;
+
+    this.bot.sendToDiscord(username, '#irc', text);
+    this.sendMessageStub.should.have.been.calledWith(expected);
+  });
+
+  it('should not convert username mentions from IRC if nickname differs', function () {
+    const testUser = new discord.User(this.bot.discord, { username: 'testuser', id: '123', nickname: 'somenickname' });
+    this.findUserStub.withArgs('username', testUser.username).returns(testUser);
+    this.findUserStub.withArgs('nickname', 'somenickname').returns(testUser);
+    this.findUserStub.withArgs('id', testUser.id).returns(testUser);
+
+    const username = 'ircuser';
+    const text = 'Hello, @username!';
+    const expected = `**<${username}>** Hello, @username!`;
+
+    this.bot.sendToDiscord(username, '#irc', text);
+    this.sendMessageStub.should.have.been.calledWith(expected);
+  });
 });
diff --git a/test/stubs/discord-stub.js b/test/stubs/discord-stub.js
index 2250294..fdf97d3 100644
--- a/test/stubs/discord-stub.js
+++ b/test/stubs/discord-stub.js
@@ -26,7 +26,12 @@ export default function createDiscordStub(sendMessageStub, findUserStub) {
       return {
         name: 'discord',
         id: 1234,
-        sendMessage: sendMessageStub
+        sendMessage: sendMessageStub,
+        guild: {
+          members: {
+            find: findUserStub
+          }
+        }
       };
     }