Skip to content

Commit 1daf62e

Browse files
authored
Merge pull request #203 from Throne3d/add/role-mentions
Parse role mentions appropriately, as with channel and user mentions
2 parents 8c37393 + b30c4c3 commit 1daf62e

File tree

3 files changed

+94
-4
lines changed

3 files changed

+94
-4
lines changed

lib/bot.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,11 @@ class Bot {
142142
if (channel) return `#${channel.name}`;
143143
return '#deleted-channel';
144144
})
145+
.replace(/<@&(\d+)>/g, (match, roleId) => {
146+
const role = message.guild.roles.get(roleId);
147+
if (role) return `@${role.name}`;
148+
return '@deleted-role';
149+
})
145150
.replace(/<(:\w+:)\d+>/g, (match, emoteName) => emoteName);
146151
}
147152

@@ -221,6 +226,11 @@ class Bot {
221226
}
222227
}
223228

229+
const role = guild.roles.find('name', search);
230+
if (role && role.mentionable) {
231+
return role;
232+
}
233+
224234
return match;
225235
});
226236

test/bot.test.js

Lines changed: 79 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,9 @@ describe('Bot', function () {
2525
sandbox.stub(logger, 'error');
2626
this.sendMessageStub = sandbox.stub();
2727
this.findUserStub = sandbox.stub();
28+
this.findRoleStub = sandbox.stub();
2829
irc.Client = ClientStub;
29-
discord.Client = createDiscordStub(this.sendMessageStub, this.findUserStub);
30+
discord.Client = createDiscordStub(this.sendMessageStub, this.findUserStub, this.findRoleStub);
3031
ClientStub.prototype.say = sandbox.stub();
3132
ClientStub.prototype.send = sandbox.stub();
3233
ClientStub.prototype.join = sandbox.stub();
@@ -44,11 +45,14 @@ describe('Bot', function () {
4445
return attachments;
4546
};
4647

47-
const createGuildStub = (nickname = null) => ({
48+
const createGuildStub = (findRoleStub, nickname = null) => ({
4849
members: {
4950
get() {
5051
return { nickname };
5152
}
53+
},
54+
roles: {
55+
get: findRoleStub
5256
}
5357
});
5458

@@ -446,7 +450,7 @@ describe('Bot', function () {
446450
const newConfig = { ...config, ircNickColor: false };
447451
const bot = new Bot(newConfig);
448452
const nickname = 'discord-nickname';
449-
const guild = createGuildStub(nickname);
453+
const guild = createGuildStub(null, nickname);
450454
bot.connect();
451455
const message = {
452456
content: text,
@@ -493,4 +497,76 @@ describe('Bot', function () {
493497
this.bot.sendToDiscord(username, '#irc', text);
494498
this.sendMessageStub.should.have.been.calledWith(expected);
495499
});
500+
501+
it('should convert role mentions from discord', function () {
502+
const testRole = new discord.Role(this.bot.discord, { name: 'example-role', id: '12345' });
503+
this.findRoleStub.withArgs('name', 'example-role').returns(testRole);
504+
this.findRoleStub.withArgs('12345').returns(testRole);
505+
506+
const text = '<@&12345>';
507+
const guild = createGuildStub(this.findRoleStub);
508+
const message = {
509+
content: text,
510+
mentions: { users: [] },
511+
channel: {
512+
name: 'discord'
513+
},
514+
author: {
515+
username: 'test',
516+
id: 'not bot id'
517+
},
518+
guild
519+
};
520+
521+
this.bot.parseText(message).should.equal('@example-role');
522+
});
523+
524+
it('should use @deleted-role when referenced role fails to exist', function () {
525+
const testRole = new discord.Role(this.bot.discord, { name: 'example-role', id: '12345' });
526+
this.findRoleStub.withArgs('12345').returns(testRole);
527+
528+
const text = '<@&12346>';
529+
const guild = createGuildStub(this.findRoleStub);
530+
const message = {
531+
content: text,
532+
mentions: { users: [] },
533+
channel: {
534+
name: 'discord'
535+
},
536+
author: {
537+
username: 'test',
538+
id: 'not bot id'
539+
},
540+
guild
541+
};
542+
543+
// Discord displays "@deleted-role" if role doesn't exist (e.g. <@&12346>)
544+
this.bot.parseText(message).should.equal('@deleted-role');
545+
});
546+
547+
it('should convert role mentions from IRC if role mentionable', function () {
548+
const testRole = new discord.Role(this.bot.discord, { name: 'example-role', id: '12345', mentionable: true });
549+
this.findRoleStub.withArgs('name', 'example-role').returns(testRole);
550+
this.findRoleStub.withArgs('12345').returns(testRole);
551+
552+
const username = 'ircuser';
553+
const text = 'Hello, @example-role!';
554+
const expected = `**<${username}>** Hello, <@&${testRole.id}>!`;
555+
556+
this.bot.sendToDiscord(username, '#irc', text);
557+
this.sendMessageStub.should.have.been.calledWith(expected);
558+
});
559+
560+
it('should not convert role mentions from IRC if role not mentionable', function () {
561+
const testRole = new discord.Role(this.bot.discord, { name: 'example-role', id: '12345' });
562+
this.findRoleStub.withArgs('name', 'example-role').returns(testRole);
563+
this.findRoleStub.withArgs('12345').returns(testRole);
564+
565+
const username = 'ircuser';
566+
const text = 'Hello, @example-role!';
567+
const expected = `**<${username}>** Hello, @example-role!`;
568+
569+
this.bot.sendToDiscord(username, '#irc', text);
570+
this.sendMessageStub.should.have.been.calledWith(expected);
571+
});
496572
});

test/stubs/discord-stub.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
import events from 'events';
33
import sinon from 'sinon';
44

5-
export default function createDiscordStub(sendMessageStub, findUserStub) {
5+
export default function createDiscordStub(sendMessageStub, findUserStub, findRoleStub) {
66
return class DiscordStub extends events.EventEmitter {
77
constructor() {
88
super();
@@ -32,6 +32,10 @@ export default function createDiscordStub(sendMessageStub, findUserStub) {
3232
members: {
3333
find: findUserStub,
3434
get: findUserStub
35+
},
36+
roles: {
37+
find: findRoleStub,
38+
get: findRoleStub
3539
}
3640
}
3741
};

0 commit comments

Comments
 (0)