Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: images sending as 0 bytes in paginator if sent as io.BytesIO #1881

Merged
merged 11 commits into from
Jan 18, 2023
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ These changes are available on the `master` branch, but have not yet been releas

### Fixed

- Fixed a bug in `Page.update_files` where `io.BytesIO` files didn't send properly more
than once. ([#1881](https://github.com/Pycord-Development/pycord/pull/1881))

- 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))
CompuGenius-Programs marked this conversation as resolved.
Show resolved Hide resolved

Expand Down
18 changes: 6 additions & 12 deletions discord/ext/pages/pagination.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
"""
from __future__ import annotations

from io import BufferedReader
from typing import List

import discord
Expand Down Expand Up @@ -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
Expand Down