mirror of
https://github.com/Cog-Creators/Red-DiscordBot.git
synced 2025-11-06 03:08:55 -05:00
[Tunnel] Add use_cached and images_only kwargs to files_from_attach (#2887)
* feat(tunnel): add `use_cached` kwarg re #2885 * feat(tunnel): add `images_only` kwarg re #2885 * chore(changelog): add towncrier entry
This commit is contained in:
parent
6e3ccc1a21
commit
59e7d063a0
1
changelog.d/2885.misc.rst
Normal file
1
changelog.d/2885.misc.rst
Normal file
@ -0,0 +1 @@
|
|||||||
|
[Tunnel] Added ``use_cached`` and ``images_only`` kwargs to `redbot.core.utils.Tunnel.files_from_attach`
|
||||||
@ -132,7 +132,9 @@ class Tunnel(metaclass=TunnelMeta):
|
|||||||
return rets
|
return rets
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
async def files_from_attach(m: discord.Message) -> List[discord.File]:
|
async def files_from_attach(
|
||||||
|
m: discord.Message, *, use_cached: bool = False, images_only: bool = False
|
||||||
|
) -> List[discord.File]:
|
||||||
"""
|
"""
|
||||||
makes a list of file objects from a message
|
makes a list of file objects from a message
|
||||||
returns an empty list if none, or if the sum of file sizes
|
returns an empty list if none, or if the sum of file sizes
|
||||||
@ -142,6 +144,10 @@ class Tunnel(metaclass=TunnelMeta):
|
|||||||
---------
|
---------
|
||||||
m: `discord.Message`
|
m: `discord.Message`
|
||||||
A message to get attachments from
|
A message to get attachments from
|
||||||
|
use_cached: `bool`
|
||||||
|
Whether to use ``proxy_url`` rather than ``url`` when downloading the attachment
|
||||||
|
images_only: `bool`
|
||||||
|
Whether only image attachments should be added to returned list
|
||||||
|
|
||||||
Returns
|
Returns
|
||||||
-------
|
-------
|
||||||
@ -153,8 +159,16 @@ class Tunnel(metaclass=TunnelMeta):
|
|||||||
max_size = 8 * 1000 * 1000
|
max_size = 8 * 1000 * 1000
|
||||||
if m.attachments and sum(a.size for a in m.attachments) <= max_size:
|
if m.attachments and sum(a.size for a in m.attachments) <= max_size:
|
||||||
for a in m.attachments:
|
for a in m.attachments:
|
||||||
|
if images_only and a.height is None:
|
||||||
|
# if this is None, it's not an image
|
||||||
|
continue
|
||||||
_fp = io.BytesIO()
|
_fp = io.BytesIO()
|
||||||
await a.save(_fp)
|
try:
|
||||||
|
await a.save(_fp, use_cached=use_cached)
|
||||||
|
except discord.HTTPException as e:
|
||||||
|
# this is required, because animated webp files aren't cached
|
||||||
|
if not (e.status == 415 and images_only and use_cached):
|
||||||
|
raise
|
||||||
files.append(discord.File(_fp, filename=a.filename))
|
files.append(discord.File(_fp, filename=a.filename))
|
||||||
return files
|
return files
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user