From 59e7d063a0b8f62ddd605f33a28634df592fe11f Mon Sep 17 00:00:00 2001 From: jack1142 <6032823+jack1142@users.noreply.github.com> Date: Sat, 28 Sep 2019 23:28:14 +0200 Subject: [PATCH] [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 --- changelog.d/2885.misc.rst | 1 + redbot/core/utils/tunnel.py | 18 ++++++++++++++++-- 2 files changed, 17 insertions(+), 2 deletions(-) create mode 100644 changelog.d/2885.misc.rst 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