Skip to content

Commit

Permalink
Fix crash when parsing position on DM channels (#496)
Browse files Browse the repository at this point in the history
  • Loading branch information
96-LB authored Feb 6, 2021
1 parent e49cf99 commit a8031ad
Show file tree
Hide file tree
Showing 7 changed files with 17 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public override async ValueTask ExecuteAsync(IConsole console)
{
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}");
}
}
Expand Down
10 changes: 5 additions & 5 deletions DiscordChatExporter.Domain/Discord/Models/Channel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,11 @@ public partial class Channel : IHasIdAndPosition

public string Name { get; }

public int Position { get; }
public int? Position { 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;
Type = type;
Expand Down Expand Up @@ -89,15 +89,15 @@ public static Channel Parse(JsonElement json, ChannelCategory? category = null,
json.GetPropertyOrNull("recipients")?.EnumerateArray().Select(User.Parse).Select(u => u.Name).JoinToString(", ") ??
id.ToString();

position ??= json.GetProperty("position").GetInt32();
position ??= json.GetPropertyOrNull("position")?.GetInt32();

return new Channel(
id,
type,
guildId ?? Guild.DirectMessages.Id,
category ?? GetDefaultCategory(type),
name,
position.Value,
position,
topic
);
}
Expand Down
8 changes: 4 additions & 4 deletions DiscordChatExporter.Domain/Discord/Models/ChannelCategory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ public partial class ChannelCategory : IHasIdAndPosition

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;
Name = name;
Expand All @@ -32,7 +32,7 @@ public partial class ChannelCategory
public static ChannelCategory Parse(JsonElement json, int? position = null)
{
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() ??
json.GetPropertyOrNull("recipients")?.EnumerateArray().Select(User.Parse).Select(u => u.Name).JoinToString(", ") ??
Expand All @@ -41,7 +41,7 @@ public static ChannelCategory Parse(JsonElement json, int? position = null)
return new ChannelCategory(
id,
name,
position.Value
position
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@
{
public interface IHasIdAndPosition : IHasId
{
int Position { get; }
int? Position { get; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,10 @@ public partial class PositionBasedComparer : IComparer<IHasIdAndPosition>
{
public int Compare(IHasIdAndPosition? x, IHasIdAndPosition? y)
{
int result;
if (x != null)
int result = Comparer<int?>.Default.Compare(x?.Position, y?.Position);
if (result == 0)
{
result = x.Position.CompareTo(y?.Position);
if(result == 0)
{
result = x.Id.Value.CompareTo(y?.Id.Value);
}
}
else
{
result = y == null ? 0 : -1;
result = Comparer<ulong?>.Default.Compare(x?.Id.Value, y?.Id.Value);
}
return result;
}
Expand Down
4 changes: 2 additions & 2 deletions DiscordChatExporter.Domain/Exporting/ExportRequest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,8 @@ private static string GetOutputBaseFilePath(
"%T" => channel.Category.Name,
"%c" => channel.Id.ToString(),
"%C" => channel.Name,
"%p" => channel.Position.ToString(),
"%P" => channel.Category.Position.ToString(),
"%p" => channel.Position?.ToString() ?? "0",
"%P" => channel.Category.Position?.ToString() ?? "0",
"%a" => (after ?? Snowflake.Zero).ToDate().ToString("yyyy-MM-dd"),
"%b" => (before?.ToDate() ?? DateTime.Now).ToString("yyyy-MM-dd"),
"%%" => "%",
Expand Down
2 changes: 1 addition & 1 deletion DiscordChatExporter.Gui/Views/RootView.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
<Window.Resources>
<CollectionViewSource x:Key="AvailableChannelsViewSource" Source="{Binding AvailableChannels, Mode=OneWay}">
<CollectionViewSource.GroupDescriptions>
<PropertyGroupDescription PropertyName="Category" />
<PropertyGroupDescription PropertyName="Category.Name" />
</CollectionViewSource.GroupDescriptions>
<CollectionViewSource.SortDescriptions>
<componentModel:SortDescription Direction="Ascending" PropertyName="Position" />
Expand Down

0 comments on commit a8031ad

Please sign in to comment.