A Library to Export your Discord Chats to a HTML File via your Python-Bot!
Get Started • Bug Report • Request Feature
Note
This Package is Only Usable for discord.py Bots or Forks that Use the Same Namespace (discord
), such as PyCord.
Python 3.8 or Higher is Required!
This Package is NOT Available on PyPI, but You Can Install it via GitHub:
pip install git+https://github.com/1337Syntax/DiscordChatExporterPy
There are 3 Methods Available to Export the Channel:
Basic Usage via .quick_export()
- channel:
discord.abc.Messageable
- The Channel to Export
- bot: Optional[
discord.Client
]
- The Bot Instance to Use for Fetching
- Optional[
discord.Message
]
- The Message of the Export if Successful
import discord from discord.ext import commands import chat_exporter bot = commands.Bot(...) @bot.command() @commands.guild_only() async def export(ctx: commands.Context): await chat_exporter.quick_export(ctx.channel, bot)
Custom Usage via .export()
- channel:
discord.abc.Messageable
- The Channel to Export
- limit: Optional[
int
]
- The Limit of Messages to Capture
- bot: Optional[
discord.Client
]
- The Bot Instance to Use for Fetching
- military_time: Optional[
bool
]
- Whether to Use Military Time
- before: Optional[
datetime.datetime
]
- The Time to Capture Messages Before
- after: Optional[
datetime.datetime
]
- The Time to Capture Messages After
- attachment_handler: Optional[
AttachmentHandler
]
- The Attachment Handler to Use
- Optional[
str
]
- The HTML of the Export
import discord from discord.ext import commands import chat_exporter import io bot = commands.Bot(...) @bot.command() @commands.guild_only() async def export(ctx: commands.Context): transcript = await chat_exporter.export( ctx.channel, bot=bot, military_time=False, ) if transcript is None: # Failed to Create Transcript - Traceback is Printed to Console return await ctx.reply( file=discord.File( io.StringIO(transcript), filename="transcript.html", ) )
Advanced Usage via .raw_export()
- channel:
discord.abc.Messageable
- The Channel to Export
- messages:
List[discord.Message]
- The Messages to Export
- bot: Optional[
discord.Client
]
- The Bot Instance to Use for Fetching
- military_time: Optional[
bool
]
- Whether to Use Military Time
- attachment_handler: Optional[
AttachmentHandler
]
- The Attachment Handler to Use
- Optional[
str
]
- The HTML of the Export
import discord from discord.ext import commands import chat_exporter import io bot = commands.Bot(...) @bot.command() @commands.guild_only() async def export(ctx: commands.Context): transcript = await chat_exporter.raw_export( ctx.channel, messages=[msg async for msg in ctx.channel.history(limit=None, oldest_first=True)], bot=bot, ) if transcript is None: # Failed to Create Transcript - Traceback is Printed to Console return await ctx.reply( file=discord.File( io.StringIO(transcript), filename="transcript.html", ) )
As Discord has Restricted their CDN so that Attachment Proxy-URLs are Temporary (hence the 'Broken'/Invalid Attachments in Transcripts), You have to Provide an AttachmentHandler
to Resolve it.
If You Do Not Provide an AttachmentHandler
, the Library will Use the Default (Temporary) Proxy-URLs.
Creating your Own AttachmentHandler
(Recommended)
All Custom
AttachmentHandler
Classes Must Inherit fromAttachmentHandler
& Implement theprocess_asset
Method.
process_asset
:
- Parameters:
- attachment:
discord.Attachment
- The Attachment to Process
- Returns:
discord.Attachment
- The Attachment Object with Updated URLs
class MyAttachmentHandler(chat_exporter.AttachmentHandler): def __init__(self, *args, **kwargs) -> None: ... # Your Initialisation Logic Here (If Any) async def process_asset(self, attachment: discord.Attachment) -> discord.Attachment: new_url = ... # Your Upload Logic Here attachment.url = new_url attachment.proxy_url = new_url return attachment # Return the Attachment Object with Updated URLs attachment_handler = MyAttachmentHandler(...) ... # In your Code: transcript = await chat_exporter.export( ..., attachment_handler=attachment_handler, )
Storing Attachments Locally via AttachmentToLocalFileHostHandler
This Class Stores the Attachments Locally on the File System & Provides a (Public) URL to Access it.
- base_path:
str
- The Base Path to Store the Attachments
- url_base:
str
- The Base URL to Access the Attachments
attachment_handler = chat_exporter.AttachmentToLocalFileHostHandler( base_path="/usr/share/assets", url_base="https://your-domain.com/assets/", ) ... # In your Code: transcript = await chat_exporter.export( ..., attachment_handler=attachment_handler, )
Sending Attachments to a Discord Channel via AttachmentToDiscordChannel
(NOT Recommended)
This Handler Sends the Attachments to a Discord Channel & Provides the New (but Still Temporary) Proxy-URLs to Access it.
- channel:
discord.ab.Messageable
- The Channel to Store the Attachments
attachment_handler = chat_exporter.AttachmentToDiscordChannel( channel=bot.get_channel(...), ) ... # In your Code: transcript = await chat_exporter.export( ..., attachment_handler=attachment_handler, )
Generating an Embed to Link the Transcript via .quick_link()
- channel:
discord.abc.Messageable
- The Channel to Send the Link
- message:
discord.Message
- The Message to Get the Transcript From
discord.Message
- The Message of the Link
@bot.command() @commands.guild_only() async def export(ctx: commands.Context): output = await chat_exporter.quick_export(...) if output is None: # Failed to Create Transcript - Traceback is Printed to Console return await chat_exporter.quick_link(ctx.channel, output)
Generating a Link to View the Transcript via .link()
- message:
discord.Message
- The Message to Get the Transcript From
str
- The URL of the Transcript
@bot.command() @commands.guild_only() async def export(ctx: commands.Context): output = await chat_exporter.quick_export(...) if output is None: # Failed to Create Transcript - Traceback is Printed to Console return url = chat_exporter.link(output) await ctx.reply(f"[View Transcript]({url})")
If You Found a Bug or Have a Feature Request, Please Open an Issue.
If You Want to Contribute to the Project, Please Fork the Repository & Install the Development Requirements from requirements/dev.txt
. Then when Ready, Open a Pull Request!
This Package was Imported from the Original DiscordChatExporterPy.
This Project is Licensed Under the GNU General Public License v3.0.