mirror of
https://github.com/Cog-Creators/Red-DiscordBot.git
synced 2025-11-06 03:08:55 -05:00
[Utils] Text to file (#2849)
This commit is contained in:
parent
580c4429e8
commit
43da727a9f
2
changelog.d/2849.misc.rst
Normal file
2
changelog.d/2849.misc.rst
Normal file
@ -0,0 +1,2 @@
|
||||
Added the function ``redbot.core.utils.chat_formatting.text_to_file`` to
|
||||
prepare a long text to be send as a file.
|
||||
@ -1,6 +1,7 @@
|
||||
import itertools
|
||||
import datetime
|
||||
from typing import Sequence, Iterator, List, Optional
|
||||
from io import BytesIO
|
||||
|
||||
import discord
|
||||
|
||||
@ -429,3 +430,30 @@ def humanize_timedelta(
|
||||
strings.append(f"{period_value} {unit}")
|
||||
|
||||
return ", ".join(strings)
|
||||
|
||||
|
||||
def text_to_file(
|
||||
text: str, filename: str = "file.txt", *, spoiler: bool = False, encoding: str = "utf-8"
|
||||
):
|
||||
"""Prepares text to be sent as a file on Discord, without character limit.
|
||||
|
||||
This writes text into a bytes object that can be used for the ``file`` or ``files`` parameters
|
||||
of :meth:`discord.abc.Messageable.send`.
|
||||
|
||||
Parameters
|
||||
----------
|
||||
text: str
|
||||
The text to put in your file.
|
||||
filename: str
|
||||
The name of the file sent. Defaults to ``file.txt``.
|
||||
spoiler: bool
|
||||
Whether the attachment is a spoiler. Defaults to ``False``.
|
||||
|
||||
Returns
|
||||
-------
|
||||
discord.File
|
||||
The file containing your text.
|
||||
|
||||
"""
|
||||
file = BytesIO(text.encode(encoding))
|
||||
return discord.File(file, filename, spoiler=spoiler)
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user