fix: category children (#800)

This commit is contained in:
Skillz4Killz
2021-04-10 05:01:50 -04:00
committed by GitHub
parent 81c85eb337
commit 91114a62c2
5 changed files with 63 additions and 8 deletions
@@ -1,9 +1,9 @@
import { cacheHandlers } from "../../cache.ts"; import { cacheHandlers } from "../../cache.ts";
/** Gets an array of all the channels ids that are the children of this category. */ /** Gets an array of all the channels ids that are the children of this category. */
export function categoryChildrenIds(guildId: string, id: string) { export function categoryChildren(id: string) {
return cacheHandlers.filter( return cacheHandlers.filter(
"channels", "channels",
(channel) => channel.parentId === id && channel.guildId === guildId, (channel) => channel.parentId === id
); );
} }
+2 -3
View File
@@ -3,14 +3,13 @@ import { cacheHandlers } from "../../cache.ts";
import { rest } from "../../rest/rest.ts"; import { rest } from "../../rest/rest.ts";
import { structures } from "../../structures/mod.ts"; import { structures } from "../../structures/mod.ts";
import { DiscordChannel } from "../../types/channels/channel.ts"; import { DiscordChannel } from "../../types/channels/channel.ts";
import { DiscordChannelTypes } from "../../types/channels/channel_types.ts";
import { CreateGuildChannel } from "../../types/guilds/create_guild_channel.ts"; import { CreateGuildChannel } from "../../types/guilds/create_guild_channel.ts";
import { PermissionStrings } from "../../types/permissions/permission_strings.ts"; import { PermissionStrings } from "../../types/permissions/permission_strings.ts";
import { endpoints } from "../../util/constants.ts"; import { endpoints } from "../../util/constants.ts";
import { import {
calculateBits,
requireBotGuildPermissions, requireBotGuildPermissions,
} from "../../util/permissions.ts"; } from "../../util/permissions.ts";
import { camelKeysToSnakeCase } from "../../util/utils.ts";
/** Create a channel in your server. Bot needs MANAGE_CHANNEL permissions in the server. */ /** Create a channel in your server. Bot needs MANAGE_CHANNEL permissions in the server. */
export async function createChannel( export async function createChannel(
@@ -34,7 +33,7 @@ export async function createChannel(
"post", "post",
endpoints.GUILD_CHANNELS(guildId), endpoints.GUILD_CHANNELS(guildId),
{ {
...options, ...camelKeysToSnakeCase(options),
permission_overwrites: options?.permissionOverwrites?.map((perm) => ({ permission_overwrites: options?.permissionOverwrites?.map((perm) => ({
...perm, ...perm,
+3 -3
View File
@@ -1,4 +1,4 @@
import { categoryChildrenIds } from "./channels/category_children_ids.ts"; import { categoryChildren } from "./channels/category_children.ts";
import { channelOverwriteHasPermission } from "./channels/channel_overwrite_has_permission.ts"; import { channelOverwriteHasPermission } from "./channels/channel_overwrite_has_permission.ts";
import { createChannel } from "./channels/create_channel.ts"; import { createChannel } from "./channels/create_channel.ts";
import { deleteChannel } from "./channels/delete_channel.ts"; import { deleteChannel } from "./channels/delete_channel.ts";
@@ -123,7 +123,7 @@ export {
avatarURL, avatarURL,
ban, ban,
banMember, banMember,
categoryChildrenIds, categoryChildren,
channelOverwriteHasPermission, channelOverwriteHasPermission,
createChannel, createChannel,
createEmoji, createEmoji,
@@ -274,7 +274,7 @@ export let helpers = {
getEmoji, getEmoji,
getEmojis, getEmojis,
// guilds // guilds
categoryChildrenIds, categoryChildren,
createGuild, createGuild,
deleteServer, deleteServer,
editGuild, editGuild,
+55
View File
@@ -0,0 +1,55 @@
import { defaultTestOptions, tempData } from "../ws/start_bot.ts";
import { assertExists } from "../deps.ts";
import { cache } from "../../src/cache.ts";
import { createChannel, categoryChildren } from "../../src/helpers/mod.ts";
import { DiscordChannelTypes } from "../../src/types/channels/channel_types.ts";
import { delay } from "../../src/util/utils.ts";
Deno.test({
name: "[channel] category channel ids",
async fn() {
const category = await createChannel(tempData.guildId, {
name: "Discordeno-test",
type: DiscordChannelTypes.GUILD_CATEGORY,
});
// Assertions
assertExists(category);
// Delay the execution by 5 seconds to allow CHANNEL_CREATE event to be processed
await delay(5000);
if (!cache.channels.has(category.id)) {
throw new Error(
"The channel seemed to be created but it was not cached."
);
}
const channelsToCreate = [1, 2, 3, 4, 5];
const channels = await Promise.all(
channelsToCreate.map((num) =>
createChannel(tempData.guildId, {
name: `Discordeno-test-${num}`,
parentId: category.id,
})
)
);
// Delay the execution by 5 seconds to allow CHANNEL_CREATE event to be processed
await delay(5000);
// If every channel is not present in the cache, error out
if (!channels.every((c) => cache.channels.has(c.id)))
throw new Error(
"The channels seemed to be created but it was not cached."
);
const ids = await categoryChildren(category.id);
if (
ids.size !== channelsToCreate.length ||
!channels.every((c) => ids.has(c.id))
)
throw new Error(
"The category channel ids did not match with the category channels."
);
},
...defaultTestOptions,
});
+1
View File
@@ -13,6 +13,7 @@ import "./guilds/create_guild.ts";
// Channel tests // Channel tests
import "./channels/create_channel.ts"; import "./channels/create_channel.ts";
import "./channels/category_children.ts";
import "./channels/delete_channel.ts"; import "./channels/delete_channel.ts";
// Messages tests // Messages tests