Skip to content

Commit

Permalink
Reworked webhook flow
Browse files Browse the repository at this point in the history
IRC formatting
  • Loading branch information
Fiaxhs committed May 2, 2017
1 parent 9ea90a9 commit 2c05038
Showing 1 changed file with 20 additions and 15 deletions.
35 changes: 20 additions & 15 deletions lib/bot.js
Original file line number Diff line number Diff line change
Expand Up @@ -322,6 +322,8 @@ class Bot {
.find('name', discordChannelName.slice(1)) : this.discord.channels.get(discordChannelName);

if (!discordChannel) {
logger.info('Tried to send a message to a channel the bot isn\'t in: ',
discordChannelName);
return null;
}
return discordChannel;
Expand All @@ -337,13 +339,15 @@ class Bot {
return webhook;
}
}
logger.info('Tried to send a message to a channel the bot isn\'t in or without webhook attached: ',
discordChannelName);
return null;
}

sendToDiscord(author, channel, text) {
const discordChannel = this.findDiscordChannel(channel);
if (!discordChannel) {
return;
}


// Convert text formatting (bold, italics, underscore)
const withFormat = formatFromIRCToDiscord(text);
Expand All @@ -369,6 +373,20 @@ class Bot {
return match;
});

// Webhooks first
const webhook = this.findWebhook(channel);
if (webhook) {
logger.debug('Sending message to Discord via webhook', withMentions, channel, '->', `#${discordChannel.name}`);
webhook.client.sendMessage(
withMentions,
{
username: author,
text,
avatarURL: this.getDiscordAvatar(author)
}).catch(logger.error);
return;
}

const patternMap = {
author,
text: withFormat,
Expand All @@ -381,19 +399,6 @@ class Bot {
// Use custom formatting from config / default formatting with bold author
const withAuthor = Bot.substitutePattern(this.formatDiscord, patternMap);
logger.debug('Sending message to Discord', withAuthor, channel, '->', `#${discordChannel.name}`);
if (!discordChannel) {
const webhook = this.findWebhook(channel);
if (webhook) {
webhook.client.sendMessage(
withAuthor,
{
username: author,
text,
avatarURL: this.getDiscordAvatar(author)
}).catch(logger.error);
}
return;
}
discordChannel.sendMessage(withAuthor);
}

Expand Down

0 comments on commit 2c05038

Please sign in to comment.