-
-
Notifications
You must be signed in to change notification settings - Fork 751
Fix 400 when "clyde" is in any webhook username #1009
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
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
bcf6993
Fix 400 when "clyde" is in webhook username
MarkKoz 311326b
Make sub_clyde case-sensitive and use Cyrillic e's
MarkKoz 55db81a
Preserve empty string when substituting clyde
MarkKoz 581573f
Write unit test for `sub_clyde`
28919b6
Merge branch 'master' into bug/mod/bot-2a/webhook-clyde
kwzrd File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,7 @@ | ||
| import asyncio | ||
| import contextlib | ||
| import logging | ||
| import re | ||
| from io import BytesIO | ||
| from typing import List, Optional, Sequence, Union | ||
|
|
||
|
|
@@ -86,7 +87,7 @@ async def send_attachments( | |
| else: | ||
| await destination.send( | ||
| file=attachment_file, | ||
| username=message.author.display_name, | ||
| username=sub_clyde(message.author.display_name), | ||
| avatar_url=message.author.avatar_url | ||
| ) | ||
| elif link_large: | ||
|
|
@@ -109,8 +110,25 @@ async def send_attachments( | |
| else: | ||
| await destination.send( | ||
| embed=embed, | ||
| username=message.author.display_name, | ||
| username=sub_clyde(message.author.display_name), | ||
| avatar_url=message.author.avatar_url | ||
| ) | ||
|
|
||
| return urls | ||
|
|
||
|
|
||
| def sub_clyde(username: Optional[str]) -> Optional[str]: | ||
| """ | ||
| Replace "e"/"E" in any "clyde" in `username` with a Cyrillic "е"/"E" and return the new string. | ||
|
|
||
| Discord disallows "clyde" anywhere in the username for webhooks. It will return a 400. | ||
| Return None only if `username` is None. | ||
| """ | ||
| def replace_e(match: re.Match) -> str: | ||
| char = "е" if match[2] == "e" else "Е" | ||
| return match[1] + char | ||
|
|
||
| if username: | ||
| return re.sub(r"(clyd)(e)", replace_e, username, flags=re.I) | ||
|
Comment on lines
+120
to
+132
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This will also return |
||
| else: | ||
| return username # Empty string or None | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| import unittest | ||
|
|
||
| from bot.utils import messages | ||
|
|
||
|
|
||
| class TestMessages(unittest.TestCase): | ||
| """Tests for functions in the `bot.utils.messages` module.""" | ||
|
|
||
| def test_sub_clyde(self): | ||
| """Uppercase E's and lowercase e's are substituted with their cyrillic counterparts.""" | ||
| sub_e = "\u0435" | ||
| sub_E = "\u0415" # noqa: N806: Uppercase E in variable name | ||
|
|
||
| test_cases = ( | ||
| (None, None), | ||
| ("", ""), | ||
| ("clyde", f"clyd{sub_e}"), | ||
| ("CLYDE", f"CLYD{sub_E}"), | ||
| ("cLyDe", f"cLyD{sub_e}"), | ||
| ("BIGclyde", f"BIGclyd{sub_e}"), | ||
| ("small clydeus the unholy", f"small clyd{sub_e}us the unholy"), | ||
| ("BIGCLYDE, babyclyde", f"BIGCLYD{sub_E}, babyclyd{sub_e}"), | ||
| ) | ||
|
|
||
| for username_in, username_out in test_cases: | ||
| with self.subTest(input=username_in, expected_output=username_out): | ||
| self.assertEqual(messages.sub_clyde(username_in), username_out) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
In what case
usernamecan beNone? So much how I see, this should passstrevery time.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.
It's optional here though every use of that function passes a name.
bot/bot/cogs/watchchannels/watchchannel.py
Lines 199 to 207 in bcf6993
I don't see a problem with leaving it optional anyway.
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.
If this internally handles the
None(and it does) then I agree the annotation is appropriate, but why does it handle it? Is it to be "shielded" from callers which may accidentally give aNone? Won't that just push the error further, since something else that is expecting a string will get the declydedNone?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.
None is a valid value for webhook username (acts like an empty string and defaults to the webhook name) that may get passed in so I don't think there's any problem with letting None through.
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.
In that case I agree that this is perfectly fine.