mirror of
https://github.com/Tyrrrz/DiscordChatExporter.git
synced 2024-09-19 12:18:48 -04:00
Change command line argument style
This commit is contained in:
parent
c7e246540b
commit
83ea7ae317
5 changed files with 51 additions and 41 deletions
|
@ -14,7 +14,6 @@
|
|||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="CommandLineParser" Version="1.9.71" />
|
||||
<PackageReference Include="HtmlAgilityPack" Version="1.5.1" />
|
||||
<PackageReference Include="Newtonsoft.json" Version="10.0.3" />
|
||||
<PackageReference Include="Tyrrrz.Extensions" Version="1.4.0" />
|
||||
|
|
15
DiscordChatExporter/Models/Options.cs
Normal file
15
DiscordChatExporter/Models/Options.cs
Normal file
|
@ -0,0 +1,15 @@
|
|||
namespace DiscordChatExporter.Models
|
||||
{
|
||||
public class Options
|
||||
{
|
||||
public string Token { get; }
|
||||
|
||||
public string ChannelId { get; }
|
||||
|
||||
public Options(string token, string channelId)
|
||||
{
|
||||
Token = token;
|
||||
ChannelId = channelId;
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,32 +0,0 @@
|
|||
using CommandLine;
|
||||
using CommandLine.Text;
|
||||
|
||||
namespace DiscordChatExporter
|
||||
{
|
||||
public class Options
|
||||
{
|
||||
[Option('t', "token", Required = true, HelpText = "Discord access token")]
|
||||
public string Token { get; set; }
|
||||
|
||||
[Option('c', "channel", Required = true, HelpText = "ID of the text channel to export")]
|
||||
public string ChannelId { get; set; }
|
||||
|
||||
[HelpOption]
|
||||
public string GetUsage()
|
||||
{
|
||||
var help = new HelpText
|
||||
{
|
||||
Heading = new HeadingInfo("DiscordChatExporter"),
|
||||
Copyright = new CopyrightInfo("Alexey 'Tyrrrz' Golub", 2017),
|
||||
AdditionalNewLineAfterOption = true,
|
||||
AddDashesToOption = true
|
||||
};
|
||||
help.AddPreOptionsLine("Usage: DiscordChatExporter.exe " +
|
||||
"-t REkOTVqm9RWOTNOLCdiuMpWd.QiglBz.Lub0E0TZ1xX4ZxCtnwtpBhWt3v1 " +
|
||||
"-c 459360869055190534");
|
||||
help.AddOptions(this);
|
||||
|
||||
return help;
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,30 +1,59 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text.RegularExpressions;
|
||||
using System.Threading.Tasks;
|
||||
using DiscordChatExporter.Models;
|
||||
using DiscordChatExporter.Services;
|
||||
using Tyrrrz.Extensions;
|
||||
|
||||
namespace DiscordChatExporter
|
||||
{
|
||||
public static class Program
|
||||
{
|
||||
private static readonly Options Options = new Options();
|
||||
|
||||
private static readonly DiscordApiService DiscordApiService = new DiscordApiService();
|
||||
private static readonly ExportService ExportService = new ExportService();
|
||||
|
||||
private static Options GetOptions(string[] args)
|
||||
{
|
||||
// Parse the arguments
|
||||
var argsDic = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase);
|
||||
foreach (string arg in args)
|
||||
{
|
||||
var match = Regex.Match(arg, "/(.*?):\"?(.*?)\"?$");
|
||||
string key = match.Groups[1].Value;
|
||||
string value = match.Groups[2].Value;
|
||||
|
||||
if (key.IsBlank())
|
||||
continue;
|
||||
|
||||
argsDic[key] = value;
|
||||
}
|
||||
|
||||
// Extract required arguments
|
||||
string token = argsDic.GetOrDefault("token");
|
||||
string channelId = argsDic.GetOrDefault("channelId");
|
||||
|
||||
// Verify arguments
|
||||
if (token.IsBlank() || channelId.IsBlank())
|
||||
throw new ArgumentException("Some or all required command line arguments are missing");
|
||||
|
||||
// Create option set
|
||||
return new Options(token, channelId);
|
||||
}
|
||||
|
||||
private static async Task MainAsync(string[] args)
|
||||
{
|
||||
// Parse cmd args
|
||||
CommandLine.Parser.Default.ParseArgumentsStrict(args, Options);
|
||||
var options = GetOptions(args);
|
||||
|
||||
// Get messages
|
||||
Console.WriteLine("Getting messages...");
|
||||
var messages = await DiscordApiService.GetMessagesAsync(Options.Token, Options.ChannelId);
|
||||
var chatLog = new ChatLog(Options.ChannelId, messages);
|
||||
var messages = await DiscordApiService.GetMessagesAsync(options.Token, options.ChannelId);
|
||||
var chatLog = new ChatLog(options.ChannelId, messages);
|
||||
|
||||
// Export
|
||||
Console.WriteLine("Exporting messages...");
|
||||
ExportService.Export($"{Options.ChannelId}.html", chatLog);
|
||||
ExportService.Export($"{options.ChannelId}.html", chatLog);
|
||||
}
|
||||
|
||||
public static void Main(string[] args)
|
||||
|
|
|
@ -25,11 +25,10 @@ You can get your token by opening the Discord app, pressing `Ctrl+Shift+I`, navi
|
|||
|
||||
You can get the channel ID by enabling `Developer Mode` in `Settings > Appearance` and then right clicking on the channel and clicking on `Copy ID`.
|
||||
|
||||
- `DiscordChatExporter.exe -t REkOTVqm9RWOTNOLCdiuMpWd.QiglBz.Lub0E0TZ1xX4ZxCtnwtpBhWt3v1 -c 459360869055190534`
|
||||
- `DiscordChatExporter.exe /token:REkOTVqm9RWOTNOLCdiuMpWd.QiglBz.Lub0E0TZ1xX4ZxCtnwtpBhWt3v1 /channelId:459360869055190534`
|
||||
|
||||
## Libraries used
|
||||
|
||||
- [CommandLineParser](https://github.com/gsscoder/commandline)
|
||||
- [HtmlAgilityPack](https://github.com/zzzprojects/html-agility-pack)
|
||||
- [Newtonsoft.Json](https://github.com/JamesNK/Newtonsoft.Json)
|
||||
- [Tyrrrz.Extensions](https://github.com/Tyrrrz/Extensions)
|
||||
|
|
Loading…
Reference in a new issue