2024-12-27 03:58:26 +00:00
using System ;
using System.IO ;
using System.IO.Compression ;
2022-02-21 08:40:09 +00:00
using System.Threading.Tasks ;
using DiscordChatExporter.Core.Discord ;
using DiscordChatExporter.Core.Discord.Data ;
using DiscordChatExporter.Core.Exporting ;
using DiscordChatExporter.Core.Exporting.Filtering ;
using DiscordChatExporter.Core.Exporting.Partitioning ;
2022-08-21 07:33:27 +00:00
namespace SupportChild ;
internal static class Transcriber
2022-02-21 08:40:09 +00:00
{
2024-12-27 03:58:26 +00:00
private static string transcriptDir = "./transcripts" ; // TODO: Should be local variable (or come from the config class) when added to config to avoid race conditions when reloading
2024-10-29 10:05:16 +00:00
2024-10-29 09:10:37 +00:00
internal static async Task ExecuteAsync ( ulong channelID , uint ticketID )
{
DiscordClient discordClient = new DiscordClient ( Config . token ) ;
ChannelExporter exporter = new ChannelExporter ( discordClient ) ;
2024-10-29 10:05:16 +00:00
if ( ! string . IsNullOrEmpty ( SupportChild . commandLineArgs . transcriptDir ) )
{
transcriptDir = SupportChild . commandLineArgs . transcriptDir ;
}
if ( ! Directory . Exists ( transcriptDir ) )
2024-10-29 09:10:37 +00:00
{
2024-10-29 10:05:16 +00:00
Directory . CreateDirectory ( transcriptDir ) ;
2024-10-29 09:10:37 +00:00
}
2024-12-27 03:58:26 +00:00
string htmlPath = GetHtmlPath ( ticketID ) ;
string zipPath = GetZipPath ( ticketID ) ;
string assetDirPath = GetAssetDirPath ( ticketID ) ;
string assetDirName = GetAssetDirName ( ticketID ) ;
string htmlFilename = GetHTMLFilename ( ticketID ) ;
// TODO: Delete existing files
if ( File . Exists ( htmlPath ) )
{
File . Delete ( htmlPath ) ;
}
if ( File . Exists ( zipPath ) )
{
File . Delete ( zipPath ) ;
}
if ( Directory . Exists ( assetDirPath ) )
{
Directory . Delete ( assetDirPath , true ) ;
}
2024-10-29 09:10:37 +00:00
Channel channel = await discordClient . GetChannelAsync ( new Snowflake ( channelID ) ) ;
Guild guild = await discordClient . GetGuildAsync ( channel . GuildId ) ;
2024-10-29 09:52:02 +00:00
ExportRequest request = new (
guild ,
channel ,
2024-12-27 03:58:26 +00:00
htmlPath ,
assetDirPath ,
2024-10-29 09:52:02 +00:00
ExportFormat . HtmlDark ,
null ,
null ,
PartitionLimit . Null ,
MessageFilter . Null ,
true ,
2024-12-27 03:58:26 +00:00
true ,
true ,
"en-SE" ,
2024-10-29 09:52:02 +00:00
true
) ;
2024-10-29 09:10:37 +00:00
await exporter . ExportChannelAsync ( request ) ;
2024-12-27 03:58:26 +00:00
string [ ] assetFiles ;
try
{
assetFiles = Directory . GetFiles ( assetDirPath ) ;
}
catch ( Exception )
{
assetFiles = [ ] ;
}
if ( assetFiles . Length > 0 )
{
using ( ZipArchive zip = ZipFile . Open ( zipPath , ZipArchiveMode . Create ) )
{
zip . CreateEntryFromFile ( htmlPath , htmlFilename ) ;
foreach ( string assetFile in assetFiles )
{
zip . CreateEntryFromFile ( assetFile , assetDirName + "/" + Path . GetFileName ( assetFile ) ) ;
}
}
Directory . Delete ( assetDirPath , true ) ;
}
2024-10-29 09:10:37 +00:00
}
2024-12-27 03:58:26 +00:00
internal static string GetHtmlPath ( uint ticketNumber )
2024-10-29 09:10:37 +00:00
{
2024-12-27 03:58:26 +00:00
return transcriptDir + "/" + GetHTMLFilename ( ticketNumber ) ;
2024-10-29 09:10:37 +00:00
}
2024-12-27 03:58:26 +00:00
internal static string GetZipPath ( uint ticketNumber )
{
return transcriptDir + "/" + GetZipFilename ( ticketNumber ) ;
}
internal static string GetAssetDirPath ( uint ticketNumber )
{
return transcriptDir + "/" + GetAssetDirName ( ticketNumber ) ;
}
internal static string GetAssetDirName ( uint ticketNumber )
{
return "ticket-" + ticketNumber . ToString ( "00000" ) + "-assets" ;
}
internal static string GetHTMLFilename ( uint ticketNumber )
2024-10-29 09:10:37 +00:00
{
return "ticket-" + ticketNumber . ToString ( "00000" ) + ".html" ;
}
2024-12-27 03:58:26 +00:00
internal static string GetZipFilename ( uint ticketNumber )
{
return "ticket-" + ticketNumber . ToString ( "00000" ) + ".zip" ;
}
2022-05-17 13:11:57 +00:00
}