From 7c8b85cde9e17544cc38b5693e180041801b6479 Mon Sep 17 00:00:00 2001 From: Skillz Date: Wed, 18 Nov 2020 00:11:22 -0500 Subject: [PATCH] remove guild.channels --- src/controllers/cache.ts | 25 +++++++++++++++++++++++++ src/handlers/channel.ts | 6 +++--- src/handlers/guild.ts | 5 ++--- src/structures/channel.ts | 11 +---------- src/structures/guild.ts | 6 ++---- 5 files changed, 33 insertions(+), 20 deletions(-) diff --git a/src/controllers/cache.ts b/src/controllers/cache.ts index a0588c6ac..b7208334a 100644 --- a/src/controllers/cache.ts +++ b/src/controllers/cache.ts @@ -79,6 +79,29 @@ function forEach( return cache[table].forEach(callback); } +function filter( + table: "guilds", + callback: (value: Guild, key: string) => boolean, +): Collection; +function filter( + table: "unavailableGuilds", + callback: (value: Guild, key: string) => boolean, +): Collection; +function filter( + table: "channels", + callback: (value: Channel, key: string) => boolean, +): Collection; +function filter( + table: "messages", + callback: (value: Message, key: string) => boolean, +): Collection; +function filter( + table: TableName, + callback: (value: any, key: string) => boolean, +) { + return cache[table].filter(callback); +} + export let cacheHandlers = { /** Deletes all items from the cache */ clear: async function (table: TableName) { @@ -105,4 +128,6 @@ export let cacheHandlers = { get, /** Run a function on all items in this cache */ forEach, + /** Allows you to filter our all items in this cache. */ + filter, }; diff --git a/src/handlers/channel.ts b/src/handlers/channel.ts index fedba0bd1..fe77283fe 100644 --- a/src/handlers/channel.ts +++ b/src/handlers/channel.ts @@ -17,7 +17,7 @@ import { PermissionOverwrite } from "../types/guild.ts"; import { MessageCreateOptions } from "../types/message.ts"; import { Permissions } from "../types/permission.ts"; import { endpoints } from "../utils/constants.ts"; -import { botHasChannelPermissions } from "../utils/permissions.ts"; +import { botHasChannelPermissions, calculateBits } from "../utils/permissions.ts"; /** Checks if a channel overwrite for a user id or a role id has permission in this channel */ export function channelOverwriteHasPermission( @@ -455,8 +455,8 @@ export async function isChannelSynced(channelID: string) { const parentChannel = await cacheHandlers.get("channels", channel.parentID); if (!parentChannel) return false; - return channel.permission_overwrites?.every((overwrite) => { - const permission = parentChannel.permission_overwrites?.find((ow) => + return channel.permissionOverwrites?.every((overwrite) => { + const permission = parentChannel.permissionOverwrites?.find((ow) => ow.id === overwrite.id ); if (!permission) return false; diff --git a/src/handlers/guild.ts b/src/handlers/guild.ts index ebcdb7e6e..c446a18d8 100644 --- a/src/handlers/guild.ts +++ b/src/handlers/guild.ts @@ -54,8 +54,8 @@ export function deleteServer(guildID: string) { } /** Gets an array of all the channels ids that are the children of this category. */ -export function categoryChildrenIDs(guild: Guild, id: string) { - return guild.channels.filter((channel) => channel.parentID === id); +export function categoryChildrenIDs(guildID: string, id: string) { + return cacheHandlers.filter("channels", (channel) => channel.parentID === id && channel.guildID === guildID); } /** The full URL of the icon from Discords CDN. Undefined when no icon is set. */ @@ -132,7 +132,6 @@ export async function createGuildChannel( })) as ChannelCreatePayload; const channel = await structures.createChannel(result); - guild.channels.set(result.id, channel); return channel; } diff --git a/src/structures/channel.ts b/src/structures/channel.ts index b36662d2b..ce90156cc 100644 --- a/src/structures/channel.ts +++ b/src/structures/channel.ts @@ -1,8 +1,6 @@ import { cacheHandlers } from "../controllers/cache.ts"; import { ChannelCreatePayload } from "../types/channel.ts"; -import { PermissionOverwrite } from "../types/guild.ts"; import { Unpromise } from "../types/misc.ts"; -import { calculatePermissions } from "../utils/permissions.ts"; export async function createChannel( data: ChannelCreatePayload, @@ -34,14 +32,7 @@ export async function createChannel( /** The last time when a message was pinned in this channel */ lastPinTimestamp, /** The permission overwrites for this channel */ - permissionOverwrites: - (data.permission_overwrites - ? data.permission_overwrites.map((perm) => ({ - ...perm, - allow: calculatePermissions(BigInt(perm.allow)), - deny: calculatePermissions(BigInt(perm.deny)), - })) - : []) as PermissionOverwrite[], + permissionOverwrites: permission_overwrites, /** Whether this channel is nsfw or not */ nsfw: data.nsfw || false, /** The mention of the channel */ diff --git a/src/structures/guild.ts b/src/structures/guild.ts index 3cde3638f..51b76877f 100644 --- a/src/structures/guild.ts +++ b/src/structures/guild.ts @@ -29,7 +29,8 @@ export async function createGuild(data: CreateGuildPayload, shardID: number) { const roles = await Promise.all( data.roles.map((r) => structures.createRole(r)), ); - const channels = await Promise.all( + + await Promise.all( data.channels.map((c) => structures.createChannel(c, data.id)), ); @@ -65,15 +66,12 @@ export async function createGuild(data: CreateGuildPayload, shardID: number) { premiumSubscriptionCount, /** The preferred language in this server. */ preferredLocale, - /** The roles in the guild */ roles: new Collection(roles.map((r) => [r.id, r])), /** When this guild was joined at. */ joinedAt: Date.parse(joinedAt), /** The users in this guild. */ members: new Collection(), - /** The channels in the guild */ - channels: new Collection(channels.map((c) => [c.id, c])), /** The presences of all the users in the guild. */ presences: new Collection(data.presences.map((p) => [p.user.id, p])), /** The total number of members in this guild. This value is updated as members leave and join the server. However, if you do not have the intent enabled to be able to listen to these events, then this will not be accurate. */