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

[Utils] Text to file #2849

Merged
merged 10 commits into from Aug 26, 2019
Merged

[Utils] Text to file #2849

merged 10 commits into from Aug 26, 2019

Conversation

laggron42
Copy link
Contributor

@laggron42 laggron42 commented Jul 10, 2019

Type

  • Bugfix
  • Enhancement
  • New feature

Description of the changes

This is a short util that will convert a long string into a discord.File object, useful to bypass the character limit. Nothing is wrote on the disk, io.BytesIO is used instead.

Example

from redbot.core.utils.chat_formatting import text_to_file

@commands.command()
async def getchannelmsg(self, amount: str, channel: discord.TextChannel = None):
    text = ""
    async for message in channel.history(limit=amount):
        text += f"{message.author}: {message.content}"
    if len(text) > 2000:
        await ctx.send(file=text_to_file(text))
    else:
        await ctx.send(text)

@laggron42 laggron42 requested a review from tekulvw as a code owner Jul 10, 2019
@jack1142
Copy link
Member

@jack1142 jack1142 commented Jul 10, 2019

Please use StringIO instead as you're using just text in this file, not bytes content.
Also, if other decide BytesIO is fine, bytes requires encoding passed to it, for example bytes(text, encoding="utf-8")

@zephyrkul
Copy link
Contributor

@zephyrkul zephyrkul commented Jul 10, 2019

iirc, discord.File requires the use of BytesIO for in-memory streams.

@jack1142
Copy link
Member

@jack1142 jack1142 commented Jul 10, 2019

discord.File support any file object, I also tested it specifically with StringIO before I posted.

@laggron42
Copy link
Contributor Author

@laggron42 laggron42 commented Jul 10, 2019

I tested with StringIO and it just returned an empty file.
image
^ following the python tutorial on how to use it, you can see the "0 bytes"

And I tested it without the encoding, it works fine.

@zephyrkul
Copy link
Contributor

@zephyrkul zephyrkul commented Jul 10, 2019

https://discordpy.readthedocs.io/en/stable/api.html#file

The type requires BufferedIOBase. StringIO does not inherit from this base class.

Copy link
Member

@Tobotimus Tobotimus left a comment

Thanks, this is a pretty handy util :)

redbot/core/utils/chat_formatting.py Outdated Show resolved Hide resolved
redbot/core/utils/chat_formatting.py Outdated Show resolved Hide resolved
@Tobotimus Tobotimus added the Type: Feature label Jul 11, 2019
@Tobotimus Tobotimus added this to the 3.2.0 milestone Jul 11, 2019
@DevilXD
Copy link
Contributor

@DevilXD DevilXD commented Jul 11, 2019

@retke I had the same problem when I wanted to send a file to the chat long time ago, and this is what was explained to me: https://i.imgur.com/nJnT4Wt.png
Your example should very much work after adding a seek(0) right before sending.

As for BytesIO/StringIO usage, one of the things I was trying to send in the chat as StringIO simply failed with some weird encoding error, that went away after switching to BytesIO with UTF-8 encoding set, so I'd recommend that. It was quite complicated mess of markdown-rich reddit post generator if this gives you any context to what could've happened. .encode() uses UTF-8 by default.

Signed-off-by: retke <laggron42@gmail.com>
@mikeshardmind mikeshardmind added the Changelog Entry: Pending label Jul 26, 2019
redbot/core/utils/chat_formatting.py Outdated Show resolved Hide resolved
redbot/core/utils/chat_formatting.py Outdated Show resolved Hide resolved
redbot/core/utils/chat_formatting.py Show resolved Hide resolved
@laggron42 laggron42 requested a review from Twentysix26 as a code owner Aug 23, 2019
@laggron42
Copy link
Contributor Author

@laggron42 laggron42 commented Aug 23, 2019

Should be ready now.

@Flame442 Flame442 removed the Changelog Entry: Pending label Aug 23, 2019
@Tobotimus Tobotimus merged commit 43da727 into Cog-Creators:V3/develop Aug 26, 2019
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Type: Feature
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

7 participants