@@ -34,6 +34,8 @@ class Bot {
34
34
this . commandCharacters = options . commandCharacters || [ ] ;
35
35
this . ircNickColor = options . ircNickColor !== false ; // default to true
36
36
this . channels = _ . values ( options . channelMapping ) ;
37
+ this . ircStatusNotices = options . ircStatusNotices ;
38
+ this . announceSelfJoin = options . announceSelfJoin ;
37
39
38
40
this . format = options . format || { } ;
39
41
// "{$keyName}" => "variableValue"
@@ -120,6 +122,24 @@ class Bot {
120
122
this . sendToDiscord ( author , to , `*${ text } *` ) ;
121
123
} ) ;
122
124
125
+ this . ircClient . on ( 'join' , ( channel , nick ) => {
126
+ if ( ! this . ircStatusNotices ) return ;
127
+ if ( nick === this . nickname && ! this . announceSelfJoin ) return ;
128
+ this . sendExactToDiscord ( channel , `*${ nick } * has joined the channel` ) ;
129
+ } ) ;
130
+
131
+ this . ircClient . on ( 'part' , ( channel , nick , reason ) => {
132
+ if ( ! this . ircStatusNotices || nick === this . nickname ) return ;
133
+ this . sendExactToDiscord ( channel , `*${ nick } * has left the channel (${ reason } )` ) ;
134
+ } ) ;
135
+
136
+ this . ircClient . on ( 'quit' , ( nick , reason , channels ) => {
137
+ if ( ! this . ircStatusNotices || nick === this . nickname ) return ;
138
+ channels . forEach ( ( channel ) => {
139
+ this . sendExactToDiscord ( channel , `*${ nick } * has quit (${ reason } )` ) ;
140
+ } ) ;
141
+ } ) ;
142
+
123
143
this . ircClient . on ( 'action' , ( author , to , text ) => {
124
144
this . sendToDiscord ( author , to , `_${ text } _` ) ;
125
145
} ) ;
@@ -236,8 +256,8 @@ class Bot {
236
256
}
237
257
}
238
258
239
- sendToDiscord ( author , channel , text ) {
240
- const discordChannelName = this . invertedMapping [ channel . toLowerCase ( ) ] ;
259
+ findDiscordChannel ( ircChannel ) {
260
+ const discordChannelName = this . invertedMapping [ ircChannel . toLowerCase ( ) ] ;
241
261
if ( discordChannelName ) {
242
262
// #channel -> channel before retrieving and select only text channels:
243
263
const discordChannel = discordChannelName . startsWith ( '#' ) ? this . discord . channels
@@ -247,47 +267,63 @@ class Bot {
247
267
if ( ! discordChannel ) {
248
268
logger . info ( 'Tried to send a message to a channel the bot isn\'t in: ' ,
249
269
discordChannelName ) ;
250
- return ;
270
+ return null ;
251
271
}
272
+ return discordChannel ;
273
+ }
274
+ return null ;
275
+ }
252
276
253
- // Convert text formatting (bold, italics, underscore)
254
- const withFormat = formatFromIRCToDiscord ( text ) ;
277
+ sendToDiscord ( author , channel , text ) {
278
+ const discordChannel = this . findDiscordChannel ( channel ) ;
279
+ if ( ! discordChannel ) return ;
280
+
281
+ // Convert text formatting (bold, italics, underscore)
282
+ const withFormat = formatFromIRCToDiscord ( text ) ;
283
+
284
+ const withMentions = withFormat . replace ( / @ [ ^ \s ] + \b / g, ( match ) => {
285
+ const search = match . substring ( 1 ) ;
286
+ const guild = discordChannel . guild ;
287
+ const nickUser = guild . members . find ( 'nickname' , search ) ;
288
+ if ( nickUser ) {
289
+ return nickUser ;
290
+ }
255
291
256
- const withMentions = withFormat . replace ( / @ [ ^ \s ] + \b / g, ( match ) => {
257
- const search = match . substring ( 1 ) ;
258
- const guild = discordChannel . guild ;
259
- const nickUser = guild . members . find ( 'nickname' , search ) ;
260
- if ( nickUser ) {
261
- return nickUser ;
262
- }
292
+ const user = this . discord . users . find ( 'username' , search ) ;
293
+ if ( user ) {
294
+ return user ;
295
+ }
263
296
264
- const user = this . discord . users . find ( 'username ' , search ) ;
265
- if ( user ) {
266
- return user ;
267
- }
297
+ const role = guild . roles . find ( 'name ' , search ) ;
298
+ if ( role && role . mentionable ) {
299
+ return role ;
300
+ }
268
301
269
- const role = guild . roles . find ( 'name' , search ) ;
270
- if ( role && role . mentionable ) {
271
- return role ;
272
- }
302
+ return match ;
303
+ } ) ;
273
304
274
- return match ;
275
- } ) ;
305
+ const patternMap = {
306
+ author,
307
+ text : withFormat ,
308
+ withMentions,
309
+ discordChannel : `#${ discordChannel . name } ` ,
310
+ ircChannel : channel
311
+ } ;
276
312
277
- const patternMap = {
278
- author,
279
- text : withFormat ,
280
- withMentions,
281
- discordChannel : `#${ discordChannel . name } ` ,
282
- ircChannel : channel
283
- } ;
313
+ // Add bold formatting:
314
+ // Use custom formatting from config / default formatting with bold author
315
+ const withAuthor = Bot . substitutePattern ( this . formatDiscord , patternMap ) ;
316
+ logger . debug ( 'Sending message to Discord' , withAuthor , channel , '->' , `#${ discordChannel . name } ` ) ;
317
+ discordChannel . sendMessage ( withAuthor ) ;
318
+ }
284
319
285
- // Add bold formatting:
286
- // Use custom formatting from config / default formatting with bold author
287
- const withAuthor = Bot . substitutePattern ( this . formatDiscord , patternMap ) ;
288
- logger . debug ( 'Sending message to Discord' , withAuthor , channel , '->' , discordChannelName ) ;
289
- discordChannel . sendMessage ( withAuthor ) ;
290
- }
320
+ /* Sends a message to Discord exactly as it appears */
321
+ sendExactToDiscord ( channel , text ) {
322
+ const discordChannel = this . findDiscordChannel ( channel ) ;
323
+ if ( ! discordChannel ) return ;
324
+
325
+ logger . debug ( 'Sending special message to Discord' , text , channel , '->' , `#${ discordChannel . name } ` ) ;
326
+ discordChannel . sendMessage ( text ) ;
291
327
}
292
328
}
293
329
0 commit comments