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

Add asset channel as way to export assets #91

Merged
merged 10 commits into from
Sep 15, 2023
16 changes: 9 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -94,13 +94,14 @@ This would be the main function to use within chat-exporter.

**Optional Argument(s):**<br/>
`limit`: Integer value to set the limit (amount of messages) the chat exporter gathers when grabbing the history (default=unlimited).<br/>
`tz_info`: String value of a [TZ Database name](https://en.wikipedia.org/wiki/List_of_tz_database_time_zones#List) to set a custom timezone for the exported messages (default=UTC)<br/>
`guild`: `discord.Guild` object which can be passed in to solve bugs for certain forks<br/>
`military_time`: Boolean value to set a 24h format for times within your exported chat (default=False | 12h format)<br/>
`fancy_times`: Boolean value which toggles the 'fancy times' (Today|Yesterday|Day)<br/>
`before`: `datetime.datetime` object which allows to gather messages from before a certain date
`after`: `datetime.datetime` object which allows to gather messages from after a certain date
`bot`: `commands.Bot` object to gather members who are no longer in your guild.
`tz_info`: String value of a [TZ Database name](https://en.wikipedia.org/wiki/List_of_tz_database_time_zones#List) to set a custom timezone for the exported messages (default=UTC).<br/>
`guild`: `discord.Guild` object which can be passed in to solve bugs for certain forks.<br/>
`military_time`: Boolean value to set a 24h format for times within your exported chat (default=False | 12h format).<br/>
`fancy_times`: Boolean value which toggles the 'fancy times' (Today|Yesterday|Day).<br/>
`before`: `datetime.datetime` object which allows to gather messages from before a certain date.<br/>
`after`: `datetime.datetime` object which allows to gather messages from after a certain date.<br/>
`bot`: `commands.Bot` object to gather members who are no longer in your guild.<br/>
`asset_channel`: `discord.TextChannel` object to export assets to in order to make them available after the `channel` got deleted.<br/>

**Return Argument:**<br/>
`transcript`: The HTML build-up for you to construct the HTML File with Discord.
Expand Down Expand Up @@ -149,6 +150,7 @@ This would be for people who want to filter what content to export.
`military_time`: Boolean value to set a 24h format for times within your exported chat (default=False | 12h format)<br/>
`fancy_times`: Boolean value which toggles the 'fancy times' (Today|Yesterday|Day)<br/>
`bot`: `commands.Bot` object to gather members who are no longer in your guild.
`asset_channel`: `discord.TextChannel` object to export assets to in order to make them available after the `channel` got deleted.<br/>

**Return Argument:**<br/>
`transcript`: The HTML build-up for you to construct the HTML File with Discord.
Expand Down
6 changes: 6 additions & 0 deletions chat_exporter/chat_exporter.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ async def export(
before: Optional[datetime.datetime] = None,
after: Optional[datetime.datetime] = None,
support_dev: Optional[bool] = True,
asset_channel: Optional[discord.TextChannel] = None,
):
"""
Create a customised transcript of your Discord channel.
Expand All @@ -74,6 +75,7 @@ async def export(
:param fancy_times: (optional) boolean - set javascript around time display
:param before: (optional) datetime.datetime - allows before time for history
:param after: (optional) datetime.datetime - allows after time for history
:param asset_channel: (optional) discord.TextChannel - channel to save attachments to
:return: string - transcript file make up
"""
if guild:
Expand All @@ -91,6 +93,7 @@ async def export(
after=after,
support_dev=support_dev,
bot=bot,
asset_channel=asset_channel,
).export()
).html

Expand All @@ -104,6 +107,7 @@ async def raw_export(
military_time: Optional[bool] = False,
fancy_times: Optional[bool] = True,
support_dev: Optional[bool] = True,
asset_channel: Optional[discord.TextChannel] = None,
):
"""
Create a customised transcript with your own captured Discord messages
Expand All @@ -115,6 +119,7 @@ async def raw_export(
:param bot: (optional) discord.Client - set getting member role colour
:param military_time: (optional) boolean - set military time (24hour clock)
:param fancy_times: (optional) boolean - set javascript around time display
:param asset_channel: (optional) discord.TextChannel - channel to save attachments to
:return: string - transcript file make up
"""
if guild:
Expand All @@ -132,6 +137,7 @@ async def raw_export(
after=None,
support_dev=support_dev,
bot=bot,
asset_channel=asset_channel
).export()
).html

Expand Down
27 changes: 24 additions & 3 deletions chat_exporter/construct/message.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import html
import io
import traceback
from typing import List, Optional, Union

import aiohttp
from pytz import timezone
from datetime import timedelta

Expand Down Expand Up @@ -61,15 +64,16 @@ def __init__(
military_time: bool,
guild: discord.Guild,
meta_data: dict,
message_dict: dict
message_dict: dict,
asset_channel: Optional[discord.TextChannel]
):
self.message = message
self.previous_message = previous_message
self.pytz_timezone = pytz_timezone
self.military_time = military_time
self.guild = guild
self.message_dict = message_dict

self.asset_channel = asset_channel
self.time_format = "%A, %e %B %Y %I:%M %p"
if self.military_time:
self.time_format = "%A, %e %B %Y %H:%M"
Expand Down Expand Up @@ -246,6 +250,20 @@ async def build_assets(self):
self.embeds += await Embed(e, self.guild).flow()

for a in self.message.attachments:
if self.asset_channel and isinstance(self.asset_channel, discord.TextChannel):
try:
async with aiohttp.ClientSession() as session:
async with session.get(a.url) as res:
if res.status != 200:
res.raise_for_status()
data = io.BytesIO(await res.read())
data.seek(0)
attach = discord.File(data, a.filename)
msg: discord.Message = await self.asset_channel.send(file=attach)
a = msg.attachments[0]
except discord.errors.HTTPException as e:
# discords http errors, including missing permissions
raise e
self.attachments += await Attachment(a, self.guild).flow()

for c in self.message.components:
Expand Down Expand Up @@ -434,6 +452,7 @@ async def gather_messages(
guild: discord.Guild,
pytz_timezone,
military_time,
asset_channel: discord.TextChannel
) -> (str, dict):
message_html: str = ""
meta_data: dict = {}
Expand All @@ -460,7 +479,9 @@ async def gather_messages(
guild,
meta_data,
message_dict,
).construct_message()
asset_channel,
).construct_message()

message_html += content_html
previous_message = message

Expand Down
3 changes: 3 additions & 0 deletions chat_exporter/construct/transcript.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ def __init__(
after: Optional[datetime.datetime],
support_dev: bool,
bot: Optional[discord.Client],
asset_channel: Optional[discord.TextChannel]
):
self.channel = channel
self.messages = messages
Expand All @@ -45,6 +46,7 @@ def __init__(
self.after = after
self.support_dev = support_dev
self.pytz_timezone = pytz_timezone
self.asset_channel = asset_channel

# This is to pass timezone in to mention.py without rewriting
setattr(discord.Guild, "timezone", self.pytz_timezone)
Expand All @@ -58,6 +60,7 @@ async def build_transcript(self):
self.channel.guild,
self.pytz_timezone,
self.military_time,
self.asset_channel
)
await self.export_transcript(message_html, meta_data)
clear_cache()
Expand Down
Loading