|
17 | 17 | starboard = SnedPlugin("Starboard") |
18 | 18 |
|
19 | 19 | 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@:%._\+~#=?&]*" |
21 | 21 | ) |
22 | 22 |
|
23 | 23 | STAR_MAPPING = { |
|
28 | 28 | } |
29 | 29 |
|
30 | 30 |
|
31 | | -def get_image_url(content: str) -> str | None: |
| 31 | +def get_image_urls(content: str) -> list[str]: |
32 | 32 | """Return a list of image URLs found in the message content.""" |
| 33 | + return IMAGE_URL_REGEX.findall(content) |
33 | 34 |
|
34 | | - matches: re.Match[str] | None = re.search(IMAGE_URL_REGEX, content) |
35 | 35 |
|
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]: |
43 | 37 | """Return a list of image attachment URLs found in the message.""" |
44 | 38 |
|
45 | 39 | if not message.attachments: |
46 | | - return |
| 40 | + return [] |
47 | 41 |
|
48 | 42 | 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)] |
57 | 44 |
|
58 | 45 |
|
59 | 46 | def create_starboard_payload( |
@@ -83,39 +70,49 @@ def create_starboard_payload( |
83 | 70 | guild_id = hikari.Snowflake(guild) |
84 | 71 | member = starboard.app.cache.get_member(guild_id, message.author.id) |
85 | 72 | emoji = [emoji for emoji, value in STAR_MAPPING.items() if value <= stars][-1] |
| 73 | + |
86 | 74 | 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") |
89 | 79 | .set_author( |
90 | 80 | name=member.display_name if member else "Unknown", icon=member.display_avatar_url if member else None |
91 | 81 | ) |
92 | 82 | .set_footer(f"ID: {message.id}") |
93 | 83 | ) |
94 | | - attachments = message.attachments |
95 | 84 |
|
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 |
99 | 89 |
|
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]] |
103 | 95 |
|
104 | 96 | if attachments: |
105 | | - embed.add_field( |
| 97 | + head_embed.add_field( |
106 | 98 | "Attachments", |
107 | 99 | "\n".join([f"[{attachment.filename[:100]}]({attachment.url})" for attachment in attachments][:5]), |
108 | 100 | ) |
109 | 101 |
|
110 | 102 | if message.referenced_message: |
111 | | - embed.add_field( |
| 103 | + head_embed.add_field( |
112 | 104 | "Replying to", |
113 | 105 | f"[{message.referenced_message.author}]({message.referenced_message.make_link(guild_id)})", |
114 | 106 | ) |
115 | 107 |
|
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]] |
117 | 114 |
|
118 | | - return {"content": content, "embed": embed} |
| 115 | + return {"content": content, "embeds": [head_embed] + tail_embeds} |
119 | 116 |
|
120 | 117 |
|
121 | 118 | async def star_message( |
|
0 commit comments