remove guild.channels

This commit is contained in:
Skillz
2020-11-18 00:11:22 -05:00
parent 3c97b29cbe
commit 7c8b85cde9
5 changed files with 33 additions and 20 deletions
+25
View File
@@ -79,6 +79,29 @@ function forEach(
return cache[table].forEach(callback);
}
function filter(
table: "guilds",
callback: (value: Guild, key: string) => boolean,
): Collection<string, Guild>;
function filter(
table: "unavailableGuilds",
callback: (value: Guild, key: string) => boolean,
): Collection<string, Guild>;
function filter(
table: "channels",
callback: (value: Channel, key: string) => boolean,
): Collection<string, Channel>;
function filter(
table: "messages",
callback: (value: Message, key: string) => boolean,
): Collection<string, Message>;
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,
};
+3 -3
View File
@@ -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;
+2 -3
View File
@@ -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;
}
+1 -10
View File
@@ -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 */
+2 -4
View File
@@ -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<string, Member>(),
/** 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. */