Repository: reactiflux/discord-irc (js)

Issue:
Mentions from IRC -> Discord
I am using a framework for my Discord Bot, and it has a regex to autoconvert `@username` to a mention. Passing the code for your ease of implementation.

```js
.replace(/@[^\s]+\b/g, (o) => {
    var member = message.channel.server.members.get("username", o.substring(1));
    return member ? member.mention() : o;
});
```

Pull request:
Mention discord users in messages sent from IRC
This was requested by my friends. Also closes #30.

Hopefully I'll have the time to get more contributions coming!

Patch:
diff --git a/lib/bot.js b/lib/bot.js
index aa24c73..619777e 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);
     }


Test patch:
diff --git a/test/bot.test.js b/test/bot.test.js
index 5707b03..3b4f369 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();
@@ -169,6 +170,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: [],