Skip to content

Commit 786d672

Browse files
committed
Add support for multiple image and file attachments
1 parent 98ee9c0 commit 786d672

File tree

3 files changed

+35
-11
lines changed

3 files changed

+35
-11
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,12 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
55
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
66

77

8+
# v2.0.9
9+
10+
### Quick Fix
11+
- Support multiple image and file attachments in one message
12+
- This is only possible on mobile so its good to handle it in code.
13+
814
# v2.0.8
915

1016
Improvements in commands and new config options available.

bot.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
SOFTWARE.
2323
"""
2424

25-
__version__ = '2.0.8'
25+
__version__ = '2.0.9'
2626

2727
import asyncio
2828
import textwrap

core/thread.py

Lines changed: 28 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -93,21 +93,39 @@ async def send(self, message, destination=None, from_mod=False, delete_message=T
9393
em.set_author(name=str(author), icon_url=author.avatar_url, url=message.jump_url) # store message id in hidden url
9494

9595
image_types = ['.png', '.jpg', '.gif', '.jpeg', '.webp']
96-
is_image_url = lambda u: any(urlparse(u.lower()).path.endswith(x) for x in image_types)
96+
is_image_url = lambda u, _: any(urlparse(u.lower()).path.endswith(x) for x in image_types)
9797

9898
delete_message = not bool(message.attachments)
99-
attachments = list(filter(lambda a: not is_image_url(a.url), message.attachments))
10099

101-
image_urls = [a.url for a in message.attachments]
102-
image_urls.extend(re.findall(r'(https?://[^\s]+)', message.content))
103-
image_urls = list(filter(is_image_url, image_urls))
100+
attachments = [(a.url, a.filename) for a in message.attachments]
101+
102+
images = [x for x in attachments if is_image_url(*x)]
103+
attachments = [x for x in attachments if not is_image_url(*x)]
104104

105-
if image_urls:
106-
em.set_image(url=image_urls[0])
105+
image_links = [(link, None) for link in re.findall(r'(https?://[^\s]+)', message.content)]
106+
image_links = [x for x in image_links if is_image_url(*x)]
107+
images.extend(image_links)
107108

108-
if attachments:
109-
att = attachments[0]
110-
em.add_field(name='File Attachment', value=f'[{att.filename}]({att.url})')
109+
embedded_image = False
110+
111+
prioritize_uploads = any(i[1] is not None for i in images)
112+
113+
additional_count = 1
114+
115+
for att in images:
116+
if is_image_url(*att) and not embedded_image and att[1] if prioritize_uploads else True:
117+
em.set_image(url=att[0])
118+
embedded_image = True
119+
elif att[1] is not None:
120+
link = f'[{att[1]}]({att[0]})'
121+
em.add_field(name=f'Additional Image upload ({additional_count})', value=link, inline=False)
122+
additional_count += 1
123+
124+
file_upload_count = 1
125+
126+
for att in attachments:
127+
em.add_field(name=f'File upload ({file_upload_count})', value=f'[{att[1]}]({att[0]})')
128+
file_upload_count += 1
111129

112130
if from_mod:
113131
em.color = discord.Color.green()

0 commit comments

Comments
 (0)