@@ -25,8 +25,9 @@ describe('Bot', function () {
25
25
sandbox . stub ( logger , 'error' ) ;
26
26
this . sendMessageStub = sandbox . stub ( ) ;
27
27
this . findUserStub = sandbox . stub ( ) ;
28
+ this . findRoleStub = sandbox . stub ( ) ;
28
29
irc . Client = ClientStub ;
29
- discord . Client = createDiscordStub ( this . sendMessageStub , this . findUserStub ) ;
30
+ discord . Client = createDiscordStub ( this . sendMessageStub , this . findUserStub , this . findRoleStub ) ;
30
31
ClientStub . prototype . say = sandbox . stub ( ) ;
31
32
ClientStub . prototype . send = sandbox . stub ( ) ;
32
33
ClientStub . prototype . join = sandbox . stub ( ) ;
@@ -44,11 +45,14 @@ describe('Bot', function () {
44
45
return attachments ;
45
46
} ;
46
47
47
- const createGuildStub = ( nickname = null ) => ( {
48
+ const createGuildStub = ( findRoleStub , nickname = null ) => ( {
48
49
members : {
49
50
get ( ) {
50
51
return { nickname } ;
51
52
}
53
+ } ,
54
+ roles : {
55
+ get : findRoleStub
52
56
}
53
57
} ) ;
54
58
@@ -446,7 +450,7 @@ describe('Bot', function () {
446
450
const newConfig = { ...config , ircNickColor : false } ;
447
451
const bot = new Bot ( newConfig ) ;
448
452
const nickname = 'discord-nickname' ;
449
- const guild = createGuildStub ( nickname ) ;
453
+ const guild = createGuildStub ( null , nickname ) ;
450
454
bot . connect ( ) ;
451
455
const message = {
452
456
content : text ,
@@ -493,4 +497,76 @@ describe('Bot', function () {
493
497
this . bot . sendToDiscord ( username , '#irc' , text ) ;
494
498
this . sendMessageStub . should . have . been . calledWith ( expected ) ;
495
499
} ) ;
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
+ } ) ;
496
572
} ) ;
0 commit comments