diff --git a/changelog.d/2885.misc.rst b/changelog.d/2885.misc.rst new file mode 100644 index 000000000..d7e2ac3db --- /dev/null +++ b/changelog.d/2885.misc.rst @@ -0,0 +1 @@ +[Tunnel] Added ``use_cached`` and ``images_only`` kwargs to `redbot.core.utils.Tunnel.files_from_attach` \ No newline at end of file diff --git a/redbot/core/utils/tunnel.py b/redbot/core/utils/tunnel.py index e1655ee78..3081364a0 100644 --- a/redbot/core/utils/tunnel.py +++ b/redbot/core/utils/tunnel.py @@ -132,7 +132,9 @@ class Tunnel(metaclass=TunnelMeta): return rets @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 returns an empty list if none, or if the sum of file sizes @@ -142,6 +144,10 @@ class Tunnel(metaclass=TunnelMeta): --------- m: `discord.Message` 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 ------- @@ -153,8 +159,16 @@ class Tunnel(metaclass=TunnelMeta): max_size = 8 * 1000 * 1000 if m.attachments and sum(a.size for a in m.attachments) <= max_size: 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() - 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)) return files