Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion lib/bot.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
37 changes: 37 additions & 0 deletions test/bot.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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: [],
Expand Down