Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
raidensakura committed Apr 4, 2024
1 parent d7eb469 commit 8e27794
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 41 deletions.
9 changes: 9 additions & 0 deletions core/clients.py
Original file line number Diff line number Diff line change
Expand Up @@ -680,6 +680,15 @@ async def append_log(
}
for a in message.attachments
],
"stickers": [
{
"format": s.format,
"id": s.id,
"name": s.name,
"url": s.url,
}
for s in message.stickers
],
}

return await self.logs.find_one_and_update(
Expand Down
88 changes: 47 additions & 41 deletions core/thread.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
ConfirmThreadCreationView,
DenyButton,
DummyParam,
convert_sticker,
create_thread_channel,
get_joint_id,
get_top_role,
Expand Down Expand Up @@ -943,6 +944,11 @@ async def send(
if not self.ready:
await self.wait_until_ready()

for i, sticker in enumerate(message.stickers):
url = convert_sticker(sticker)
if url:
message.stickers[i] = sticker

if not from_mod and not note:
self.bot.loop.create_task(self.bot.api.append_log(message, channel_id=self.channel.id))

Expand Down Expand Up @@ -995,7 +1001,8 @@ async def send(
url=f"https://discordapp.com/users/{author.id}#{message.id}",
)

ext = [(a.url, a.filename, False) for a in message.attachments]
ext = [(a.url, a.filename) for a in message.attachments]
stickers = [(a.id, a.url, a.name) for a in message.stickers]

images = []
attachments = []
Expand All @@ -1005,62 +1012,41 @@ async def send(
else:
attachments.append(attachment)

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

image_urls = [
(is_image_url(url, convert_size=False), None, False)
for url in image_urls
if is_image_url(url, convert_size=False)
]
images.extend(image_urls)
images.extend(
(
i.url
if i.format in (discord.StickerFormatType.png, discord.StickerFormatType.apng)
else None,
i.name,
True,
)
for i in message.stickers
)
for match in image_regex:
if is_image_url(match, convert_size=False):
images.append((match, None))

embedded_image = False
img_on_first_embed = False

prioritize_uploads = any(i[1] is not None for i in images)

additional_images = []
additional_count = 1

for url, filename, is_sticker in images:
if note:
color = self.bot.main_color
elif from_mod:
color = self.bot.mod_color
else:
color = self.bot.recipient_color

for url, filename in images:
if (
not prioritize_uploads or ((url is None or is_image_url(url)) and filename)
) and not embedded_image:
if url is not None:
) and not img_on_first_embed:
if url:
embed.set_image(url=url)
if filename:
if is_sticker:
if url is None:
description = f"{filename}: Unable to retrieve sticker image"
else:
description = f"[{filename}]({url})"
embed.add_field(name="Sticker", value=description)
else:
embed.add_field(name="Image", value=f"[{filename}]({url})")
embedded_image = True
embed.add_field(name="Image", value=f"[{filename}]({url})")
img_on_first_embed = True
else:
if note:
color = self.bot.main_color
elif from_mod:
color = self.bot.mod_color
else:
color = self.bot.recipient_color

img_embed = discord.Embed(color=color)

if url is not None:
if url:
img_embed.set_image(url=url)
img_embed.url = url
if filename is not None:
Expand All @@ -1070,6 +1056,26 @@ async def send(
additional_images.append(destination.send(embed=img_embed))
additional_count += 1

for id, url, name in stickers:
if not img_on_first_embed:
embed.set_image(url=url)
embed.add_field(name="Sticker", value=f"[{name}]({url})", inline=False)
embed.url = url
embed.timestamp = message.created_at
else:
if note:
color = self.bot.main_color
elif from_mod:
color = self.bot.mod_color
else:
color = self.bot.recipient_color
sticker_embed = discord.Embed(color=self.bot.main_color)
sticker_embed.set_image(url=url)
sticker_embed.add_field(name="Sticker", value=f"[{name}]({url})", inline=False)
sticker_embed.url = url
sticker_embed.timestamp = message.created_at
additional_images.append(destination.send(embed=sticker_embed))

file_upload_count = 1

for url, filename, _ in attachments:
Expand Down
27 changes: 27 additions & 0 deletions core/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -592,3 +592,30 @@ class DummyParam:
def __init__(self, name):
self.name = name
self.displayed_name = name


def convert_sticker(sticker: discord.Sticker) -> str:
"""A method to convert Discord sticker object into a displayable image URL.
Parameters
----------
sticker : discord.Sticker
Discord sticker object.
Returns
-------
Optional[str]
URL of the image if successfully converted
"""
url = None
if sticker.format.name == "png":
url = sticker.url
elif sticker.format.name == "apng":
logger.debug("Unable to convert APNG sticker")
elif sticker.format.name == "lottie":
logger.debug("Unable to convert Lottie sticker")
elif sticker.format.name == "gif":
pattern = r"(https://)cdn\.discordapp\.com(/.+)"
replacement = r"\1media.discordapp.net\2"
url = re.sub(pattern, replacement, sticker.url)
return url

0 comments on commit 8e27794

Please sign in to comment.