Skip to content

Commit a1dfdf6

Browse files
committed
Match roles in parseText to display @-roles neatly in IRC
1 parent 26626e5 commit a1dfdf6

File tree

2 files changed

+55
-0
lines changed

2 files changed

+55
-0
lines changed

lib/bot.js

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

test/bot.test.js

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,11 +44,22 @@ describe('Bot', function () {
4444
return attachments;
4545
};
4646

47+
const getRole = (key, value) => {
48+
if (key !== '12345' && value === undefined) return null;
49+
return {
50+
name: 'example-role',
51+
id: 12345
52+
};
53+
};
54+
4755
const createGuildStub = (nickname = null) => ({
4856
members: {
4957
get() {
5058
return { nickname };
5159
}
60+
},
61+
roles: {
62+
get: getRole
5263
}
5364
});
5465

@@ -413,4 +424,43 @@ describe('Bot', function () {
413424
this.bot.sendToDiscord(username, '#irc', text);
414425
this.sendMessageStub.should.have.been.calledWith(expected);
415426
});
427+
428+
it('should convert role mentions from discord', function () {
429+
const text = '<@&12345>';
430+
const guild = createGuildStub();
431+
const message = {
432+
content: text,
433+
mentions: { users: [] },
434+
channel: {
435+
name: 'discord'
436+
},
437+
author: {
438+
username: 'test',
439+
id: 'not bot id'
440+
},
441+
guild
442+
};
443+
444+
this.bot.parseText(message).should.equal('@example-role');
445+
});
446+
447+
it('should use @deleted-role when referenced role fails to exist', function () {
448+
const text = '<@&12346>';
449+
const guild = createGuildStub();
450+
const message = {
451+
content: text,
452+
mentions: { users: [] },
453+
channel: {
454+
name: 'discord'
455+
},
456+
author: {
457+
username: 'test',
458+
id: 'not bot id'
459+
},
460+
guild
461+
};
462+
463+
// Discord displays "@deleted-role" if role doesn't exist (e.g. <@&12346>)
464+
this.bot.parseText(message).should.equal('@deleted-role');
465+
});
416466
});

0 commit comments

Comments
 (0)