@@ -133,6 +133,12 @@ class Bot {
133
133
this . attachListeners ( ) ;
134
134
}
135
135
136
+ disconnect ( ) {
137
+ this . ircClient . disconnect ( ) ;
138
+ this . discord . destroy ( ) ;
139
+ Object . values ( this . webhooks ) . forEach ( x => x . client . destroy ( ) ) ;
140
+ }
141
+
136
142
attachListeners ( ) {
137
143
this . discord . on ( 'ready' , ( ) => {
138
144
logger . info ( 'Connected to Discord' ) ;
@@ -258,7 +264,7 @@ class Bot {
258
264
259
265
static getDiscordNicknameOnServer ( user , guild ) {
260
266
if ( guild ) {
261
- const userDetails = guild . members . get ( user . id ) ;
267
+ const userDetails = guild . members . cache . get ( user . id ) ;
262
268
if ( userDetails ) {
263
269
return userDetails . nickname || user . username ;
264
270
}
@@ -277,12 +283,12 @@ class Bot {
277
283
return text
278
284
. replace ( / \n | \r \n | \r / g, ' ' )
279
285
. replace ( / < # ( \d + ) > / g, ( match , channelId ) => {
280
- const channel = this . discord . channels . get ( channelId ) ;
286
+ const channel = this . discord . channels . cache . get ( channelId ) ;
281
287
if ( channel ) return `#${ channel . name } ` ;
282
288
return '#deleted-channel' ;
283
289
} )
284
290
. replace ( / < @ & ( \d + ) > / g, ( match , roleId ) => {
285
- const role = message . guild . roles . get ( roleId ) ;
291
+ const role = message . guild . roles . cache . get ( roleId ) ;
286
292
if ( role ) return `@${ role . name } ` ;
287
293
return '@deleted-role' ;
288
294
} )
@@ -391,10 +397,10 @@ class Bot {
391
397
// #channel -> channel before retrieving and select only text channels:
392
398
let discordChannel = null ;
393
399
394
- if ( this . discord . channels . has ( discordChannelName ) ) {
395
- discordChannel = this . discord . channels . get ( discordChannelName ) ;
400
+ if ( this . discord . channels . cache . has ( discordChannelName ) ) {
401
+ discordChannel = this . discord . channels . cache . get ( discordChannelName ) ;
396
402
} else if ( discordChannelName . startsWith ( '#' ) ) {
397
- discordChannel = this . discord . channels
403
+ discordChannel = this . discord . channels . cache
398
404
. filter ( c => c . type === 'text' )
399
405
. find ( c => c . name === discordChannelName . slice ( 1 ) ) ;
400
406
}
@@ -417,7 +423,7 @@ class Bot {
417
423
}
418
424
419
425
getDiscordAvatar ( nick , channel ) {
420
- const guildMembers = this . findDiscordChannel ( channel ) . guild . members ;
426
+ const guildMembers = this . findDiscordChannel ( channel ) . guild . members . cache ;
421
427
const findByNicknameOrUsername = caseSensitive =>
422
428
( member ) => {
423
429
if ( caseSensitive ) {
@@ -438,8 +444,8 @@ class Bot {
438
444
439
445
// No matching user or more than one => default avatar
440
446
if ( users && users . size === 1 ) {
441
- const url = users . first ( ) . user . avatarURL ;
442
- if ( url ) return url . replace ( / \? s i z e = \d { 1 , } $ / , '?size=128' ) ;
447
+ const url = users . first ( ) . user . avatarURL ( { size : 128 , format : 'png' } ) ;
448
+ if ( url ) return url ;
443
449
}
444
450
445
451
// If there isn't a URL format, don't send an avatar at all
@@ -498,7 +504,7 @@ class Bot {
498
504
// @username #1234 => mention
499
505
// skips usernames including spaces for ease (they cannot include hashes)
500
506
// checks case insensitively as Discord does
501
- const user = guild . members . find ( x =>
507
+ const user = guild . members . cache . find ( x =>
502
508
Bot . caseComp ( x . user . username , username )
503
509
&& x . user . discriminator === discriminator ) ;
504
510
if ( user ) return user ;
@@ -510,16 +516,16 @@ class Bot {
510
516
// this preliminary stuff is ultimately unnecessary
511
517
// but might save time over later more complicated calculations
512
518
// @nickname => mention, case insensitively
513
- const nickUser = guild . members . find ( x =>
514
- x . nickname !== null && Bot . caseComp ( x . nickname , reference ) ) ;
519
+ const nickUser = guild . members . cache . find ( x =>
520
+ x . nickname && Bot . caseComp ( x . nickname , reference ) ) ;
515
521
if ( nickUser ) return nickUser ;
516
522
517
523
// @username => mention, case insensitively
518
- const user = guild . members . find ( x => Bot . caseComp ( x . user . username , reference ) ) ;
524
+ const user = guild . members . cache . find ( x => Bot . caseComp ( x . user . username , reference ) ) ;
519
525
if ( user ) return user ;
520
526
521
527
// @role => mention, case insensitively
522
- const role = guild . roles . find ( x => x . mentionable && Bot . caseComp ( x . name , reference ) ) ;
528
+ const role = guild . roles . cache . find ( x => x . mentionable && Bot . caseComp ( x . name , reference ) ) ;
523
529
if ( role ) return role ;
524
530
525
531
// No match found checking the whole word. Check for partial matches now instead.
@@ -544,13 +550,13 @@ class Bot {
544
550
} ;
545
551
546
552
// check users by username and nickname
547
- guild . members . forEach ( ( member ) => {
553
+ guild . members . cache . forEach ( ( member ) => {
548
554
checkMatch ( member . user . username , member ) ;
549
- if ( bestMatch === member || member . nickname === null ) return ;
555
+ if ( bestMatch === member || ! member . nickname ) return ;
550
556
checkMatch ( member . nickname , member ) ;
551
557
} ) ;
552
558
// check mentionable roles by visible name
553
- guild . roles . forEach ( ( member ) => {
559
+ guild . roles . cache . forEach ( ( member ) => {
554
560
if ( ! member . mentionable ) return ;
555
561
checkMatch ( member . name , member ) ;
556
562
} ) ;
@@ -561,7 +567,7 @@ class Bot {
561
567
return match ;
562
568
} ) . replace ( / : ( \w + ) : / g, ( match , ident ) => {
563
569
// :emoji: => mention, case sensitively
564
- const emoji = guild . emojis . find ( x => x . name === ident && x . requiresColons ) ;
570
+ const emoji = guild . emojis . cache . find ( x => x . name === ident && x . requiresColons ) ;
565
571
if ( emoji ) return emoji ;
566
572
567
573
return match ;
@@ -571,7 +577,7 @@ class Bot {
571
577
// but these seem likely to be common around channel references)
572
578
573
579
// discord matches channel names case insensitively
574
- const chan = guild . channels . find ( x => Bot . caseComp ( x . name , channelName ) ) ;
580
+ const chan = guild . channels . cache . find ( x => Bot . caseComp ( x . name , channelName ) ) ;
575
581
return chan || match ;
576
582
} ) ;
577
583
@@ -586,11 +592,10 @@ class Bot {
586
592
}
587
593
const avatarURL = this . getDiscordAvatar ( author , channel ) ;
588
594
const username = _ . padEnd ( author . substring ( 0 , USERNAME_MAX_LENGTH ) , USERNAME_MIN_LENGTH , '_' ) ;
589
- webhook . client . sendMessage ( withMentions , {
595
+ webhook . client . send ( withMentions , {
590
596
username,
591
- text,
592
597
avatarURL,
593
- disableEveryone : ! canPingEveryone ,
598
+ disableMentions : canPingEveryone ? 'none' : 'everyone' ,
594
599
} ) . catch ( logger . error ) ;
595
600
return ;
596
601
}
0 commit comments