Skip to content

Commit 664e38c

Browse files
committed
Merge pull request #31 from rce/irc-to-discord-mentions
Mention discord users in messages sent from IRC
2 parents ef2a267 + b41299f commit 664e38c

File tree

2 files changed

+43
-1
lines changed

2 files changed

+43
-1
lines changed

lib/bot.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -170,8 +170,13 @@ class Bot {
170170
return;
171171
}
172172

173+
const withMentions = text.replace(/@[^\s]+\b/g, match => {
174+
const user = this.discord.users.get('username', match.substring(1));
175+
return user ? user.mention() : match;
176+
});
177+
173178
// Add bold formatting:
174-
const withAuthor = `**<${author}>** ${text}`;
179+
const withAuthor = `**<${author}>** ${withMentions}`;
175180
logger.debug('Sending message to Discord', withAuthor, channel, '->', discordChannelName);
176181
this.discord.sendMessage(discordChannel, withAuthor);
177182
}

test/bot.test.js

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ describe('Bot', function() {
2727
irc.Client = ClientStub;
2828
discord.Client = DiscordStub;
2929
DiscordStub.prototype.sendMessage = sandbox.stub();
30+
DiscordStub.prototype.users = { get: sandbox.stub() };
3031
ClientStub.prototype.say = sandbox.stub();
3132
ClientStub.prototype.send = sandbox.stub();
3233
ClientStub.prototype.join = sandbox.stub();
@@ -169,6 +170,42 @@ describe('Bot', function() {
169170
this.bot.parseText(message).should.equal('@testuser hi');
170171
});
171172

173+
it('should convert user mentions from IRC', function() {
174+
const testuser = new discord.User({ username: 'testuser', id: '123' }, this.bot.discord);
175+
this.bot.discord.users.get.withArgs('username', testuser.username).returns(testuser);
176+
177+
const username = 'ircuser';
178+
const text = 'Hello, @testuser!';
179+
const expected = `**<${username}>** Hello, <@${testuser.id}>!`;
180+
181+
this.bot.sendToDiscord(username, '#irc', text);
182+
DiscordStub.prototype.sendMessage.should.have.been.calledWith(discordChannel, expected);
183+
});
184+
185+
it('should not convert user mentions from IRC if such user does not exist', function() {
186+
const username = 'ircuser';
187+
const text = 'See you there @5pm';
188+
const expected = `**<${username}>** See you there @5pm`;
189+
190+
this.bot.sendToDiscord(username, '#irc', text);
191+
DiscordStub.prototype.sendMessage.should.have.been.calledWith(discordChannel, expected);
192+
});
193+
194+
it('should convert multiple user mentions from IRC', function() {
195+
const testuser = new discord.User({ username: 'testuser', id: '123' }, this.bot.discord);
196+
this.bot.discord.users.get.withArgs('username', testuser.username).returns(testuser);
197+
const anotheruser = new discord.User({ username: 'anotheruser', id: '124' }, this.bot.discord);
198+
this.bot.discord.users.get.withArgs('username', anotheruser.username).returns(anotheruser);
199+
200+
const username = 'ircuser';
201+
const text = 'Hello, @testuser and @anotheruser, was our meeting scheduled @5pm?';
202+
const expected = `**<${username}>** Hello, <@${testuser.id}> and <@${anotheruser.id}>,` +
203+
` was our meeting scheduled @5pm?`;
204+
205+
this.bot.sendToDiscord(username, '#irc', text);
206+
DiscordStub.prototype.sendMessage.should.have.been.calledWith(discordChannel, expected);
207+
});
208+
172209
it('should convert newlines from discord', function() {
173210
const message = {
174211
mentions: [],

0 commit comments

Comments
 (0)