This commit is contained in:
Tyrrrz
2021-02-22 03:15:09 +02:00
parent bed0ade732
commit ebe4d58a42
101 changed files with 330 additions and 310 deletions

View File

@@ -4,9 +4,9 @@ using CliFx;
using CliFx.Attributes;
using CliFx.Exceptions;
using CliFx.Utilities;
using DiscordChatExporter.Domain.Discord;
using DiscordChatExporter.Domain.Discord.Models;
using DiscordChatExporter.Domain.Exporting;
using DiscordChatExporter.Core.Discord;
using DiscordChatExporter.Core.Discord.Data;
using DiscordChatExporter.Core.Exporting;
namespace DiscordChatExporter.Cli.Commands.Base
{
@@ -36,11 +36,15 @@ namespace DiscordChatExporter.Cli.Commands.Base
[CommandOption("dateformat", Description = "Format used when writing dates.")]
public string DateFormat { get; init; } = "dd-MMM-yy hh:mm tt";
protected ChannelExporter GetChannelExporter() => new(GetDiscordClient());
private ChannelExporter? _channelExporter;
protected ChannelExporter Exporter => _channelExporter ??= new ChannelExporter(Discord);
protected async ValueTask ExportAsync(IConsole console, Guild guild, Channel channel)
{
console.Output.Write($"Exporting channel '{channel.Category} / {channel.Name}'... ");
await console.Output.WriteAsync(
$"Exporting channel '{channel.Category} / {channel.Name}'... "
);
var progress = console.CreateProgressTicker();
var request = new ExportRequest(
@@ -56,21 +60,21 @@ namespace DiscordChatExporter.Cli.Commands.Base
DateFormat
);
await GetChannelExporter().ExportChannelAsync(request, progress);
await Exporter.ExportChannelAsync(request, progress);
console.Output.WriteLine();
console.Output.WriteLine("Done.");
await console.Output.WriteLineAsync();
await console.Output.WriteLineAsync("Done.");
}
protected async ValueTask ExportAsync(IConsole console, Channel channel)
{
var guild = await GetDiscordClient().GetGuildAsync(channel.GuildId);
var guild = await Discord.GetGuildAsync(channel.GuildId);
await ExportAsync(console, guild, channel);
}
protected async ValueTask ExportAsync(IConsole console, Snowflake channelId)
{
var channel = await GetDiscordClient().GetChannelAsync(channelId);
var channel = await Discord.GetChannelAsync(channelId);
await ExportAsync(console, channel);
}

View File

@@ -6,10 +6,10 @@ using System.Threading.Tasks;
using CliFx;
using CliFx.Attributes;
using CliFx.Utilities;
using DiscordChatExporter.Domain.Discord.Models;
using DiscordChatExporter.Domain.Exceptions;
using DiscordChatExporter.Domain.Exporting;
using DiscordChatExporter.Domain.Utilities;
using DiscordChatExporter.Core.Discord.Data;
using DiscordChatExporter.Core.Exceptions;
using DiscordChatExporter.Core.Exporting;
using DiscordChatExporter.Core.Utils.Extensions;
using Gress;
using Tyrrrz.Extensions;
@@ -25,7 +25,10 @@ namespace DiscordChatExporter.Cli.Commands.Base
// This uses a different route from ExportCommandBase.ExportAsync() because it runs
// in parallel and needs another way to report progress to console.
console.Output.Write($"Exporting {channels.Count} channels... ");
await console.Output.WriteAsync(
$"Exporting {channels.Count} channels... "
);
var progress = console.CreateProgressTicker();
var operations = progress.Wrap().CreateOperations(channels.Count);
@@ -39,7 +42,7 @@ namespace DiscordChatExporter.Cli.Commands.Base
try
{
var guild = await GetDiscordClient().GetGuildAsync(channel.GuildId);
var guild = await Discord.GetGuildAsync(channel.GuildId);
var request = new ExportRequest(
guild,
@@ -54,7 +57,7 @@ namespace DiscordChatExporter.Cli.Commands.Base
DateFormat
);
await GetChannelExporter().ExportChannelAsync(request, operation);
await Exporter.ExportChannelAsync(request, operation);
Interlocked.Increment(ref successfulExportCount);
}
@@ -68,12 +71,12 @@ namespace DiscordChatExporter.Cli.Commands.Base
}
}, ParallelLimit.ClampMin(1));
console.Output.WriteLine();
await console.Output.WriteLineAsync();
foreach (var (channel, error) in errors)
console.Error.WriteLine($"Channel '{channel}': {error}");
await console.Error.WriteLineAsync($"Channel '{channel}': {error}");
console.Output.WriteLine($"Successfully exported {successfulExportCount} channel(s).");
await console.Output.WriteLineAsync($"Successfully exported {successfulExportCount} channel(s).");
}
}
}

View File

@@ -1,26 +1,27 @@
using System.Threading.Tasks;
using CliFx;
using CliFx.Attributes;
using DiscordChatExporter.Domain.Discord;
using DiscordChatExporter.Core.Discord;
namespace DiscordChatExporter.Cli.Commands.Base
{
public abstract class TokenCommandBase : ICommand
{
[CommandOption("token", 't', IsRequired = true, EnvironmentVariableName = "DISCORD_TOKEN", Description = "Authorization token.")]
[CommandOption("token", 't', IsRequired = true, EnvironmentVariableName = "DISCORD_TOKEN", Description = "Authentication token.")]
public string TokenValue { get; init; } = "";
[CommandOption("bot", 'b', EnvironmentVariableName = "DISCORD_TOKEN_BOT", Description = "Authorize as a bot.")]
[CommandOption("bot", 'b', EnvironmentVariableName = "DISCORD_TOKEN_BOT", Description = "Authenticate as a bot.")]
public bool IsBotToken { get; init; }
protected AuthToken GetAuthToken() => new(
private AuthToken GetAuthToken() => new(
IsBotToken
? AuthTokenType.Bot
: AuthTokenType.User,
TokenValue
);
protected DiscordClient GetDiscordClient() => new(GetAuthToken());
private DiscordClient? _discordClient;
protected DiscordClient Discord => _discordClient ??= new DiscordClient(GetAuthToken());
public abstract ValueTask ExecuteAsync(IConsole console);
}