@@ -209,8 +209,8 @@ class Bot {
209
209
}
210
210
}
211
211
212
- sendToDiscord ( author , channel , text ) {
213
- const discordChannelName = this . invertedMapping [ channel . toLowerCase ( ) ] ;
212
+ getDiscordChannelFor ( ircChannel ) {
213
+ const discordChannelName = this . invertedMapping [ ircChannel . toLowerCase ( ) ] ;
214
214
if ( discordChannelName ) {
215
215
// #channel -> channel before retrieving and select only text channels:
216
216
const discordChannel = this . discord . channels
@@ -220,52 +220,50 @@ class Bot {
220
220
if ( ! discordChannel ) {
221
221
logger . info ( 'Tried to send a message to a channel the bot isn\'t in: ' ,
222
222
discordChannelName ) ;
223
- return ;
223
+ return null ;
224
224
}
225
225
226
- const withMentions = text . replace ( / @ [ ^ \s ] + \b / g, ( match ) => {
227
- const search = match . substring ( 1 ) ;
228
- const guild = discordChannel . guild ;
229
- const nickUser = guild . members . find ( 'nickname' , search ) ;
230
- if ( nickUser ) {
231
- return nickUser ;
232
- }
226
+ return discordChannel ;
227
+ }
228
+ return null ;
229
+ }
233
230
234
- const user = this . discord . users . find ( 'username' , search ) ;
235
- if ( user ) {
236
- const nickname = guild . members . get ( user . id ) . nickname ;
237
- if ( ! nickname || nickname === search ) {
238
- return user ;
239
- }
231
+ sendToDiscord ( author , channel , text ) {
232
+ const discordChannel = this . getDiscordChannelFor ( channel ) ;
233
+ if ( ! discordChannel ) return ;
234
+
235
+ const withMentions = text . replace ( / @ [ ^ \s ] + \b / g, ( match ) => {
236
+ const search = match . substring ( 1 ) ;
237
+ const guild = discordChannel . guild ;
238
+ const nickUser = guild . members . find ( 'nickname' , search ) ;
239
+ if ( nickUser ) {
240
+ return nickUser ;
241
+ }
242
+
243
+ const user = this . discord . users . find ( 'username' , search ) ;
244
+ if ( user ) {
245
+ const nickname = guild . members . get ( user . id ) . nickname ;
246
+ if ( ! nickname || nickname === search ) {
247
+ return user ;
240
248
}
249
+ }
241
250
242
- return match ;
243
- } ) ;
251
+ return match ;
252
+ } ) ;
244
253
245
- // Add bold formatting:
246
- const withAuthor = `**<${ author } >** ${ withMentions } ` ;
247
- logger . debug ( 'Sending message to Discord' , withAuthor , channel , '->' , discordChannelName ) ;
248
- discordChannel . sendMessage ( withAuthor ) ;
249
- }
254
+ // Add bold formatting:
255
+ const withAuthor = `**<${ author } >** ${ withMentions } ` ;
256
+ logger . debug ( 'Sending message to Discord' , withAuthor , channel , '->' , `#${ discordChannel . name } ` ) ;
257
+ discordChannel . sendMessage ( withAuthor ) ;
250
258
}
251
259
252
260
/* Sends a message to Discord exactly as it appears */
253
261
sendSpecialToDiscord ( channel , text ) {
254
- const discordChannelName = this . invertedMapping [ channel . toLowerCase ( ) ] ;
255
- if ( discordChannelName ) {
256
- const discordChannel = this . discord . channels
257
- . filter ( c => c . type === 'text' )
258
- . find ( 'name' , discordChannelName . slice ( 1 ) ) ;
259
-
260
- if ( ! discordChannel ) {
261
- logger . info ( 'Tried to send a message to a channel the bot isn\'t in: ' ,
262
- discordChannelName ) ;
263
- return ;
264
- }
262
+ const discordChannel = this . getDiscordChannelFor ( channel ) ;
263
+ if ( ! discordChannel ) return ;
265
264
266
- logger . debug ( 'Sending special message to Discord' , text , channel , '->' , discordChannelName ) ;
267
- discordChannel . sendMessage ( text ) ;
268
- }
265
+ logger . debug ( 'Sending special message to Discord' , text , channel , '->' , `#${ discordChannel . name } ` ) ;
266
+ discordChannel . sendMessage ( text ) ;
269
267
}
270
268
}
271
269
0 commit comments