Skip to content

Commit

Permalink
Support gyazo links in image embed, resolve #282
Browse files Browse the repository at this point in the history
  • Loading branch information
fourjr committed Nov 5, 2020
1 parent 5cb85a2 commit 3eee193
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 15 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
This project mostly adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html);
however, insignificant breaking changes do not guarantee a major version bump, see the reasoning [here](https://github.com/kyb3r/modmail/issues/319). If you're a plugins developer, note the "BREAKING" section.

# v3.6.3-dev1
# v3.6.3-dev2

### Added

Expand All @@ -17,6 +17,7 @@ however, insignificant breaking changes do not guarantee a major version bump, s
- Added `close_on_leave` to automatically close threads upon recipient leaving the server. ([GH #2757](https://github.com/kyb3r/modmail/issues/2757))
- Added `alert_on_mention` to mention mods upon a bot mention. ([GH #2833](https://github.com/kyb3r/modmail/issues/2833))
- Added `confirm_thread_creation`, `confirm_thread_creation_title`, `confirm_thread_creation_description`, `confirm_thread_creation_accept`, `confirm_thread_creation_deny` to allow users to confirm that they indeed want to create a new thread. ([GH #2773](https://github.com/kyb3r/modmail/issues/2773))
- Support Gyazo image links in message embeds. ([GH #282](https://github.com/kyb3r/modmail/issues/282))

### Fixed

Expand Down
2 changes: 1 addition & 1 deletion bot.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
__version__ = "3.6.3-dev1"
__version__ = "3.6.3-dev2"


import asyncio
Expand Down
21 changes: 12 additions & 9 deletions core/thread.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ async def wait_until_ready(self) -> None:
await task
except asyncio.TimeoutError:
pass

self.wait_tasks.remove(task)

@property
Expand Down Expand Up @@ -86,7 +86,7 @@ def ready(self, flag: bool):
@property
def cancelled(self) -> bool:
return self._cancelled

@cancelled.setter
def cancelled(self, flag: bool):
self._cancelled = flag
Expand Down Expand Up @@ -793,11 +793,15 @@ async def send(
attachments.append(attachment)

image_urls = re.findall(
r"http[s]?://(?:[a-zA-Z]|[0-9]|[$-_@.&+]|[!*(),]|(?:%[0-9a-fA-F][0-9a-fA-F]))+",
r"http[s]?:\/\/(?:[a-zA-Z]|[0-9]|[$-_@.&+]|[!*(),]|(?:%[0-9a-fA-F][0-9a-fA-F]))+",
message.content,
)

image_urls = [(url, None) for url in image_urls if is_image_url(url)]
image_urls = [
(is_image_url(url, convert_size=False), None)
for url in image_urls
if is_image_url(url, convert_size=False)
]
images.extend(image_urls)

embedded_image = False
Expand Down Expand Up @@ -1043,7 +1047,9 @@ async def create(
if thread.channel and self.bot.get_channel(thread.channel.id):
logger.warning("Found an existing thread for %s, abort creating.", recipient)
return thread
logger.warning("Found an existing thread for %s, closing previous thread.", recipient)
logger.warning(
"Found an existing thread for %s, closing previous thread.", recipient
)
self.bot.loop.create_task(
thread.close(closer=self.bot.user, silent=True, delete_channel=False)
)
Expand Down Expand Up @@ -1109,14 +1115,11 @@ async def create(
await asyncio.sleep(0.2)
await confirm.remove_reaction(deny_emoji, self.bot.user)
await message.channel.send(
embed=discord.Embed(
title="Cancelled", color=self.bot.error_color
)
embed=discord.Embed(title="Cancelled", color=self.bot.error_color)
)
del self.cache[recipient.id]
return thread


self.bot.loop.create_task(thread.setup(creator=creator, category=category))
return thread

Expand Down
19 changes: 15 additions & 4 deletions core/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ def format_preview(messages: typing.List[typing.Dict[str, typing.Any]]):
return out or "No Messages"


def is_image_url(url: str) -> bool:
def is_image_url(url: str, **kwargs) -> bool:
"""
Check if the URL is pointing to an image.
Expand All @@ -131,10 +131,18 @@ def is_image_url(url: str) -> bool:
bool
Whether the URL is a valid image URL.
"""
return bool(parse_image_url(url))
if url.startswith("https://gyazo.com") or url.startswith("http://gyazo.com"):
# gyazo support
url = re.sub(
r"(http[s]?:\/\/)((?:[a-zA-Z]|[0-9]|[$-_@.&+]|[!*(),]|(?:%[0-9a-fA-F][0-9a-fA-F]))+)",
r"\1i.\2.png",
url,
)

return parse_image_url(url, **kwargs)

def parse_image_url(url: str) -> str:

def parse_image_url(url: str, *, convert_size=True) -> str:
"""
Convert the image URL into a sized Discord avatar.
Expand All @@ -152,7 +160,10 @@ def parse_image_url(url: str) -> str:
url = parse.urlsplit(url)

if any(url.path.lower().endswith(i) for i in types):
return parse.urlunsplit((*url[:3], "size=128", url[-1]))
if convert_size:
return parse.urlunsplit((*url[:3], "size=128", url[-1]))
else:
return parse.urlunsplit(url)
return ""


Expand Down

0 comments on commit 3eee193

Please sign in to comment.