mirror of
https://github.com/Tyrrrz/DiscordChatExporter.git
synced 2025-05-22 10:55:15 -04:00
Fix crash when parsing position on DM channels (#496)
This commit is contained in:
parent
e49cf997ea
commit
a8031ad3aa
7 changed files with 17 additions and 25 deletions
|
@ -16,7 +16,7 @@ namespace DiscordChatExporter.Cli.Commands
|
||||||
{
|
{
|
||||||
var channels = await GetDiscordClient().GetGuildChannelsAsync(Guild.DirectMessages.Id);
|
var channels = await GetDiscordClient().GetGuildChannelsAsync(Guild.DirectMessages.Id);
|
||||||
|
|
||||||
foreach (var channel in channels.OrderBy(c => c.Category, PositionBasedComparer.Instance).ThenBy(c => c.Name))
|
foreach (var channel in channels.OrderBy(c => c.Name))
|
||||||
console.Output.WriteLine($"{channel.Id} | {channel.Category} / {channel.Name}");
|
console.Output.WriteLine($"{channel.Id} | {channel.Category} / {channel.Name}");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -41,11 +41,11 @@ namespace DiscordChatExporter.Domain.Discord.Models
|
||||||
|
|
||||||
public string Name { get; }
|
public string Name { get; }
|
||||||
|
|
||||||
public int Position { get; }
|
public int? Position { get; }
|
||||||
|
|
||||||
public string? Topic { get; }
|
public string? Topic { get; }
|
||||||
|
|
||||||
public Channel(Snowflake id, ChannelType type, Snowflake guildId, ChannelCategory? category, string name, int position, string? topic)
|
public Channel(Snowflake id, ChannelType type, Snowflake guildId, ChannelCategory? category, string name, int? position, string? topic)
|
||||||
{
|
{
|
||||||
Id = id;
|
Id = id;
|
||||||
Type = type;
|
Type = type;
|
||||||
|
@ -89,15 +89,15 @@ namespace DiscordChatExporter.Domain.Discord.Models
|
||||||
json.GetPropertyOrNull("recipients")?.EnumerateArray().Select(User.Parse).Select(u => u.Name).JoinToString(", ") ??
|
json.GetPropertyOrNull("recipients")?.EnumerateArray().Select(User.Parse).Select(u => u.Name).JoinToString(", ") ??
|
||||||
id.ToString();
|
id.ToString();
|
||||||
|
|
||||||
position ??= json.GetProperty("position").GetInt32();
|
position ??= json.GetPropertyOrNull("position")?.GetInt32();
|
||||||
|
|
||||||
return new Channel(
|
return new Channel(
|
||||||
id,
|
id,
|
||||||
type,
|
type,
|
||||||
guildId ?? Guild.DirectMessages.Id,
|
guildId ?? Guild.DirectMessages.Id,
|
||||||
category ?? GetDefaultCategory(type),
|
category ?? GetDefaultCategory(type),
|
||||||
name,
|
name,
|
||||||
position.Value,
|
position,
|
||||||
topic
|
topic
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
|
@ -14,9 +14,9 @@ namespace DiscordChatExporter.Domain.Discord.Models
|
||||||
|
|
||||||
public string Name { get; }
|
public string Name { get; }
|
||||||
|
|
||||||
public int Position { get; }
|
public int? Position { get; }
|
||||||
|
|
||||||
public ChannelCategory(Snowflake id, string name, int position)
|
public ChannelCategory(Snowflake id, string name, int? position)
|
||||||
{
|
{
|
||||||
Id = id;
|
Id = id;
|
||||||
Name = name;
|
Name = name;
|
||||||
|
@ -32,7 +32,7 @@ namespace DiscordChatExporter.Domain.Discord.Models
|
||||||
public static ChannelCategory Parse(JsonElement json, int? position = null)
|
public static ChannelCategory Parse(JsonElement json, int? position = null)
|
||||||
{
|
{
|
||||||
var id = json.GetProperty("id").GetString().Pipe(Snowflake.Parse);
|
var id = json.GetProperty("id").GetString().Pipe(Snowflake.Parse);
|
||||||
position ??= json.GetProperty("position").GetInt32();
|
position ??= json.GetPropertyOrNull("position")?.GetInt32();
|
||||||
|
|
||||||
var name = json.GetPropertyOrNull("name")?.GetString() ??
|
var name = json.GetPropertyOrNull("name")?.GetString() ??
|
||||||
json.GetPropertyOrNull("recipients")?.EnumerateArray().Select(User.Parse).Select(u => u.Name).JoinToString(", ") ??
|
json.GetPropertyOrNull("recipients")?.EnumerateArray().Select(User.Parse).Select(u => u.Name).JoinToString(", ") ??
|
||||||
|
@ -41,7 +41,7 @@ namespace DiscordChatExporter.Domain.Discord.Models
|
||||||
return new ChannelCategory(
|
return new ChannelCategory(
|
||||||
id,
|
id,
|
||||||
name,
|
name,
|
||||||
position.Value
|
position
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -2,6 +2,6 @@
|
||||||
{
|
{
|
||||||
public interface IHasIdAndPosition : IHasId
|
public interface IHasIdAndPosition : IHasId
|
||||||
{
|
{
|
||||||
int Position { get; }
|
int? Position { get; }
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -6,18 +6,10 @@ namespace DiscordChatExporter.Domain.Discord.Models.Common
|
||||||
{
|
{
|
||||||
public int Compare(IHasIdAndPosition? x, IHasIdAndPosition? y)
|
public int Compare(IHasIdAndPosition? x, IHasIdAndPosition? y)
|
||||||
{
|
{
|
||||||
int result;
|
int result = Comparer<int?>.Default.Compare(x?.Position, y?.Position);
|
||||||
if (x != null)
|
if (result == 0)
|
||||||
{
|
{
|
||||||
result = x.Position.CompareTo(y?.Position);
|
result = Comparer<ulong?>.Default.Compare(x?.Id.Value, y?.Id.Value);
|
||||||
if(result == 0)
|
|
||||||
{
|
|
||||||
result = x.Id.Value.CompareTo(y?.Id.Value);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
result = y == null ? 0 : -1;
|
|
||||||
}
|
}
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
|
@ -94,8 +94,8 @@ namespace DiscordChatExporter.Domain.Exporting
|
||||||
"%T" => channel.Category.Name,
|
"%T" => channel.Category.Name,
|
||||||
"%c" => channel.Id.ToString(),
|
"%c" => channel.Id.ToString(),
|
||||||
"%C" => channel.Name,
|
"%C" => channel.Name,
|
||||||
"%p" => channel.Position.ToString(),
|
"%p" => channel.Position?.ToString() ?? "0",
|
||||||
"%P" => channel.Category.Position.ToString(),
|
"%P" => channel.Category.Position?.ToString() ?? "0",
|
||||||
"%a" => (after ?? Snowflake.Zero).ToDate().ToString("yyyy-MM-dd"),
|
"%a" => (after ?? Snowflake.Zero).ToDate().ToString("yyyy-MM-dd"),
|
||||||
"%b" => (before?.ToDate() ?? DateTime.Now).ToString("yyyy-MM-dd"),
|
"%b" => (before?.ToDate() ?? DateTime.Now).ToString("yyyy-MM-dd"),
|
||||||
"%%" => "%",
|
"%%" => "%",
|
||||||
|
|
|
@ -27,7 +27,7 @@
|
||||||
<Window.Resources>
|
<Window.Resources>
|
||||||
<CollectionViewSource x:Key="AvailableChannelsViewSource" Source="{Binding AvailableChannels, Mode=OneWay}">
|
<CollectionViewSource x:Key="AvailableChannelsViewSource" Source="{Binding AvailableChannels, Mode=OneWay}">
|
||||||
<CollectionViewSource.GroupDescriptions>
|
<CollectionViewSource.GroupDescriptions>
|
||||||
<PropertyGroupDescription PropertyName="Category" />
|
<PropertyGroupDescription PropertyName="Category.Name" />
|
||||||
</CollectionViewSource.GroupDescriptions>
|
</CollectionViewSource.GroupDescriptions>
|
||||||
<CollectionViewSource.SortDescriptions>
|
<CollectionViewSource.SortDescriptions>
|
||||||
<componentModel:SortDescription Direction="Ascending" PropertyName="Position" />
|
<componentModel:SortDescription Direction="Ascending" PropertyName="Position" />
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue