From dc9b0a78ec30d799c477baaef3659d2e21422b91 Mon Sep 17 00:00:00 2001 From: Skillz4Killz Date: Tue, 15 Dec 2020 14:53:28 -0500 Subject: [PATCH] channels with getters --- src/structures/channel.ts | 94 +++++++++++++++++++++++++++++---------- 1 file changed, 70 insertions(+), 24 deletions(-) diff --git a/src/structures/channel.ts b/src/structures/channel.ts index fa3029636..9fd1f7cfd 100644 --- a/src/structures/channel.ts +++ b/src/structures/channel.ts @@ -1,5 +1,20 @@ +import { Collection, createNewProp, Guild } from "../../mod.ts"; import { cacheHandlers } from "../controllers/cache.ts"; -import { ChannelCreatePayload, Unpromise } from "../types/types.ts"; +import { ChannelCreatePayload, RawOverwrite, Unpromise } from "../types/types.ts"; +import { cache } from "../utils/cache.ts"; +import { Message } from "./message.ts"; + +const baseChannel: any = { + get guild() { + return cache.guilds.get(this.guildID); + }, + get messages() { + return cache.messages.filter(m => m.channelID === this.id); + }, + get mention() { + return `<#${this.id}>`; + } +}; export async function createChannel( data: ChannelCreatePayload, @@ -13,33 +28,64 @@ export async function createChannel( parent_id: parentID, last_pin_timestamp: lastPinTimestamp, permission_overwrites, + nsfw, ...rest } = data; - const channel = { - ...rest, - /** The guild id of the channel if it is a guild channel. */ - guildID: guildID || rawGuildID || "", - /** The id of the last message sent in this channel */ - lastMessageID, - /** The amount of users allowed in this voice channel. */ - userLimit, - /** The rate limit(slowmode) in this text channel that users can send messages. */ - rateLimitPerUser, - /** The category id for this channel */ - parentID, - /** The last time when a message was pinned in this channel */ - lastPinTimestamp, - /** The permission overwrites for this channel */ - permissionOverwrites: permission_overwrites, - /** Whether this channel is nsfw or not */ - nsfw: data.nsfw || false, - /** The mention of the channel */ - mention: `<#${data.id}>`, - }; + const restProps = {}; + for (const key of Object.keys(rest)) { + // @ts-ignore + restProps[key] = createNewProp(rest[key]); + } + + const channel = Object.create(baseChannel, { + ...restProps, + guildID: createNewProp(guildID || rawGuildID || ""), + lastMessageID: createNewProp(lastMessageID), + userLimit: createNewProp(userLimit), + rateLimitPerUser: createNewProp(rateLimitPerUser), + parentID: createNewProp(parentID || undefined), + lastPinTimestamp: createNewProp(lastPinTimestamp ? Date.parse(lastPinTimestamp) : undefined), + permissionOverwrites: createNewProp(permission_overwrites || []), + nsfw: createNewProp(data.nsfw || false), + }); cacheHandlers.set("channels", data.id, channel); - return channel; + return channel as Channel; } -export interface Channel extends Unpromise> {} +export interface Channel { + /** The guild id of the channel if it is a guild channel. */ + guildID: string; + /** The id of the last message sent in this channel */ + lastMessageID?: string; + /** The amount of users allowed in this voice channel. */ + userLimit?: number; + /** The rate limit(slowmode) in this text channel that users can send messages. */ + rateLimitPerUser?: number; + /** The category id for this channel */ + parentID?: string; + /** The last time when a message was pinned in this channel */ + lastPinTimestamp?: number; + /** The permission overwrites for this channel */ + permissionOverwrites: RawOverwrite[]; + /** Whether this channel is nsfw or not */ + nsfw: boolean; + + // GETTERS + + /** + * Gets the guild object for this channel. + * + * ⚠️ ADVANCED: If you use the custom cache, these will not work for you. Getters can not be async and custom cache requires async. + */ + guild?: Guild; + /** + * Gets the messages from cache that were sent in this channel + * + * ⚠️ ADVANCED: If you use the custom cache, these will not work for you. Getters can not be async and custom cache requires async. + */ + messages: Collection; + /** The mention of the channel */ + mention: string; +} \ No newline at end of file