Skip to content

Commit

Permalink
Extract getDiscordChannelFor from send(Special)ToDiscord
Browse files Browse the repository at this point in the history
  • Loading branch information
Throne3d committed Mar 26, 2017
1 parent 13ef998 commit 9b4cfa2
Showing 1 changed file with 35 additions and 37 deletions.
72 changes: 35 additions & 37 deletions lib/bot.js
Original file line number Diff line number Diff line change
Expand Up @@ -209,8 +209,8 @@ class Bot {
}
}

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

const withMentions = text.replace(/@[^\s]+\b/g, (match) => {
const search = match.substring(1);
const guild = discordChannel.guild;
const nickUser = guild.members.find('nickname', search);
if (nickUser) {
return nickUser;
}
return discordChannel;
}
return null;
}

const user = this.discord.users.find('username', search);
if (user) {
const nickname = guild.members.get(user.id).nickname;
if (!nickname || nickname === search) {
return user;
}
sendToDiscord(author, channel, text) {
const discordChannel = this.getDiscordChannelFor(channel);
if (!discordChannel) return;

const withMentions = text.replace(/@[^\s]+\b/g, (match) => {
const search = match.substring(1);
const guild = discordChannel.guild;
const nickUser = guild.members.find('nickname', search);
if (nickUser) {
return nickUser;
}

const user = this.discord.users.find('username', search);
if (user) {
const nickname = guild.members.get(user.id).nickname;
if (!nickname || nickname === search) {
return user;
}
}

return match;
});
return match;
});

// Add bold formatting:
const withAuthor = `**<${author}>** ${withMentions}`;
logger.debug('Sending message to Discord', withAuthor, channel, '->', discordChannelName);
discordChannel.sendMessage(withAuthor);
}
// Add bold formatting:
const withAuthor = `**<${author}>** ${withMentions}`;
logger.debug('Sending message to Discord', withAuthor, channel, '->', `#${discordChannel.name}`);
discordChannel.sendMessage(withAuthor);
}

/* Sends a message to Discord exactly as it appears */
sendSpecialToDiscord(channel, text) {
const discordChannelName = this.invertedMapping[channel.toLowerCase()];
if (discordChannelName) {
const discordChannel = this.discord.channels
.filter(c => c.type === 'text')
.find('name', discordChannelName.slice(1));

if (!discordChannel) {
logger.info('Tried to send a message to a channel the bot isn\'t in: ',
discordChannelName);
return;
}
const discordChannel = this.getDiscordChannelFor(channel);
if (!discordChannel) return;

logger.debug('Sending special message to Discord', text, channel, '->', discordChannelName);
discordChannel.sendMessage(text);
}
logger.debug('Sending special message to Discord', text, channel, '->', `#${discordChannel.name}`);
discordChannel.sendMessage(text);
}
}

Expand Down

0 comments on commit 9b4cfa2

Please sign in to comment.