From 3941b31f11ebb265651232709d7ccab0ff245e31 Mon Sep 17 00:00:00 2001 From: Yaakov Schlachter Date: Wed, 18 Jan 2023 20:27:28 +0200 Subject: [PATCH] fix: images sending as 0 bytes in paginator if sent as `io.BytesIO` (#1881) Signed-off-by: Yaakov Schlachter Signed-off-by: BobDotCom <71356958+BobDotCom@users.noreply.github.com> Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Co-authored-by: Lala Sabathil Co-authored-by: BobDotCom <71356958+BobDotCom@users.noreply.github.com> Co-authored-by: plun1331 <49261529+plun1331@users.noreply.github.com> --- CHANGELOG.md | 6 ++++-- discord/ext/pages/pagination.py | 18 ++++++------------ 2 files changed, 10 insertions(+), 14 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index c1275d147c..e79b307ffe 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -31,8 +31,10 @@ These changes are available on the `master` branch, but have not yet been releas ### Fixed -- Fixed a bug in `Page.update_files` where file objects stored in memory were causing an - `AttributeError`. ([#1869](https://github.com/Pycord-Development/pycord/pull/1869)) +- Fixed bugs in `Page.update_files` where file objects stored in memory were causing an + `AttributeError`, and `io.BytesIO` files didn't send properly more than once. + ([#1869](https://github.com/Pycord-Development/pycord/pull/1869) & + [#1881](https://github.com/Pycord-Development/pycord/pull/1881)) ## [2.3.2] - 2022-12-03 diff --git a/discord/ext/pages/pagination.py b/discord/ext/pages/pagination.py index 7bc601e66d..6bffc7e194 100644 --- a/discord/ext/pages/pagination.py +++ b/discord/ext/pages/pagination.py @@ -23,7 +23,6 @@ """ from __future__ import annotations -from io import BufferedReader from typing import List import discord @@ -169,19 +168,14 @@ async def callback(self, interaction: discord.Interaction | None = None): """ def update_files(self) -> list[discord.File] | None: - """Re-opens and reads new file contents for local files if they were updated. - Typically used when the page is changed. + """Updates :class:`discord.File` objects so that they can be sent multiple + times. This is called internally each time the page is sent. """ for file in self._files: - if not isinstance(file.fp, BufferedReader): - continue - with open(file.fp.name, "rb") as fp: # type: ignore - self._files[self._files.index(file)] = discord.File( - fp, # type: ignore - filename=file.filename, - description=file.description, - spoiler=file.spoiler, - ) + if file.fp.closed and (fn := getattr(file.fp, "name", None)): + file.fp = open(fn, "rb") + file.reset() + file.fp.close = lambda: None return self._files @property