Skip to content

Commit d9375a0

Browse files
committed
Support image carousels for starboard entries
1 parent 93766f0 commit d9375a0

File tree

1 file changed

+29
-32
lines changed

1 file changed

+29
-32
lines changed

extensions/starboard.py

Lines changed: 29 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
starboard = SnedPlugin("Starboard")
1818

1919
IMAGE_URL_REGEX = re.compile(
20-
r"https?:\/\/(www\.)?[-a-zA-Z0-9@:%._\+~#=]{1,256}\.[a-zA-Z0-9()]{1,6}\b([-a-zA-Z0-9()!@:%_\+.~#?&\/\/=]*)([.]jpe?g|png|gif|bmp|webp)[-a-zA-Z0-9@:%._\+~#=?&]*"
20+
r"https?:\/\/(?:www\.)?[-a-zA-Z0-9@:%._\+~#=]{1,256}\.[a-zA-Z0-9()]{1,6}\b(?:[-a-zA-Z0-9()!@:%_\+.~#?&\/\/=]*)(?:[.]jpe?g|png|gif|bmp|webp)[-a-zA-Z0-9@:%._\+~#=?&]*"
2121
)
2222

2323
STAR_MAPPING = {
@@ -28,32 +28,19 @@
2828
}
2929

3030

31-
def get_image_url(content: str) -> str | None:
31+
def get_image_urls(content: str) -> list[str]:
3232
"""Return a list of image URLs found in the message content."""
33+
return IMAGE_URL_REGEX.findall(content)
3334

34-
matches: re.Match[str] | None = re.search(IMAGE_URL_REGEX, content)
3535

36-
if not matches:
37-
return None
38-
39-
return content[matches.span()[0] : matches.span()[1]]
40-
41-
42-
def get_attachment_url(message: hikari.Message) -> str | None:
36+
def get_img_attach_urls(message: hikari.Message) -> list[str]:
4337
"""Return a list of image attachment URLs found in the message."""
4438

4539
if not message.attachments:
46-
return
40+
return []
4741

4842
attach_urls = [attachment.url for attachment in message.attachments]
49-
string = " ".join(attach_urls)
50-
51-
matches: re.Match[str] | None = re.search(IMAGE_URL_REGEX, string)
52-
53-
if not matches:
54-
return None
55-
56-
return string[matches.span()[0] : matches.span()[1]]
43+
return [url for url in attach_urls if IMAGE_URL_REGEX.fullmatch(url)]
5744

5845

5946
def create_starboard_payload(
@@ -83,39 +70,49 @@ def create_starboard_payload(
8370
guild_id = hikari.Snowflake(guild)
8471
member = starboard.app.cache.get_member(guild_id, message.author.id)
8572
emoji = [emoji for emoji, value in STAR_MAPPING.items() if value <= stars][-1]
73+
8674
content = f"{emoji} **{stars}{' (Forced)' if force_starred else ''}** <#{message.channel_id}>"
87-
embed = (
88-
hikari.Embed(description=message.content, color=0xFFC20C)
75+
76+
# A url must be set for all embeds to make the image carousel work
77+
head_embed = (
78+
hikari.Embed(description=message.content, color=0xFFC20C, url="https://example.com")
8979
.set_author(
9080
name=member.display_name if member else "Unknown", icon=member.display_avatar_url if member else None
9181
)
9282
.set_footer(f"ID: {message.id}")
9383
)
94-
attachments = message.attachments
9584

96-
if image_urls := get_attachment_url(message):
97-
embed.set_image(image_urls)
98-
attachments = [attachment for attachment in attachments if attachment.url != image_urls]
85+
image_urls: list[str] = []
86+
87+
if message.attachments and (attach_urls := get_img_attach_urls(message)):
88+
image_urls += attach_urls
9989

100-
elif message.content:
101-
if image_urls := get_image_url(message.content):
102-
embed.set_image(image_urls)
90+
if message.content and (content_urls := get_image_urls(message.content)):
91+
image_urls += content_urls
92+
93+
# Remove duplicate attachments
94+
attachments = [attachment for attachment in message.attachments if attachment.url not in image_urls[:10]]
10395

10496
if attachments:
105-
embed.add_field(
97+
head_embed.add_field(
10698
"Attachments",
10799
"\n".join([f"[{attachment.filename[:100]}]({attachment.url})" for attachment in attachments][:5]),
108100
)
109101

110102
if message.referenced_message:
111-
embed.add_field(
103+
head_embed.add_field(
112104
"Replying to",
113105
f"[{message.referenced_message.author}]({message.referenced_message.make_link(guild_id)})",
114106
)
115107

116-
embed.add_field("Original Message", f"[Jump!]({message.make_link(guild_id)})")
108+
head_embed.add_field("Original Message", f"[Jump!]({message.make_link(guild_id)})")
109+
110+
if image_urls:
111+
head_embed.set_image(image_urls[0])
112+
113+
tail_embeds = [hikari.Embed(url="https://example.com").set_image(image_url) for image_url in image_urls[1:][:10]]
117114

118-
return {"content": content, "embed": embed}
115+
return {"content": content, "embeds": [head_embed] + tail_embeds}
119116

120117

121118
async def star_message(

0 commit comments

Comments
 (0)