-
Notifications
You must be signed in to change notification settings - Fork 294
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Use webhooks to reflect irc usernames + avatars #230
Conversation
Could you add tests for |
Sure! I'll do this soon. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Bump on tests?
Yes, I'm a bit short on free time these days. I'll try to produce something
this week-end :)
…On 16 May 2017 at 20:01, Edward Jones ***@***.***> wrote:
***@***.**** requested changes on this pull request.
Bump on tests?
—
You are receiving this because you authored the thread.
Reply to this email directly, view it on GitHub
<#230 (review)>,
or mute the thread
<https://github.com/notifications/unsubscribe-auth/AArNkT1dnEipdy1wkc2PF_PymWZs83xFks5r6eRngaJpZM4NN89p>
.
|
Finally got some time to write tests, sorry for the delay :) |
I would like to add this as a warning if this gets pushed to master. So basically currently this wont be really readable on android client because of messages merging under one author incorrectly. |
Aw man, you already made it work with latest commit. Oh well. I'll delete my fork now. |
Testing this locally, it seems to work well, and the tests added should help to ensure it keeps this functionality. Assuming @ekmartin is okay with it, I think this is good to go. (I'm slightly worried about IRC users masquerading as authenticated Discord users by changing to their usernames, but I don't think this is readily fixable, and the messages have 'BOT' marked on them, and it presents a problem on the IRC end as is anyway, so this shouldn't actually make it much if at all worse.) Edit: Oh, a note – maybe add to the readme that webhooks allow for this sort of display behavior? That's not immediately obvious from the term 'webhook'. |
Just added a note on webhooks. However feel free to change it, even the image. I feel quite uncomfortable writing the readme for a project that isn't mine! |
That looks like an accurate and useful summary, so it should be fine! Thanks. |
@Fiaxhs Can you make this case-insensitive? |
lib/bot.js
Outdated
} | ||
return null; | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think case insensitivity should be a blocker but it does make me wonder what happens if two Discord users have the same display name, as this seems doable, or the same name ignoring case. This is a problem elsewhere already, I guess, such as when an IRC user mentions a Discord user.
This function also currently checks by username as opposed to nickname (display name), which might be unexpected. This should maybe:
- Check by nickname and username
- Check by the above, case-insensitively, if no match found
- If more than one found, default to no fancy icon
That might be more complicated than necessary, but I think it would suffice for most or all use-cases. It should at least check nicknames?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What's the purpose of checking case-sensitively at all? Performance?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So that if you have two users with the same nickname barring case, you can pick a particular one to use. (It's also possible to have two users with the exact same nickname, case-sensitively, but then the ambiguity is basically required as opposed to mostly quite avoidable.)
Maybe it's over-much, though – if two users have the same nickname, case-sensitive or not, it's not particularly unreasonable to expect weird behavior around the ambiguity. It at least shouldn't be actively detrimental.
lib/bot.js
Outdated
}).catch(logger.error); | ||
return; | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
… I also failed to notice that this seems to discard the custom formatting when sending with a webhook; I think the 'Webhooks first' code should go after the .const withAuthor…
bit, here, and use withAuthor
instead of withMentions
Edit: Or, rather… make a new pattern for webhook messages?
Edit2: … I'm not sure. Uh. It seems plausibly useful to be able to change the formatting for a webhook message, if people were changing the withAuthor
thing drastically. Maybe. But defeats the point of a webhook a little. Uh. Maybe ignore this. We can address it later if anybody complains. (Above thing should still use nicknames and maybe case insensitively check.)
(Sorry I didn't notice this before.) |
Hey @Fiaxhs, can you update this to the latest commit? |
Nice! I didn't see the
Fine by me :)
You're welcome! I use it every day, for once I could bring some value to a project I use. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry about the late answer here - this is definitely cool. Just a few minor comments :)
lib/bot.js
Outdated
|
||
// Remove channel passwords from the mapping and lowercase IRC channel names | ||
_.forOwn(options.channelMapping, (ircChan, discordChan) => { | ||
this.channelMapping[discordChan] = ircChan.split(' ')[0].toLowerCase(); | ||
}); | ||
|
||
// Extract id and token from Webhook urls | ||
_.forOwn(options.webhooks, (url, chan) => { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
chan
-> channel
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
(consistent with your block on line 80)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
on second thought, might as well merge this down into the connect
block and only have one forOwn(options.webhooks, ...)
?
lib/bot.js
Outdated
@@ -258,7 +268,9 @@ class Bot { | |||
sendToIRC(message) { | |||
const author = message.author; | |||
// 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(chan => this.webhooks[chan].id === author.id) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
channel
lib/bot.js
Outdated
@@ -335,6 +347,44 @@ class Bot { | |||
return null; | |||
} | |||
|
|||
findWebhook(ircChannel) { | |||
const discordChannelName = this.invertedMapping[ircChannel.toLowerCase()]; | |||
if (discordChannelName) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Might as well do: return discordChannelName && this.webhooks[discordChannelName]
.
const guildMembers = this.findDiscordChannel(channel).guild.members; | ||
const findByNicknameOrUsername = caseSensitive => | ||
(member) => { | ||
if (caseSensitive) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Are discord nicknames case sensitive or insensitive? Can there be two users with the same username/nickname with different casing? I think we can simplify this logic as long as we know what assumptions we're making.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Insensitive and yes, which is why I suggested (I think I suggested?) matching case sensitively first and then falling back to case insensitive if there's no match found. (Since IRC users have no way to, in a dropdown, select the correct one of the various case sensitivities – Discord users have this ability since Discord translates the mention in its UI – I thought it best to allow them to force the right one by picking the right case, where relevant.)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You can even have two users with the exact same nickname (same case): http://i.imgur.com/ACuYlcS.png + http://i.imgur.com/oD3JEfK.png
(And @maorninja suggested the change :) )
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
(Yes but I suggested here the logic for how to implement it. :P)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
( right :D )
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Personally, I would blame Discord because they allow 2 users with the exact same nickname, but that's just my opinion. I wish there was a bot that could block that.
lib/bot.js
Outdated
username: author, | ||
text, | ||
avatarURL: this.getDiscordAvatar(author, channel) | ||
}).catch(logger.error); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would the error here not bubble up to our on('error'
handler? (I don't know)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Webhook clients aren't attached to the main discord client:
this.discord = new discord.Client({ autoReconnect: true });
vs.
this.webhooks[channel] = { [...], client: new discord.WebhookClient(id, token);}
So there's no way errors will bubble up to the main on('error'
I could probably define a function and use it for both error handling, though.
lib/bot.js
Outdated
const webhook = this.findWebhook(channel); | ||
if (webhook) { | ||
logger.debug('Sending message to Discord via webhook', withMentions, channel, '->', `#${discordChannel.name}`); | ||
webhook.client.sendMessage( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
formatting nitpick:
webhook.client.sendMessage(withMentions, {
text,
username: author,
avatarUrl: this.getDiscordAvatar(author, channel)
})
(could even do const avatarURL = this.getDiscordAvatar(author, channel);
first - always optimize for readability)
test/bot.test.js
Outdated
@@ -807,4 +810,81 @@ describe('Bot', function () { | |||
this.sendStub.should.have.been.calledOnce; | |||
this.sendStub.getCall(0).args.should.deep.equal([msg]); | |||
}); | |||
|
|||
it('should create webhooks clients for each webhook url in the config', function () { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Lots of tests - nice!
Variables renaming Improved code readability
What if we fall back to adorable.io avatars? See https://github.com/ekmartin/slack-irc/ and qaisjp/go-discord-irc@8593484 |
Also, please send feedback to Discord asking them to put some focus on this! https://trello.com/c/6ZZTEBJw @Kakkela's #230 (comment) shows a major block |
@qiasjp I'm not sure there's a need to fallback to specific avatars – if the users haven't got Discord accounts with avatars, then they haven't chosen an avatar, so it seems sensible to stick to what Discord does by default? (That is, the standard game controller picture.) I agree that there should be a warning about the bug when this gets merged. That's a pretty big issue on Discord's end. I don't think it should be a block to us merging this, but instead added as a clear warning in the relevant config area, so users of the individual servers can choose whether to enable it despite this. |
Also, something to mention to people reading this issue: A nice workaround for the Android/web bug is to alternate the webhook per user (This wouldn't be suitable for reactiflux/discord-irc, but my purpose build discord-irc bridge bot will do this) |
const webhook = this.findWebhook(channel); | ||
if (webhook) { | ||
logger.debug('Sending message to Discord via webhook', withMentions, channel, '->', `#${discordChannel.name}`); | ||
const avatarURL = this.getDiscordAvatar(author, channel); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
During netsplits author
is nil. This would usually send some message like <${username}> ${message}
(or something similar, I can't recall. but the message would contain ${}
sent verbatim). Now this.getDiscordAvatar
crashes with TypeError: Cannot read property 'toLowerCase' of undefined
EDIT: NVM. |
@Kovaelin Here's what my config looks like:
|
I still can't seem to get the webhook to work. Do I have to change any of the other files? Does it work if I use the Discord channel ID rather than the channel name? My config file looks like this: { |
@Kovaelin Could you open an issue on https://github.com/Fiaxhs/discord-irc instead, please? |
Still looking for this even with the bug. This would be a huge improvement to quality of life |
Webhook merging bug on Android is now moved to "Claimed fixed, needs verification" https://trello.com/c/6ZZTEBJw/278-webhook-incorrectly-stacks-message-authors-on-android-client |
Omg excited. Please keep us updated :)
…On Wed, 21 Mar 2018, 10:29 pm Iku, ***@***.***> wrote:
Webhook merging bug on Android is now moved to "Claimed fixed, needs
verification"
I'm going to test on my bridge today or tomorrow. Not sure if it's fixed
on just Alpha channel on Android or what.
https://trello.com/c/6ZZTEBJw/278-webhook-incorrectly-stacks-message-authors-on-android-client
—
You are receiving this because you commented.
Reply to this email directly, view it on GitHub
<#230 (comment)>,
or mute the thread
<https://github.com/notifications/unsubscribe-auth/AA4WasiBpdYzaXZsPzjqPNtLG0Lt89sUks5tgtQwgaJpZM4NN89p>
.
|
Yes this does now indeed work on Android (all channels) as intended and no unnecessary merging happens. |
And it's fixed on web too!!
…On Thu, 22 Mar 2018, 9:36 am Iku, ***@***.***> wrote:
Yes this does now indeed work on Android (all channels) as intended and no
unnecessary merging happens.
—
You are receiving this because you commented.
Reply to this email directly, view it on GitHub
<#230 (comment)>,
or mute the thread
<https://github.com/notifications/unsubscribe-auth/AA4Wahd1WFQzo45R210JnMnMbLjBiy6Zks5tg3CNgaJpZM4NN89p>
.
|
Published in version 2.6.0 of discord-irc - thank you for your patience! I wonder if the usernames on the Discord side should be suffixed with |
Side note: discord webhooks have a minimum name length of 2 characters, and
having a suffix of `(IRC)` would be an easy countermeasure.
…On Thu, 22 Mar 2018 at 14:34 Martin Ek ***@***.***> wrote:
Published in version 2.6.0
<https://github.com/reactiflux/discord-irc/blob/master/CHANGELOG.md#260---2018-03-22>
of discord-irc - thank you for your patience!
I wonder if the usernames on the Discord side should be suffixed with
(IRC) to make what's happening clearer though? If you think that's a good
idea we can publish a new version later.
—
You are receiving this because you commented.
Reply to this email directly, view it on GitHub
<#230 (comment)>,
or mute the thread
<https://github.com/notifications/unsubscribe-auth/AA4WalOpkW_cP004I-c5nKEfn50ToSWgks5tg7aOgaJpZM4NN89p>
.
|
Opened an issue for it: #377 - PRs welcome! |
Thank you for this update! |
This fixes #128
The discord bot gets renamed with the irc nickname, and use the account avatar if there's a matching username in the room.