Skip to content

Commit

Permalink
Add support for multiple image and file attachments
Browse files Browse the repository at this point in the history
  • Loading branch information
kyb3r committed Jan 9, 2019
1 parent 98ee9c0 commit 786d672
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 11 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).


# v2.0.9

### Quick Fix
- Support multiple image and file attachments in one message
- This is only possible on mobile so its good to handle it in code.

# v2.0.8

Improvements in commands and new config options available.
Expand Down
2 changes: 1 addition & 1 deletion bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
SOFTWARE.
"""

__version__ = '2.0.8'
__version__ = '2.0.9'

import asyncio
import textwrap
Expand Down
38 changes: 28 additions & 10 deletions core/thread.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,21 +93,39 @@ async def send(self, message, destination=None, from_mod=False, delete_message=T
em.set_author(name=str(author), icon_url=author.avatar_url, url=message.jump_url) # store message id in hidden url

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

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

image_urls = [a.url for a in message.attachments]
image_urls.extend(re.findall(r'(https?://[^\s]+)', message.content))
image_urls = list(filter(is_image_url, image_urls))
attachments = [(a.url, a.filename) for a in message.attachments]

images = [x for x in attachments if is_image_url(*x)]
attachments = [x for x in attachments if not is_image_url(*x)]

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

if attachments:
att = attachments[0]
em.add_field(name='File Attachment', value=f'[{att.filename}]({att.url})')
embedded_image = False

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

additional_count = 1

for att in images:
if is_image_url(*att) and not embedded_image and att[1] if prioritize_uploads else True:
em.set_image(url=att[0])
embedded_image = True
elif att[1] is not None:
link = f'[{att[1]}]({att[0]})'
em.add_field(name=f'Additional Image upload ({additional_count})', value=link, inline=False)
additional_count += 1

file_upload_count = 1

for att in attachments:
em.add_field(name=f'File upload ({file_upload_count})', value=f'[{att[1]}]({att[0]})')
file_upload_count += 1

if from_mod:
em.color = discord.Color.green()
Expand Down

0 comments on commit 786d672

Please sign in to comment.