GalaxyCogs/exportchannels/exportchannels.py

105 lines
3.5 KiB
Python
Raw Permalink Normal View History

2023-08-11 15:01:58 -04:00
import asyncio
2023-03-20 13:01:10 -04:00
import os
2023-09-24 20:45:00 -04:00
import discord
2023-08-11 15:57:07 -04:00
from redbot.core import Config, checks, commands, data_manager
2023-03-20 13:01:10 -04:00
2023-03-20 13:01:10 -04:00
class ExportChannels(commands.Cog):
2023-03-20 13:03:02 -04:00
"""Custom cog to export channels to JSON and HTML formats using Discord Chat Exporter.
Developed by cswimr and yname."""
2023-03-20 13:01:10 -04:00
def __init__(self, bot):
self.bot = bot
self.config = Config.get_conf(self, identifier=48258471944753312)
self.config.register_global(
bot_token = None
2023-03-20 13:01:10 -04:00
)
2023-09-24 20:29:46 -04:00
self.data_path = data_manager.cog_data_path(self)
2023-03-20 13:01:10 -04:00
2023-08-11 16:49:31 -04:00
class ConfigException(Exception):
pass
2023-03-20 13:01:10 -04:00
2023-08-10 22:22:46 -04:00
async def export(self, channels: list):
token = await self.config.bot_token()
if token is None:
2023-09-24 20:29:46 -04:00
raise(self.ConfigException("Bot token not set!"))
data_path = data_manager.cog_data_path(self)
bundled_data_path = data_manager.bundled_data_path(self)
out = f'{data_path}{os.sep}Exported Channels'
2023-08-10 22:22:46 -04:00
channel = channels[0]
2023-08-11 15:57:41 -04:00
file_location_html = f'{os.sep}{out}{os.sep}%g{os.sep}%c{os.sep}Export.html'
file_location_json = f'{os.sep}{out}{os.sep}%g{os.sep}%c{os.sep}DCE-f.json'
2023-03-20 13:01:10 -04:00
try:
os.mkdir(out)
except FileExistsError:
pass
args = [
'dotnet',
'DiscordChatExporter.Cli.dll',
'export',
'--format', 'HtmlDark',
2023-08-11 15:57:41 -04:00
'--output', file_location_html,
'--token', token,
'--channel', channel,
'--reuse-media',
2023-03-20 13:01:10 -04:00
'--media',
'--fuck_russia', 'true',
]
os.chdir(bundled_data_path)
process_1 = await asyncio.create_subprocess_exec(*args)
while True:
return_code_1 = process_1.poll()
if return_code_1 is not None:
break
2023-03-20 13:01:10 -04:00
args = [
'dotnet',
'DiscordChatExporter.Cli.dll',
'export',
'--format', 'Json',
2023-08-11 15:57:41 -04:00
'--output', file_location_json,
'--token', token,
'--channel', channels,
2023-03-20 13:01:10 -04:00
'--reuse_media',
'--media',
'--markdown', 'false',
2023-03-20 13:01:10 -04:00
'--fuck_russia', 'true',
]
os.chdir(bundled_data_path)
process_2 = await asyncio.create_subprocess_exec(*args)
while True:
return_code_2 = process_2.poll()
if return_code_2 is not None:
break
2023-03-20 13:01:10 -04:00
@commands.group()
@checks.is_owner()
2023-09-24 20:29:46 -04:00
async def exportset(self, ctx: commands.Context):
2023-03-20 13:01:10 -04:00
"""Configuration options for the ExportChannels cog."""
@exportset.command()
@checks.is_owner()
2023-09-24 20:29:46 -04:00
async def token(self, ctx: commands.Context, token: str):
2023-03-20 13:01:10 -04:00
"""Sets the bot token used for Discord Chat Exporter."""
await self.config.bot_token.set({token})
await ctx.send(content="Token set!")
await ctx.delete()
2023-03-20 13:01:10 -04:00
@exportset.command()
@checks.is_owner()
2023-09-24 20:29:46 -04:00
async def checkoutputpath(self, ctx: commands.Context):
2023-03-20 13:01:10 -04:00
"""Checks what file path DCE is outputting to."""
await ctx.send(content=f"{self.data_path}")
@commands.command()
@commands.admin()
2023-09-24 20:29:46 -04:00
async def exportchannel(self, ctx: commands.Context, channel: discord.TextChannel):
2023-03-20 13:01:10 -04:00
"""Exports a channel using Discord Chat Exporter."""
token = await self.config.bot_token()
if token is None:
await ctx.send(content=f"Please set your token with the ``{ctx.prefix}exportset token`` command!")
2023-03-20 13:01:10 -04:00
return
2023-09-24 20:29:46 -04:00
id_list = [thread.id for thread in channel.threads]
id_list.insert(0, channel.id)
await self.export(id_list)