Skip to content

Commit 0d40eaa

Browse files
committed
Migrate clientStyleChannelSort to dashboard
1 parent 7a7c98f commit 0d40eaa

File tree

2 files changed

+4
-65
lines changed

2 files changed

+4
-65
lines changed

src/lib/util/util.ts

Lines changed: 2 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { container } from "@sapphire/framework";
2-
import { inlineCodeBlock, isNullishOrEmpty } from "@sapphire/utilities";
3-
import { ChannelType, PermissionFlagsBits, StageChannel, VoiceChannel, type AuditLogEvent, type Collection, type Guild, type GuildBasedChannel, type GuildChannel, type Message } from "discord.js";
2+
import { inlineCodeBlock } from "@sapphire/utilities";
3+
import { PermissionFlagsBits, StageChannel, VoiceChannel, type AuditLogEvent, type Guild, type Message } from "discord.js";
44

55
/**
66
* Get the executor user from the last audit log entry of specific type
@@ -117,57 +117,3 @@ export function getPermissionString(permission: string) {
117117
default: return undefined;
118118
}
119119
}
120-
121-
// These are just here for the sorting function below
122-
const textChannelTypes = [ChannelType.GuildText, ChannelType.GuildAnnouncement, ChannelType.GuildForum];
123-
// const voiceChannelTypes = [ChannelType.GuildVoice, ChannelType.GuildStageVoice];
124-
125-
/**
126-
* Sorts a collection of GuildBasedChannels into the order they'd be displayed in the Discord client
127-
* @param inputChannels The collection of channels you want to be sorted client-style
128-
*/
129-
export function clientStyleChannelSort(inputChannels: Collection<string, GuildBasedChannel>) {
130-
// First, make sure we don't have any threads--they don't have position properties
131-
const allChannels = inputChannels.filter((channel) => channel.type !== ChannelType.PrivateThread && channel.type !== ChannelType.PublicThread);
132-
// Now that we know we have the right channels, just ignore me constantly reassigning types to them...
133-
const levelZero = allChannels.clone().filter((channel) => !channel.parentId);
134-
const levelOne: { parentID: string, children: Collection<string, GuildBasedChannel> }[] = [];
135-
136-
levelZero.forEach((l0Channel) => {
137-
// Now we'll build collections of nested channels and group them by their parent
138-
const l1Channels = allChannels.clone().filter((l1Channel) => l1Channel.parentId === l0Channel.id);
139-
// Now we'll run our three-way sort on these channels
140-
// Special Channels (eg: Forums/Stages) can only exist in categories
141-
// They can also freely mix with their respective types (Forums w/ Text, Stages w/ Voice)
142-
// So we do a slightly different sort for child channels than for parent/non-nested ones
143-
l1Channels.sort((a, b) => {
144-
const chA = a as any as GuildChannel;
145-
const chB = b as any as GuildChannel;
146-
const chATypeScore = textChannelTypes.includes(chA.type) ? 1 : 0;
147-
const chBTypeScore = textChannelTypes.includes(chA.type) ? 1 : 0;
148-
return chATypeScore - chBTypeScore || chA.rawPosition - chB.rawPosition || chA.createdTimestamp - chB.createdTimestamp;
149-
});
150-
levelOne.push({ parentID: l0Channel.id, children: l1Channels });
151-
});
152-
153-
// And we'll run our more strict three-way sort on the parent/non-nested channels
154-
levelZero.sort((a, b) => {
155-
const chA = a as any as GuildChannel;
156-
const chB = b as any as GuildChannel;
157-
return chA.type - chB.type || chA.rawPosition - chB.rawPosition || chA.createdTimestamp - chB.createdTimestamp
158-
});
159-
160-
const sortedChannels: GuildChannel[] = [];
161-
162-
// Now that everything's sorted, we're just pushing every channel into an array in order
163-
levelZero.forEach((channel) => {
164-
sortedChannels.push(channel as any as GuildChannel);
165-
const childrenContainer = levelOne.find((obj) => obj.parentID === channel.id);
166-
if (isNullishOrEmpty(childrenContainer) || !childrenContainer?.children.size) return;
167-
childrenContainer.children.forEach((childChannel) => {
168-
sortedChannels.push(childChannel as any as GuildChannel);
169-
});
170-
});
171-
172-
return sortedChannels;
173-
}

src/routes/guilds/channels/channels.ts

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
import { authenticated } from '#root/lib/util/decorators/routeAuthenticated';
2-
import { clientStyleChannelSort } from '#utils/util';
32
import { ApplyOptions } from '@sapphire/decorators';
43
import { HttpCodes, Route, methods, type ApiRequest, type ApiResponse } from '@sapphire/plugin-api';
54
import { isNullishOrEmpty } from '@sapphire/utilities';
6-
import { PermissionsBitField, type GuildChannel } from 'discord.js';
5+
import { PermissionsBitField } from 'discord.js';
76

87
@ApplyOptions<Route.Options>({
98
name: 'guildChannels',
@@ -49,14 +48,8 @@ export class UserRoute extends Route {
4948
// If no channels are left after filtering, Error 404
5049
if (!channels.size) return response.error(HttpCodes.NotFound);
5150

52-
// If request asked for channels to be sorted (Discord Client Style) get a sorted array
53-
let sortedChannels: GuildChannel[] | null = null;
54-
if (!isNullishOrEmpty(queryParams.sorted)) {
55-
sortedChannels = clientStyleChannelSort(channels);
56-
}
57-
5851
// Return collection of channels
59-
return response.json({ data: { channels: sortedChannels ?? channels } });
52+
return response.json({ data: { channels } });
6053
}
6154

6255
}

0 commit comments

Comments
 (0)