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);
}

View File

@@ -3,7 +3,7 @@ using System.Threading.Tasks;
using CliFx;
using CliFx.Attributes;
using DiscordChatExporter.Cli.Commands.Base;
using DiscordChatExporter.Domain.Discord.Models;
using DiscordChatExporter.Core.Discord.Data;
namespace DiscordChatExporter.Cli.Commands
{
@@ -15,16 +15,18 @@ namespace DiscordChatExporter.Cli.Commands
public override async ValueTask ExecuteAsync(IConsole console)
{
await base.ExecuteAsync(console);
var channels = new List<Channel>();
// Aggregate channels from all guilds
await foreach (var guild in GetDiscordClient().GetUserGuildsAsync())
await foreach (var guild in Discord.GetUserGuildsAsync())
{
// Skip DMs if instructed to
if (!IncludeDirectMessages && guild.Id == Guild.DirectMessages.Id)
continue;
await foreach (var channel in GetDiscordClient().GetGuildChannelsAsync(guild.Id))
await foreach (var channel in Discord.GetGuildChannelsAsync(guild.Id))
{
channels.Add(channel);
}

View File

@@ -2,7 +2,7 @@
using CliFx;
using CliFx.Attributes;
using DiscordChatExporter.Cli.Commands.Base;
using DiscordChatExporter.Domain.Discord;
using DiscordChatExporter.Core.Discord;
namespace DiscordChatExporter.Cli.Commands
{

View File

@@ -2,8 +2,8 @@
using CliFx;
using CliFx.Attributes;
using DiscordChatExporter.Cli.Commands.Base;
using DiscordChatExporter.Domain.Discord.Models;
using DiscordChatExporter.Domain.Utilities;
using DiscordChatExporter.Core.Discord.Data;
using DiscordChatExporter.Core.Utils.Extensions;
namespace DiscordChatExporter.Cli.Commands
{
@@ -12,7 +12,9 @@ namespace DiscordChatExporter.Cli.Commands
{
public override async ValueTask ExecuteAsync(IConsole console)
{
var channels = await GetDiscordClient().GetGuildChannelsAsync(Guild.DirectMessages.Id);
await base.ExecuteAsync(console);
var channels = await Discord.GetGuildChannelsAsync(Guild.DirectMessages.Id);
await ExportMultipleAsync(console, channels);
}
}

View File

@@ -2,8 +2,8 @@
using CliFx;
using CliFx.Attributes;
using DiscordChatExporter.Cli.Commands.Base;
using DiscordChatExporter.Domain.Discord;
using DiscordChatExporter.Domain.Utilities;
using DiscordChatExporter.Core.Discord;
using DiscordChatExporter.Core.Utils.Extensions;
namespace DiscordChatExporter.Cli.Commands
{
@@ -15,7 +15,9 @@ namespace DiscordChatExporter.Cli.Commands
public override async ValueTask ExecuteAsync(IConsole console)
{
var channels = await GetDiscordClient().GetGuildChannelsAsync(GuildId);
await base.ExecuteAsync(console);
var channels = await Discord.GetGuildChannelsAsync(GuildId);
await ExportMultipleAsync(console, channels);
}
}

View File

@@ -3,9 +3,9 @@ using System.Threading.Tasks;
using CliFx;
using CliFx.Attributes;
using DiscordChatExporter.Cli.Commands.Base;
using DiscordChatExporter.Domain.Discord;
using DiscordChatExporter.Domain.Discord.Models.Common;
using DiscordChatExporter.Domain.Utilities;
using DiscordChatExporter.Core.Discord;
using DiscordChatExporter.Core.Discord.Data.Common;
using DiscordChatExporter.Core.Utils.Extensions;
namespace DiscordChatExporter.Cli.Commands
{
@@ -17,10 +17,14 @@ namespace DiscordChatExporter.Cli.Commands
public override async ValueTask ExecuteAsync(IConsole console)
{
var channels = await GetDiscordClient().GetGuildChannelsAsync(GuildId);
var channels = await Discord.GetGuildChannelsAsync(GuildId);
foreach (var channel in channels.OrderBy(c => c.Category, PositionBasedComparer.Instance).ThenBy(c => c.Name))
console.Output.WriteLine($"{channel.Id} | {channel.Category} / {channel.Name}");
foreach (var channel in channels.OrderBy(c => c.Category.Position).ThenBy(c => c.Name))
{
await console.Output.WriteLineAsync(
$"{channel.Id} | {channel.Category} / {channel.Name}"
);
}
}
}
}

View File

@@ -3,9 +3,8 @@ using System.Threading.Tasks;
using CliFx;
using CliFx.Attributes;
using DiscordChatExporter.Cli.Commands.Base;
using DiscordChatExporter.Domain.Discord.Models;
using DiscordChatExporter.Domain.Discord.Models.Common;
using DiscordChatExporter.Domain.Utilities;
using DiscordChatExporter.Core.Discord.Data;
using DiscordChatExporter.Core.Utils.Extensions;
namespace DiscordChatExporter.Cli.Commands
{
@@ -14,10 +13,14 @@ namespace DiscordChatExporter.Cli.Commands
{
public override async ValueTask ExecuteAsync(IConsole console)
{
var channels = await GetDiscordClient().GetGuildChannelsAsync(Guild.DirectMessages.Id);
var channels = await Discord.GetGuildChannelsAsync(Guild.DirectMessages.Id);
foreach (var channel in channels.OrderBy(c => c.Name))
console.Output.WriteLine($"{channel.Id} | {channel.Category} / {channel.Name}");
{
await console.Output.WriteLineAsync(
$"{channel.Id} | {channel.Category} / {channel.Name}"
);
}
}
}
}

View File

@@ -3,7 +3,7 @@ using System.Threading.Tasks;
using CliFx;
using CliFx.Attributes;
using DiscordChatExporter.Cli.Commands.Base;
using DiscordChatExporter.Domain.Utilities;
using DiscordChatExporter.Core.Utils.Extensions;
namespace DiscordChatExporter.Cli.Commands
{
@@ -12,10 +12,14 @@ namespace DiscordChatExporter.Cli.Commands
{
public override async ValueTask ExecuteAsync(IConsole console)
{
var guilds = await GetDiscordClient().GetUserGuildsAsync();
var guilds = await Discord.GetUserGuildsAsync();
foreach (var guild in guilds.OrderBy(g => g.Name))
console.Output.WriteLine($"{guild.Id} | {guild.Name}");
{
await console.Output.WriteLineAsync(
$"{guild.Id} | {guild.Name}"
);
}
}
}
}