mirror of
https://github.com/Tyrrrz/DiscordChatExporter.git
synced 2025-05-31 23:08:23 -04:00
Improve naming of dictionaries
This commit is contained in:
parent
0ca42f5a14
commit
817d54039b
3 changed files with 21 additions and 21 deletions
|
@ -177,7 +177,7 @@ public abstract class ExportCommandBase : DiscordCommandBase
|
||||||
|
|
||||||
// Export
|
// Export
|
||||||
var cancellationToken = console.RegisterCancellationHandler();
|
var cancellationToken = console.RegisterCancellationHandler();
|
||||||
var errors = new ConcurrentDictionary<Channel, string>();
|
var errorsByChannel = new ConcurrentDictionary<Channel, string>();
|
||||||
|
|
||||||
await console.Output.WriteLineAsync($"Exporting {channels.Count} channel(s)...");
|
await console.Output.WriteLineAsync($"Exporting {channels.Count} channel(s)...");
|
||||||
await console.CreateProgressTicker().StartAsync(async progressContext =>
|
await console.CreateProgressTicker().StartAsync(async progressContext =>
|
||||||
|
@ -225,7 +225,7 @@ public abstract class ExportCommandBase : DiscordCommandBase
|
||||||
}
|
}
|
||||||
catch (DiscordChatExporterException ex) when (!ex.IsFatal)
|
catch (DiscordChatExporterException ex) when (!ex.IsFatal)
|
||||||
{
|
{
|
||||||
errors[channel] = ex.Message;
|
errorsByChannel[channel] = ex.Message;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
|
@ -235,23 +235,23 @@ public abstract class ExportCommandBase : DiscordCommandBase
|
||||||
using (console.WithForegroundColor(ConsoleColor.White))
|
using (console.WithForegroundColor(ConsoleColor.White))
|
||||||
{
|
{
|
||||||
await console.Output.WriteLineAsync(
|
await console.Output.WriteLineAsync(
|
||||||
$"Successfully exported {channels.Count - errors.Count} channel(s)."
|
$"Successfully exported {channels.Count - errorsByChannel.Count} channel(s)."
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Print errors
|
// Print errors
|
||||||
if (errors.Any())
|
if (errorsByChannel.Any())
|
||||||
{
|
{
|
||||||
await console.Output.WriteLineAsync();
|
await console.Output.WriteLineAsync();
|
||||||
|
|
||||||
using (console.WithForegroundColor(ConsoleColor.Red))
|
using (console.WithForegroundColor(ConsoleColor.Red))
|
||||||
{
|
{
|
||||||
await console.Error.WriteLineAsync(
|
await console.Error.WriteLineAsync(
|
||||||
$"Failed to export {errors.Count} channel(s):"
|
$"Failed to export {errorsByChannel.Count} channel(s):"
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
foreach (var (channel, error) in errors)
|
foreach (var (channel, error) in errorsByChannel)
|
||||||
{
|
{
|
||||||
await console.Error.WriteAsync($"{channel.Category} / {channel.Name}: ");
|
await console.Error.WriteAsync($"{channel.Category} / {channel.Name}: ");
|
||||||
|
|
||||||
|
@ -264,7 +264,7 @@ public abstract class ExportCommandBase : DiscordCommandBase
|
||||||
|
|
||||||
// Fail the command only if ALL channels failed to export.
|
// Fail the command only if ALL channels failed to export.
|
||||||
// If only some channels failed to export, it's okay.
|
// If only some channels failed to export, it's okay.
|
||||||
if (errors.Count >= channels.Count)
|
if (errorsByChannel.Count >= channels.Count)
|
||||||
throw new CommandException("Export failed.");
|
throw new CommandException("Export failed.");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -25,7 +25,7 @@ internal partial class ExportAssetDownloader
|
||||||
private readonly bool _reuse;
|
private readonly bool _reuse;
|
||||||
|
|
||||||
// File paths of the previously downloaded assets
|
// File paths of the previously downloaded assets
|
||||||
private readonly Dictionary<string, string> _pathCache = new(StringComparer.Ordinal);
|
private readonly Dictionary<string, string> _previousPathsByUrl = new(StringComparer.Ordinal);
|
||||||
|
|
||||||
public ExportAssetDownloader(string workingDirPath, bool reuse)
|
public ExportAssetDownloader(string workingDirPath, bool reuse)
|
||||||
{
|
{
|
||||||
|
@ -40,12 +40,12 @@ internal partial class ExportAssetDownloader
|
||||||
|
|
||||||
using var _ = await Locker.LockAsync(filePath, cancellationToken);
|
using var _ = await Locker.LockAsync(filePath, cancellationToken);
|
||||||
|
|
||||||
if (_pathCache.TryGetValue(url, out var cachedFilePath))
|
if (_previousPathsByUrl.TryGetValue(url, out var cachedFilePath))
|
||||||
return cachedFilePath;
|
return cachedFilePath;
|
||||||
|
|
||||||
// Reuse existing files if we're allowed to
|
// Reuse existing files if we're allowed to
|
||||||
if (_reuse && File.Exists(filePath))
|
if (_reuse && File.Exists(filePath))
|
||||||
return _pathCache[url] = filePath;
|
return _previousPathsByUrl[url] = filePath;
|
||||||
|
|
||||||
Directory.CreateDirectory(_workingDirPath);
|
Directory.CreateDirectory(_workingDirPath);
|
||||||
|
|
||||||
|
@ -80,7 +80,7 @@ internal partial class ExportAssetDownloader
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
return _pathCache[url] = filePath;
|
return _previousPathsByUrl[url] = filePath;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -14,9 +14,9 @@ namespace DiscordChatExporter.Core.Exporting;
|
||||||
|
|
||||||
internal class ExportContext
|
internal class ExportContext
|
||||||
{
|
{
|
||||||
private readonly Dictionary<Snowflake, Member?> _members = new();
|
private readonly Dictionary<Snowflake, Member?> _membersById = new();
|
||||||
private readonly Dictionary<Snowflake, Channel> _channels = new();
|
private readonly Dictionary<Snowflake, Channel> _channelsById = new();
|
||||||
private readonly Dictionary<Snowflake, Role> _roles = new();
|
private readonly Dictionary<Snowflake, Role> _rolesById = new();
|
||||||
private readonly ExportAssetDownloader _assetDownloader;
|
private readonly ExportAssetDownloader _assetDownloader;
|
||||||
|
|
||||||
public DiscordClient Discord { get; }
|
public DiscordClient Discord { get; }
|
||||||
|
@ -38,10 +38,10 @@ internal class ExportContext
|
||||||
public async ValueTask PopulateChannelsAndRolesAsync(CancellationToken cancellationToken = default)
|
public async ValueTask PopulateChannelsAndRolesAsync(CancellationToken cancellationToken = default)
|
||||||
{
|
{
|
||||||
await foreach (var channel in Discord.GetGuildChannelsAsync(Request.Guild.Id, cancellationToken))
|
await foreach (var channel in Discord.GetGuildChannelsAsync(Request.Guild.Id, cancellationToken))
|
||||||
_channels[channel.Id] = channel;
|
_channelsById[channel.Id] = channel;
|
||||||
|
|
||||||
await foreach (var role in Discord.GetGuildRolesAsync(Request.Guild.Id, cancellationToken))
|
await foreach (var role in Discord.GetGuildRolesAsync(Request.Guild.Id, cancellationToken))
|
||||||
_roles[role.Id] = role;
|
_rolesById[role.Id] = role;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Because members cannot be pulled in bulk, we need to populate them on demand
|
// Because members cannot be pulled in bulk, we need to populate them on demand
|
||||||
|
@ -50,7 +50,7 @@ internal class ExportContext
|
||||||
User? fallbackUser,
|
User? fallbackUser,
|
||||||
CancellationToken cancellationToken = default)
|
CancellationToken cancellationToken = default)
|
||||||
{
|
{
|
||||||
if (_members.ContainsKey(id))
|
if (_membersById.ContainsKey(id))
|
||||||
return;
|
return;
|
||||||
|
|
||||||
var member = await Discord.TryGetGuildMemberAsync(Request.Guild.Id, id, cancellationToken);
|
var member = await Discord.TryGetGuildMemberAsync(Request.Guild.Id, id, cancellationToken);
|
||||||
|
@ -67,7 +67,7 @@ internal class ExportContext
|
||||||
}
|
}
|
||||||
|
|
||||||
// Store the result even if it's null, to avoid re-fetching non-existing members
|
// Store the result even if it's null, to avoid re-fetching non-existing members
|
||||||
_members[id] = member;
|
_membersById[id] = member;
|
||||||
}
|
}
|
||||||
|
|
||||||
public async ValueTask PopulateMemberAsync(Snowflake id, CancellationToken cancellationToken = default) =>
|
public async ValueTask PopulateMemberAsync(Snowflake id, CancellationToken cancellationToken = default) =>
|
||||||
|
@ -83,11 +83,11 @@ internal class ExportContext
|
||||||
var format => instant.ToLocalString(format)
|
var format => instant.ToLocalString(format)
|
||||||
};
|
};
|
||||||
|
|
||||||
public Member? TryGetMember(Snowflake id) => _members.GetValueOrDefault(id);
|
public Member? TryGetMember(Snowflake id) => _membersById.GetValueOrDefault(id);
|
||||||
|
|
||||||
public Channel? TryGetChannel(Snowflake id) => _channels.GetValueOrDefault(id);
|
public Channel? TryGetChannel(Snowflake id) => _channelsById.GetValueOrDefault(id);
|
||||||
|
|
||||||
public Role? TryGetRole(Snowflake id) => _roles.GetValueOrDefault(id);
|
public Role? TryGetRole(Snowflake id) => _rolesById.GetValueOrDefault(id);
|
||||||
|
|
||||||
public IReadOnlyList<Role> GetUserRoles(Snowflake id) => TryGetMember(id)?
|
public IReadOnlyList<Role> GetUserRoles(Snowflake id) => TryGetMember(id)?
|
||||||
.RoleIds
|
.RoleIds
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue