@@ -6,6 +6,10 @@ import { ConfigurationError } from './errors';
6
6
import { validateChannelMapping } from './validators' ;
7
7
import { formatFromDiscordToIRC , formatFromIRCToDiscord } from './formatting' ;
8
8
9
+ // Usernames need to be between 2 and 32 characters for webhooks:
10
+ const USERNAME_MIN_LENGTH = 2 ;
11
+ const USERNAME_MAX_LENGTH = 32 ;
12
+
9
13
const REQUIRED_FIELDS = [ 'server' , 'nickname' , 'channelMapping' , 'discordToken' ] ;
10
14
const NICK_COLORS = [ 'light_blue' , 'dark_blue' , 'light_red' , 'dark_red' , 'light_green' ,
11
15
'dark_green' , 'magenta' , 'light_magenta' , 'orange' , 'yellow' , 'cyan' , 'light_cyan' ] ;
@@ -471,12 +475,14 @@ class Bot {
471
475
// skips usernames including spaces for ease (they cannot include hashes)
472
476
// checks case insensitively as Discord does
473
477
const user = guild . members . find ( x =>
474
- Bot . caseComp ( x . user . username , username . toUpperCase ( ) )
478
+ Bot . caseComp ( x . user . username , username )
475
479
&& x . user . discriminator === discriminator ) ;
476
480
if ( user ) return user ;
477
481
478
482
return match ;
479
- } ) . replace ( / @ ( [ ^ \s ] + ) / g, ( match , reference ) => {
483
+ } ) . replace ( / ^ ( [ ^ @ \s : , ] + ) [: ,] | @ ( [ ^ \s ] + ) / g, ( match , startRef , atRef ) => {
484
+ const reference = startRef || atRef ;
485
+
480
486
// this preliminary stuff is ultimately unnecessary
481
487
// but might save time over later more complicated calculations
482
488
// @nickname => mention, case insensitively
@@ -535,15 +541,24 @@ class Bot {
535
541
if ( emoji ) return emoji ;
536
542
537
543
return match ;
544
+ } ) . replace ( / # ( [ ^ \s # @ ' ! ? , . ] + ) / g, ( match , channelName ) => {
545
+ // channel names can't contain spaces, #, @, ', !, ?, , or .
546
+ // (based on brief testing. they also can't contain some other symbols,
547
+ // but these seem likely to be common around channel references)
548
+
549
+ // discord matches channel names case insensitively
550
+ const chan = guild . channels . find ( x => Bot . caseComp ( x . name , channelName ) ) ;
551
+ return chan || match ;
538
552
} ) ;
539
553
540
554
// Webhooks first
541
555
const webhook = this . findWebhook ( channel ) ;
542
556
if ( webhook ) {
543
557
logger . debug ( 'Sending message to Discord via webhook' , withMentions , channel , '->' , `#${ discordChannel . name } ` ) ;
544
558
const avatarURL = this . getDiscordAvatar ( author , channel ) ;
559
+ const username = _ . padEnd ( author . substring ( 0 , USERNAME_MAX_LENGTH ) , USERNAME_MIN_LENGTH , '_' ) ;
545
560
webhook . client . sendMessage ( withMentions , {
546
- username : author ,
561
+ username,
547
562
text,
548
563
avatarURL
549
564
} ) . catch ( logger . error ) ;
0 commit comments