Skip to content

Commit 3eee193

Browse files
committed
Support gyazo links in image embed, resolve #282
1 parent 5cb85a2 commit 3eee193

File tree

4 files changed

+30
-15
lines changed

4 files changed

+30
-15
lines changed

Diff for: CHANGELOG.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
66
This project mostly adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html);
77
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.
88

9-
# v3.6.3-dev1
9+
# v3.6.3-dev2
1010

1111
### Added
1212

@@ -17,6 +17,7 @@ however, insignificant breaking changes do not guarantee a major version bump, s
1717
- Added `close_on_leave` to automatically close threads upon recipient leaving the server. ([GH #2757](https://github.com/kyb3r/modmail/issues/2757))
1818
- Added `alert_on_mention` to mention mods upon a bot mention. ([GH #2833](https://github.com/kyb3r/modmail/issues/2833))
1919
- 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))
20+
- Support Gyazo image links in message embeds. ([GH #282](https://github.com/kyb3r/modmail/issues/282))
2021

2122
### Fixed
2223

Diff for: bot.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
__version__ = "3.6.3-dev1"
1+
__version__ = "3.6.3-dev2"
22

33

44
import asyncio

Diff for: core/thread.py

+12-9
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ async def wait_until_ready(self) -> None:
5656
await task
5757
except asyncio.TimeoutError:
5858
pass
59-
59+
6060
self.wait_tasks.remove(task)
6161

6262
@property
@@ -86,7 +86,7 @@ def ready(self, flag: bool):
8686
@property
8787
def cancelled(self) -> bool:
8888
return self._cancelled
89-
89+
9090
@cancelled.setter
9191
def cancelled(self, flag: bool):
9292
self._cancelled = flag
@@ -793,11 +793,15 @@ async def send(
793793
attachments.append(attachment)
794794

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

800-
image_urls = [(url, None) for url in image_urls if is_image_url(url)]
800+
image_urls = [
801+
(is_image_url(url, convert_size=False), None)
802+
for url in image_urls
803+
if is_image_url(url, convert_size=False)
804+
]
801805
images.extend(image_urls)
802806

803807
embedded_image = False
@@ -1043,7 +1047,9 @@ async def create(
10431047
if thread.channel and self.bot.get_channel(thread.channel.id):
10441048
logger.warning("Found an existing thread for %s, abort creating.", recipient)
10451049
return thread
1046-
logger.warning("Found an existing thread for %s, closing previous thread.", recipient)
1050+
logger.warning(
1051+
"Found an existing thread for %s, closing previous thread.", recipient
1052+
)
10471053
self.bot.loop.create_task(
10481054
thread.close(closer=self.bot.user, silent=True, delete_channel=False)
10491055
)
@@ -1109,14 +1115,11 @@ async def create(
11091115
await asyncio.sleep(0.2)
11101116
await confirm.remove_reaction(deny_emoji, self.bot.user)
11111117
await message.channel.send(
1112-
embed=discord.Embed(
1113-
title="Cancelled", color=self.bot.error_color
1114-
)
1118+
embed=discord.Embed(title="Cancelled", color=self.bot.error_color)
11151119
)
11161120
del self.cache[recipient.id]
11171121
return thread
11181122

1119-
11201123
self.bot.loop.create_task(thread.setup(creator=creator, category=category))
11211124
return thread
11221125

Diff for: core/utils.py

+15-4
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ def format_preview(messages: typing.List[typing.Dict[str, typing.Any]]):
117117
return out or "No Messages"
118118

119119

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

142+
return parse_image_url(url, **kwargs)
136143

137-
def parse_image_url(url: str) -> str:
144+
145+
def parse_image_url(url: str, *, convert_size=True) -> str:
138146
"""
139147
Convert the image URL into a sized Discord avatar.
140148
@@ -152,7 +160,10 @@ def parse_image_url(url: str) -> str:
152160
url = parse.urlsplit(url)
153161

154162
if any(url.path.lower().endswith(i) for i in types):
155-
return parse.urlunsplit((*url[:3], "size=128", url[-1]))
163+
if convert_size:
164+
return parse.urlunsplit((*url[:3], "size=128", url[-1]))
165+
else:
166+
return parse.urlunsplit(url)
156167
return ""
157168

158169

0 commit comments

Comments
 (0)