Skip to content

Commit 9b4cfa2

Browse files
committed
Extract getDiscordChannelFor from send(Special)ToDiscord
1 parent 13ef998 commit 9b4cfa2

File tree

1 file changed

+35
-37
lines changed

1 file changed

+35
-37
lines changed

lib/bot.js

Lines changed: 35 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -209,8 +209,8 @@ class Bot {
209209
}
210210
}
211211

212-
sendToDiscord(author, channel, text) {
213-
const discordChannelName = this.invertedMapping[channel.toLowerCase()];
212+
getDiscordChannelFor(ircChannel) {
213+
const discordChannelName = this.invertedMapping[ircChannel.toLowerCase()];
214214
if (discordChannelName) {
215215
// #channel -> channel before retrieving and select only text channels:
216216
const discordChannel = this.discord.channels
@@ -220,52 +220,50 @@ class Bot {
220220
if (!discordChannel) {
221221
logger.info('Tried to send a message to a channel the bot isn\'t in: ',
222222
discordChannelName);
223-
return;
223+
return null;
224224
}
225225

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+
}
233230

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;
240248
}
249+
}
241250

242-
return match;
243-
});
251+
return match;
252+
});
244253

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);
250258
}
251259

252260
/* Sends a message to Discord exactly as it appears */
253261
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;
265264

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);
269267
}
270268
}
271269

0 commit comments

Comments
 (0)