discord-irc
Advanced tools
Comparing version 2.5.1 to 2.6.0
# Changelog | ||
This project adheres to [Semantic Versioning](http://semver.org/). | ||
## [2.6.0] - 2018-03-22 | ||
### Added | ||
* Support for posting messages to Discord using webhooks (thanks to | ||
[Fiaxhs](https://github.com/reactiflux/discord-irc/pull/230)!). | ||
Webhooks lets you override nicknames and avatars, so messages coming from IRC | ||
can appear as regular Discord messages: | ||
 | ||
To enable webhooks, follow part 1 of [this | ||
guide](https://support.discordapp.com/hc/en-us/articles/228383668-Intro-to-Webhooks) | ||
to create and retrieve a webhook URL for a specific channel, then enable it in | ||
discord-irc's config as follows: | ||
```json | ||
"webhooks": { | ||
"#discord-channel": "https://discordapp.com/api/webhooks/id/token" | ||
} | ||
``` | ||
## [2.5.1] - 2018-01-18 | ||
@@ -5,0 +26,0 @@ ### Fixed |
@@ -62,2 +62,3 @@ 'use strict'; | ||
this.announceSelfJoin = options.announceSelfJoin; | ||
this.webhookOptions = options.webhooks; | ||
@@ -97,2 +98,3 @@ // Nicks to ignore | ||
this.channelMapping = {}; | ||
this.webhooks = {}; | ||
@@ -112,2 +114,12 @@ // Remove channel passwords from the mapping and lowercase IRC channel names | ||
// Extract id and token from Webhook urls and connect. | ||
_lodash2.default.forOwn(this.webhookOptions, (url, channel) => { | ||
const [id, token] = url.split('/').slice(-2); | ||
const client = new _discord2.default.WebhookClient(id, token); | ||
this.webhooks[channel] = { | ||
id, | ||
client | ||
}; | ||
}); | ||
const ircOptions = _extends({ | ||
@@ -305,3 +317,3 @@ userName: this.nickname, | ||
// Ignore messages sent by the bot itself: | ||
if (author.id === this.discord.user.id) return; | ||
if (author.id === this.discord.user.id || Object.keys(this.webhooks).some(channel => this.webhooks[channel].id === author.id)) return; | ||
@@ -384,2 +396,32 @@ // Do not send to IRC if this user is on the ignore list. | ||
findWebhook(ircChannel) { | ||
const discordChannelName = this.invertedMapping[ircChannel.toLowerCase()]; | ||
return discordChannelName && this.webhooks[discordChannelName]; | ||
} | ||
getDiscordAvatar(nick, channel) { | ||
const guildMembers = this.findDiscordChannel(channel).guild.members; | ||
const findByNicknameOrUsername = caseSensitive => member => { | ||
if (caseSensitive) { | ||
return member.user.username === nick || member.nickname === nick; | ||
} | ||
const nickLowerCase = nick.toLowerCase(); | ||
return member.user.username.toLowerCase() === nickLowerCase || member.nickname && member.nickname.toLowerCase() === nickLowerCase; | ||
}; | ||
// Try to find exact matching case | ||
let users = guildMembers.filter(findByNicknameOrUsername(true)); | ||
// Now let's search case insensitive. | ||
if (users.size === 0) { | ||
users = guildMembers.filter(findByNicknameOrUsername(false)); | ||
} | ||
// No matching user or more than one => no avatar | ||
if (users && users.size === 1) { | ||
return users.first().user.avatarURL; | ||
} | ||
return null; | ||
} | ||
// compare two strings case-insensitively | ||
@@ -496,2 +538,15 @@ // for discord mention matching | ||
// Webhooks first | ||
const webhook = this.findWebhook(channel); | ||
if (webhook) { | ||
_winston2.default.debug('Sending message to Discord via webhook', withMentions, channel, '->', `#${discordChannel.name}`); | ||
const avatarURL = this.getDiscordAvatar(author, channel); | ||
webhook.client.sendMessage(withMentions, { | ||
username: author, | ||
text, | ||
avatarURL | ||
}).catch(_winston2.default.error); | ||
return; | ||
} | ||
patternMap.withMentions = withMentions; | ||
@@ -498,0 +553,0 @@ |
{ | ||
"name": "discord-irc", | ||
"version": "2.5.1", | ||
"version": "2.6.0", | ||
"description": "Connects IRC and Discord channels by sending messages back and forth.", | ||
@@ -5,0 +5,0 @@ "keywords": [ |
@@ -96,2 +96,6 @@ # discord-irc [](https://travis-ci.org/reactiflux/discord-irc) [](https://coveralls.io/github/reactiflux/discord-irc?branch=master) | ||
"discord": ["discord_nick1", "discord_nick2"] // Ignore specified Discord nicks and do not send their messages to IRC. | ||
}, | ||
// List of webhooks per channel | ||
"webhooks": { | ||
"#discord": "https://discordapp.com/api/webhooks/id/token" | ||
} | ||
@@ -106,4 +110,20 @@ } | ||
### Webhooks | ||
Webhooks lets you override nicknames and avatars, so messages coming from IRC | ||
can appear as regular Discord messages: | ||
 | ||
To enable webhooks, follow part 1 of [this | ||
guide](https://support.discordapp.com/hc/en-us/articles/228383668-Intro-to-Webhooks) | ||
to create and retrieve a webhook URL for a specific channel, then enable it in | ||
discord-irc's config as follows: | ||
```json | ||
"webhooks": { | ||
"#discord-channel": "https://discordapp.com/api/webhooks/id/token" | ||
} | ||
``` | ||
### Encodings | ||
If you encounter trouble with some characters being corrupted from some clients (particularly umlauted characters, such as `ä` or `ö`), try installing the optional dependencies `iconv` and `node-icu-charset-detector`. | ||
@@ -110,0 +130,0 @@ The bot will produce a warning when started if the IRC library is unable to convert between encodings. |
48686
644
145