-
-
Notifications
You must be signed in to change notification settings - Fork 12
/
Transcriber.cs
139 lines (116 loc) · 3.84 KB
/
Transcriber.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
using System;
using System.IO;
using System.IO.Compression;
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;
namespace SupportBoi;
internal static class Transcriber
{
internal static async Task ExecuteAsync(ulong channelID, uint ticketID)
{
DiscordClient discordClient = new DiscordClient(Config.token);
ChannelExporter exporter = new ChannelExporter(discordClient);
string htmlPath = GetHtmlPath(ticketID);
string zipPath = GetZipPath(ticketID);
string assetDirPath = GetAssetDirPath(ticketID);
string assetDirName = GetAssetDirName(ticketID);
string htmlFilename = GetHTMLFilename(ticketID);
if (File.Exists(htmlPath))
{
File.Delete(htmlPath);
}
if (File.Exists(zipPath))
{
File.Delete(zipPath);
}
if (Directory.Exists(assetDirPath))
{
Directory.Delete(assetDirPath, true);
}
Channel channel = await discordClient.GetChannelAsync(new Snowflake(channelID));
Guild guild = await discordClient.GetGuildAsync(channel.GuildId);
ExportRequest request = new (
guild,
channel,
htmlPath,
assetDirPath,
ExportFormat.HtmlDark,
null,
null,
PartitionLimit.Null,
MessageFilter.Null,
true,
true,
true,
"en-SE",
true
);
await exporter.ExportChannelAsync(request);
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, CompressionLevel.SmallestSize);
foreach (string assetFile in assetFiles)
{
zip.CreateEntryFromFile(assetFile, assetDirName + "/" + Path.GetFileName(assetFile), CompressionLevel.SmallestSize);
}
}
Directory.Delete(assetDirPath, true);
}
}
private static string GetTranscriptDir()
{
string transcriptDir = "./transcripts";
if (!string.IsNullOrEmpty(SupportBoi.commandLineArgs.transcriptDir))
{
transcriptDir = SupportBoi.commandLineArgs.transcriptDir;
}
else if (!string.IsNullOrEmpty(Config.transcriptDir))
{
transcriptDir = Config.transcriptDir;
}
if (!Directory.Exists(transcriptDir))
{
Directory.CreateDirectory(transcriptDir);
}
return transcriptDir;
}
internal static string GetHtmlPath(uint ticketNumber)
{
return GetTranscriptDir() + "/" + GetHTMLFilename(ticketNumber);
}
internal static string GetZipPath(uint ticketNumber)
{
return GetTranscriptDir() + "/" + GetZipFilename(ticketNumber);
}
internal static string GetAssetDirPath(uint ticketNumber)
{
return GetTranscriptDir() + "/" + GetAssetDirName(ticketNumber);
}
internal static string GetAssetDirName(uint ticketNumber)
{
return "ticket-" + ticketNumber.ToString("00000") + "-assets";
}
internal static string GetHTMLFilename(uint ticketNumber)
{
return "ticket-" + ticketNumber.ToString("00000") + ".html";
}
internal static string GetZipFilename(uint ticketNumber)
{
return "ticket-" + ticketNumber.ToString("00000") + ".zip";
}
}