mirror of
https://github.com/Tyrrrz/DiscordChatExporter.git
synced 2026-02-16 22:48:26 +00:00
[CLI] Fix sorting by channel/category position (#490)
This commit is contained in:
@@ -4,6 +4,7 @@ using CliFx;
|
||||
using CliFx.Attributes;
|
||||
using DiscordChatExporter.Cli.Commands.Base;
|
||||
using DiscordChatExporter.Domain.Discord;
|
||||
using DiscordChatExporter.Domain.Discord.Models.Common;
|
||||
using DiscordChatExporter.Domain.Utilities;
|
||||
|
||||
namespace DiscordChatExporter.Cli.Commands
|
||||
@@ -18,7 +19,7 @@ namespace DiscordChatExporter.Cli.Commands
|
||||
{
|
||||
var channels = await GetDiscordClient().GetGuildChannelsAsync(GuildId);
|
||||
|
||||
foreach (var channel in channels.OrderBy(c => c.Category).ThenBy(c => c.Name))
|
||||
foreach (var channel in channels.OrderBy(c => c.Category, PositionBasedComparer.Instance).ThenBy(c => c.Name))
|
||||
console.Output.WriteLine($"{channel.Id} | {channel.Category} / {channel.Name}");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -4,6 +4,7 @@ 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;
|
||||
|
||||
namespace DiscordChatExporter.Cli.Commands
|
||||
@@ -15,7 +16,7 @@ namespace DiscordChatExporter.Cli.Commands
|
||||
{
|
||||
var channels = await GetDiscordClient().GetGuildChannelsAsync(Guild.DirectMessages.Id);
|
||||
|
||||
foreach (var channel in channels.OrderBy(c => c.Category).ThenBy(c => c.Name))
|
||||
foreach (var channel in channels.OrderBy(c => c.Category, PositionBasedComparer.Instance).ThenBy(c => c.Name))
|
||||
console.Output.WriteLine($"{channel.Id} | {channel.Category} / {channel.Name}");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -22,7 +22,7 @@ namespace DiscordChatExporter.Domain.Discord.Models
|
||||
}
|
||||
|
||||
// https://discord.com/developers/docs/resources/channel#channel-object
|
||||
public partial class Channel : IHasId
|
||||
public partial class Channel : IHasIdAndPosition
|
||||
{
|
||||
public Snowflake Id { get; }
|
||||
|
||||
|
||||
@@ -8,7 +8,7 @@ using Tyrrrz.Extensions;
|
||||
|
||||
namespace DiscordChatExporter.Domain.Discord.Models
|
||||
{
|
||||
public partial class ChannelCategory : IHasId
|
||||
public partial class ChannelCategory : IHasIdAndPosition
|
||||
{
|
||||
public Snowflake Id { get; }
|
||||
|
||||
|
||||
@@ -2,29 +2,29 @@
|
||||
|
||||
namespace DiscordChatExporter.Domain.Discord.Models.Common
|
||||
{
|
||||
public partial class ChannelPositionBasedComparer : IComparer<Channel>
|
||||
public partial class PositionBasedComparer : IComparer<IHasIdAndPosition>
|
||||
{
|
||||
public int Compare(Channel? x, Channel? y)
|
||||
public int Compare(IHasIdAndPosition? x, IHasIdAndPosition? y)
|
||||
{
|
||||
int result;
|
||||
if (x != null)
|
||||
{
|
||||
result = x.Position.CompareTo(y?.Position);
|
||||
}
|
||||
else if (y != null)
|
||||
{
|
||||
result = -y.Position.CompareTo(x?.Position);
|
||||
if(result == 0)
|
||||
{
|
||||
result = x.Id.Value.CompareTo(y?.Id.Value);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
result = 0;
|
||||
result = y == null ? 0 : -1;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
public partial class ChannelPositionBasedComparer
|
||||
public partial class PositionBasedComparer
|
||||
{
|
||||
public static ChannelPositionBasedComparer Instance { get; } = new();
|
||||
public static PositionBasedComparer Instance { get; } = new();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
namespace DiscordChatExporter.Domain.Discord.Models.Common
|
||||
{
|
||||
public interface IHasIdAndPosition : IHasId
|
||||
{
|
||||
int Position { get; }
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user