From 5428aae1b7c602e20f4bb60c61916fc35aee4470 Mon Sep 17 00:00:00 2001 From: DjDeveloperr Date: Wed, 2 Dec 2020 18:06:38 +0530 Subject: [PATCH 1/9] feat(mod): remove useless exports from mod.ts --- mod.ts | 114 ++++++++++++++++++++++++++------------------------------- 1 file changed, 51 insertions(+), 63 deletions(-) diff --git a/mod.ts b/mod.ts index d3f9103..2f6ef0c 100644 --- a/mod.ts +++ b/mod.ts @@ -1,70 +1,58 @@ -export * from './src/gateway/index.ts' +export { Gateway } from './src/gateway/index.ts' export * from './src/models/client.ts' -export * from './src/models/rest.ts' +export { RESTManager } from './src/models/rest.ts' export * from './src/models/cacheAdapter.ts' -export * from './src/models/shard.ts' export * from './src/models/command.ts' -export * from './src/models/extensions.ts' +export { + Extension, + ExtensionCommands, + ExtensionsManager +} from './src/models/extensions.ts' export * from './src/models/commandClient.ts' -export * from './src/managers/base.ts' -export * from './src/managers/baseChild.ts' -export * from './src/managers/channels.ts' -export * from './src/managers/emojis.ts' -export * from './src/managers/gatewayCache.ts' -export * from './src/managers/guildChannels.ts' -export * from './src/managers/guilds.ts' -export * from './src/managers/guildChannels.ts' -export * from './src/managers/guildEmojis.ts' -export * from './src/managers/members.ts' -export * from './src/managers/messageReactions.ts' -export * from './src/managers/reactionUsers.ts' -export * from './src/managers/messages.ts' -export * from './src/managers/roles.ts' -export * from './src/managers/users.ts' -export * from './src/structures/application.ts' -export * from './src/structures/base.ts' -export * from './src/structures/cdn.ts' -export * from './src/structures/channel.ts' -export * from './src/structures/dmChannel.ts' -export * from './src/structures/embed.ts' -export * from './src/structures/emoji.ts' -export * from './src/structures/groupChannel.ts' -export * from './src/structures/guild.ts' -export * from './src/structures/guildCategoryChannel.ts' -export * from './src/structures/guildNewsChannel.ts' -export * from './src/structures/guildVoiceChannel.ts' -export * from './src/structures/invite.ts' +export { BaseManager } from './src/managers/base.ts' +export { BaseChildManager } from './src/managers/baseChild.ts' +export { ChannelsManager } from './src/managers/channels.ts' +export { EmojisManager } from './src/managers/emojis.ts' +export { GatewayCache } from './src/managers/gatewayCache.ts' +export { GuildChannelsManager } from './src/managers/guildChannels.ts' +export { GuildManager } from './src/managers/guilds.ts' +export { GuildEmojisManager } from './src/managers/guildEmojis.ts' +export { MembersManager } from './src/managers/members.ts' +export { MessageReactionsManager } from './src/managers/messageReactions.ts' +export { ReactionUsersManager } from './src/managers/reactionUsers.ts' +export { MessagesManager } from './src/managers/messages.ts' +export { RolesManager } from './src/managers/roles.ts' +export { UserManager } from './src/managers/users.ts' +export { Application } from './src/structures/application.ts' +export { ImageURL } from './src/structures/cdn.ts' +export { Channel } from './src/structures/channel.ts' +export { DMChannel } from './src/structures/dmChannel.ts' +export { Embed } from './src/structures/embed.ts' +export { Emoji } from './src/structures/emoji.ts' +export { GroupDMChannel } from './src/structures/groupChannel.ts' +export { + Guild, + GuildBan, + GuildBans, + GuildIntegration +} from './src/structures/guild.ts' +export { CategoryChannel } from './src/structures/guildCategoryChannel.ts' +export { NewsChannel } from './src/structures/guildNewsChannel.ts' +export { VoiceChannel } from './src/structures/guildVoiceChannel.ts' +export { Invite } from './src/structures/invite.ts' export * from './src/structures/member.ts' -export * from './src/structures/message.ts' -export * from './src/structures/messageMentions.ts' -export * from './src/structures/presence.ts' -export * from './src/structures/role.ts' -export * from './src/structures/snowflake.ts' -export * from './src/structures/textChannel.ts' -export * from './src/structures/messageReaction.ts' -export * from './src/structures/user.ts' -export * from './src/structures/webhook.ts' -export * from './src/types/application.ts' -export * from './src/types/cdn.ts' -export * from './src/types/channel.ts' -export * from './src/types/emoji.ts' -export * from './src/types/endpoint.ts' -export * from './src/types/gateway.ts' -export * from './src/types/gatewayBot.ts' -export * from './src/types/gatewayResponse.ts' -export * from './src/types/guild.ts' -export * from './src/types/invite.ts' -export * from './src/types/permissionFlags.ts' -export * from './src/types/presence.ts' -export * from './src/types/role.ts' -export * from './src/types/template.ts' -export * from './src/types/user.ts' -export * from './src/types/voice.ts' -export * from './src/types/webhook.ts' -export * from './src/utils/collection.ts' -export * from './src/utils/intents.ts' -export * from './src/utils/buildInfo.ts' +export { Message } from './src/structures/message.ts' +export { MessageMentions } from './src/structures/messageMentions.ts' +export { Presence } from './src/structures/presence.ts' +export { Role } from './src/structures/role.ts' +export { Snowflake } from './src/structures/snowflake.ts' +export { TextChannel, GuildTextChannel } from './src/structures/textChannel.ts' +export { MessageReaction } from './src/structures/messageReaction.ts' +export { User } from './src/structures/user.ts' +export { Webhook } from './src/structures/webhook.ts' +export { Collection } from './src/utils/collection.ts' +export { Intents } from './src/utils/intents.ts' +export { getBuildInfo } from './src/utils/buildInfo.ts' export * from './src/utils/permissions.ts' -export * from './src/utils/userFlags.ts' +export { UserFlagsManager } from './src/utils/userFlags.ts' export * from './src/utils/bitfield.ts' -export * from './src/utils/getChannelByType.ts' From 62ed3ac9f7ae60f0e83943959f59e66c4eb2fe16 Mon Sep 17 00:00:00 2001 From: DjDeveloperr Date: Thu, 3 Dec 2020 08:49:39 +0530 Subject: [PATCH 2/9] feat(jsdoc): added some --- src/models/cacheAdapter.ts | 12 ++++++++++++ src/models/client.ts | 7 ++++++- src/models/command.ts | 2 ++ src/models/commandClient.ts | 15 +++++++++++++++ src/models/extensions.ts | 19 +++++++++++++++++++ src/models/rest.ts | 33 +++++++++++++++++++++++++-------- src/models/voice.ts | 2 +- 7 files changed, 80 insertions(+), 10 deletions(-) diff --git a/src/models/cacheAdapter.ts b/src/models/cacheAdapter.ts index 3b6fc08..64d7b01 100644 --- a/src/models/cacheAdapter.ts +++ b/src/models/cacheAdapter.ts @@ -5,19 +5,30 @@ import { RedisConnectOptions } from 'https://denopkg.com/keroxp/deno-redis/mod.ts' +/** + * ICacheAdapter is the interface to be implemented by Cache Adapters for them to be usable with Harmony. + * + * Methods can return Promises too. + */ export interface ICacheAdapter { + /** Get a key from a Cache */ get: (cacheName: string, key: string) => Promise | any + /** Set a key to value in a Cache Name with optional expire value in MS */ set: ( cacheName: string, key: string, value: any, expire?: number ) => Promise | any + /** Delete a key from a Cache */ delete: (cacheName: string, key: string) => Promise | boolean + /** Get array of all values in a Cache */ array: (cacheName: string) => undefined | any[] | Promise + /** Entirely delete a Cache */ deleteCache: (cacheName: string) => any } +/** Default Cache Adapter for in-memory caching. */ export class DefaultCacheAdapter implements ICacheAdapter { data: { [name: string]: Collection @@ -65,6 +76,7 @@ export class DefaultCacheAdapter implements ICacheAdapter { } } +/** Redis Cache Adatper for using Redis as a cache-provider. */ export class RedisCacheAdapter implements ICacheAdapter { _redis: Promise redis?: Redis diff --git a/src/models/client.ts b/src/models/client.ts index 17b96a9..e9d16a2 100644 --- a/src/models/client.ts +++ b/src/models/client.ts @@ -105,7 +105,12 @@ export class Client extends EventEmitter { this.fetchUncachedReactions = true } - /** Set Cache Adapter */ + /** + * Set Cache Adapter + * + * Should NOT set after bot is already logged in or using current cache. + * Please look into using `cache` option. + */ setAdapter(adapter: ICacheAdapter): Client { this.cache = adapter return this diff --git a/src/models/command.ts b/src/models/command.ts index 2df4e5e..b9ee863 100644 --- a/src/models/command.ts +++ b/src/models/command.ts @@ -403,12 +403,14 @@ export class CategoriesManager { } } +/** Parsed Command object */ export interface ParsedCommand { name: string args: string[] argString: string } +/** Parses a Command to later look for. */ export const parseCommand = ( client: CommandClient, msg: Message, diff --git a/src/models/commandClient.ts b/src/models/commandClient.ts index 69a8423..8fd09ce 100644 --- a/src/models/commandClient.ts +++ b/src/models/commandClient.ts @@ -11,19 +11,33 @@ import { ExtensionsManager } from './extensions.ts' type PrefixReturnType = string | string[] | Promise +/** Command Client options extending Client Options to provide a lot of Commands-related customizations */ export interface CommandClientOptions extends ClientOptions { + /** Global prefix(s) of the bot. */ prefix: string | string[] + /** Whether to enable mention prefix or not. */ mentionPrefix?: boolean + /** Method to get a Guild's custom prefix(s). */ getGuildPrefix?: (guildID: string) => PrefixReturnType + /** Method to get a User's custom prefix(s). */ getUserPrefix?: (userID: string) => PrefixReturnType + /** Method to check if certain Guild is blacklisted from using Commands. */ isGuildBlacklisted?: (guildID: string) => boolean | Promise + /** Method to check if certain User is blacklisted from using Commands. */ isUserBlacklisted?: (guildID: string) => boolean | Promise + /** Method to check if certain Channel is blacklisted from using Commands. */ isChannelBlacklisted?: (guildID: string) => boolean | Promise + /** Allow spaces after prefix? Recommended with Mention Prefix ON. */ spacesAfterPrefix?: boolean + /** Better Arguments regex to split at every whitespace. */ betterArgs?: boolean + /** List of Bot's Owner IDs whom can access `ownerOnly` commands. */ owners?: string[] + /** Whether to allow Bots to use Commands or not, not allowed by default. */ allowBots?: boolean + /** Whether to allow Commands in DMs or not, allowed by default. */ allowDMs?: boolean + /** Whether Commands should be case-sensitive or not, not by default. */ caseSensitive?: boolean } @@ -88,6 +102,7 @@ export class CommandClient extends Client implements CommandClientOptions { ) } + /** Process a Message to Execute Command. */ async processMessage(msg: Message): Promise { if (!this.allowBots && msg.author.bot === true) return diff --git a/src/models/extensions.ts b/src/models/extensions.ts index 8af3aee..c68bdc3 100644 --- a/src/models/extensions.ts +++ b/src/models/extensions.ts @@ -4,6 +4,7 @@ import { CommandClient } from './commandClient.ts' export type ExtensionEventCallback = (ext: Extension, ...args: any[]) => any +/** Extension Commands Manager */ export class ExtensionCommands { extension: Extension @@ -11,12 +12,14 @@ export class ExtensionCommands { this.extension = ext } + /** Get a list of Extension's Commands */ get list(): Collection { return this.extension.client.commands.list.filter( (c) => c.extension?.name === this.extension.name ) } + /** Get an Extension Command */ get(cmd: string): Command | undefined { const find = this.extension.client.commands.find(cmd) // linter sucks @@ -26,12 +29,14 @@ export class ExtensionCommands { else return find } + /** Add an Extension Command */ add(Cmd: Command | typeof Command): boolean { const cmd = Cmd instanceof Command ? Cmd : new Cmd() cmd.extension = this.extension return this.extension.client.commands.add(cmd) } + /** Delete an Extension Command */ delete(cmd: Command | string): boolean { const find = this.extension.client.commands.find( typeof cmd === 'string' ? cmd : cmd.name @@ -45,6 +50,7 @@ export class ExtensionCommands { else return this.extension.client.commands.delete(find) } + /** Delete all Commands of an Extension */ deleteAll(): void { for (const [cmd] of this.list) { this.delete(cmd) @@ -52,17 +58,23 @@ export class ExtensionCommands { } } +/** Customizable, isolated and pluggable Extensions are a great way of writing certain Modules independent of others */ export class Extension { client: CommandClient + /** Name of the Extension */ name: string = '' + /** Description of the Extension */ description?: string + /** Extensions's Commands Manager */ commands: ExtensionCommands = new ExtensionCommands(this) + /** Events registered by this Extension */ events: { [name: string]: (...args: any[]) => {} } = {} constructor(client: CommandClient) { this.client = client } + /** Listen for an Event through Extension. */ listen(event: string, cb: ExtensionEventCallback): boolean { if (this.events[event] !== undefined) return false else { @@ -76,10 +88,13 @@ export class Extension { } } + /** Method called upon loading of an Extension */ load(): any {} + /** Method called upon unloading of an Extension */ unload(): any {} } +/** Extensions Manager for CommandClient */ export class ExtensionsManager { client: CommandClient list: Collection = new Collection() @@ -88,14 +103,17 @@ export class ExtensionsManager { this.client = client } + /** Get an Extension by name */ get(ext: string): Extension | undefined { return this.list.get(ext) } + /** Check whether an Extension exists or not */ exists(ext: string): boolean { return this.get(ext) !== undefined } + /** Load an Extension onto Command Client */ load(ext: Extension | typeof Extension): void { // eslint-disable-next-line new-cap if (!(ext instanceof Extension)) ext = new ext(this.client) @@ -105,6 +123,7 @@ export class ExtensionsManager { ext.load() } + /** Unload an Extension from Command Client */ unload(ext: Extension | string): boolean { const name = typeof ext === 'string' ? ext : ext.name const extension = this.get(name) diff --git a/src/models/rest.ts b/src/models/rest.ts index 8bd889a..fd19bbf 100644 --- a/src/models/rest.ts +++ b/src/models/rest.ts @@ -65,7 +65,7 @@ export class RESTManager { this.handleRateLimits() } - async checkQueues(): Promise { + private async checkQueues(): Promise { Object.entries(this.queues).forEach(([key, value]) => { if (value.length === 0) { // eslint-disable-next-line @typescript-eslint/no-dynamic-delete @@ -74,7 +74,7 @@ export class RESTManager { }) } - queue(request: QueuedItem): void { + private queue(request: QueuedItem): void { const route = request.url.substring( // eslint seriously? // eslint-disable-next-line @typescript-eslint/restrict-plus-operands @@ -91,7 +91,7 @@ export class RESTManager { } } - async processQueue(): Promise { + private async processQueue(): Promise { if (Object.keys(this.queues).length !== 0 && !this.globalRateLimit) { await Promise.allSettled( Object.values(this.queues).map(async (pathQueue) => { @@ -139,7 +139,7 @@ export class RESTManager { } else this.processing = false } - prepare(body: any, method: RequestMethods): { [key: string]: any } { + private prepare(body: any, method: RequestMethods): { [key: string]: any } { const headers: RequestHeaders = { 'User-Agent': `DiscordBot (harmony, https://github.com/harmony-org/harmony)` } @@ -192,7 +192,7 @@ export class RESTManager { return data } - async isRateLimited(url: string): Promise { + private async isRateLimited(url: string): Promise { const global = this.rateLimits.get('global') const rateLimited = this.rateLimits.get(url) const now = Date.now() @@ -207,7 +207,10 @@ export class RESTManager { return false } - processHeaders(url: string, headers: Headers): string | null | undefined { + private processHeaders( + url: string, + headers: Headers + ): string | null | undefined { let rateLimited = false const global = headers.get('x-ratelimit-global') @@ -257,7 +260,7 @@ export class RESTManager { return rateLimited ? bucket : undefined } - async handleStatusCode( + private async handleStatusCode( response: Response, body: any, data: { [key: string]: any } @@ -304,6 +307,15 @@ export class RESTManager { } else throw new DiscordAPIError('Request - Unknown Error') } + /** + * Make a Request to Discord API + * @param method HTTP Method to use + * @param url URL of the Request + * @param body Body to send with Request + * @param maxRetries Number of Max Retries to perform + * @param bucket BucketID of the Request + * @param rawResponse Whether to get Raw Response or body itself + */ async make( method: RequestMethods, url: string, @@ -389,7 +401,7 @@ export class RESTManager { }) } - async handleRateLimits(): Promise { + private async handleRateLimits(): Promise { const now = Date.now() this.rateLimits.forEach((value, key) => { if (value.resetAt > now) return @@ -398,6 +410,7 @@ export class RESTManager { }) } + /** Make a GET Request to API */ async get( url: string, body?: unknown, @@ -408,6 +421,7 @@ export class RESTManager { return await this.make('get', url, body, maxRetries, bucket, rawResponse) } + /** Make a POST Request to API */ async post( url: string, body?: unknown, @@ -418,6 +432,7 @@ export class RESTManager { return await this.make('post', url, body, maxRetries, bucket, rawResponse) } + /** Make a DELETE Request to API */ async delete( url: string, body?: unknown, @@ -428,6 +443,7 @@ export class RESTManager { return await this.make('delete', url, body, maxRetries, bucket, rawResponse) } + /** Make a PATCH Request to API */ async patch( url: string, body?: unknown, @@ -438,6 +454,7 @@ export class RESTManager { return await this.make('patch', url, body, maxRetries, bucket, rawResponse) } + /** Make a PUT Request to API */ async put( url: string, body?: unknown, diff --git a/src/models/voice.ts b/src/models/voice.ts index 8c9e4a4..65c63e8 100644 --- a/src/models/voice.ts +++ b/src/models/voice.ts @@ -20,7 +20,7 @@ export class VoiceClient { } async connect(): Promise { - // TODO(DjDeveloperr): Actually understand what the hell docs say + // TODO(DjDeveloperr): understand docs return this } } From d1aba4a98137ce67487adc2f6f4a516098400f8d Mon Sep 17 00:00:00 2001 From: DjDeveloperr Date: Thu, 3 Dec 2020 09:36:41 +0530 Subject: [PATCH 3/9] feat(jsdoc): pretty much done --- mod.ts | 2 +- src/gateway/index.ts | 1 + src/managers/base.ts | 12 ++++++++ src/managers/baseChild.ts | 6 ++-- src/managers/channels.ts | 1 + src/managers/emojis.ts | 1 + src/managers/gatewayCache.ts | 5 ++++ src/managers/reactionUsers.ts | 4 +-- src/managers/users.ts | 2 +- src/models/client.ts | 4 +-- src/structures/base.ts | 54 ----------------------------------- src/structures/cdn.ts | 10 ++++--- src/structures/channel.ts | 3 -- src/structures/emoji.ts | 8 +----- src/structures/guild.ts | 1 - src/structures/invite.ts | 1 - src/structures/member.ts | 1 - src/structures/message.ts | 1 - src/structures/role.ts | 1 - src/structures/snowflake.ts | 9 ++++-- src/utils/bitfield.ts | 1 + src/utils/buildInfo.ts | 1 + src/utils/collection.ts | 14 +++++++++ src/utils/delay.ts | 1 + src/utils/getChannelByType.ts | 1 + src/utils/intents.ts | 3 +- src/utils/permissions.ts | 1 + 27 files changed, 65 insertions(+), 84 deletions(-) diff --git a/mod.ts b/mod.ts index 2f6ef0c..5ee3e76 100644 --- a/mod.ts +++ b/mod.ts @@ -22,7 +22,7 @@ export { MessageReactionsManager } from './src/managers/messageReactions.ts' export { ReactionUsersManager } from './src/managers/reactionUsers.ts' export { MessagesManager } from './src/managers/messages.ts' export { RolesManager } from './src/managers/roles.ts' -export { UserManager } from './src/managers/users.ts' +export { UsersManager } from './src/managers/users.ts' export { Application } from './src/structures/application.ts' export { ImageURL } from './src/structures/cdn.ts' export { Channel } from './src/structures/channel.ts' diff --git a/src/gateway/index.ts b/src/gateway/index.ts index 5763b40..d234661 100644 --- a/src/gateway/index.ts +++ b/src/gateway/index.ts @@ -26,6 +26,7 @@ export interface RequestMembersOptions { /** * Handles Discord gateway connection. + * * You should not use this and rather use Client class. */ class Gateway { diff --git a/src/managers/base.ts b/src/managers/base.ts index ce956ac..c268045 100644 --- a/src/managers/base.ts +++ b/src/managers/base.ts @@ -1,6 +1,11 @@ import { Client } from '../models/client.ts' import { Collection } from '../utils/collection.ts' +/** + * Managers handle caching data. And also some REST Methods as required. + * + * You should not be making Managers yourself. + */ export class BaseManager { client: Client /** Cache Name or Key used to differentiate caches */ @@ -14,30 +19,36 @@ export class BaseManager { this.DataType = DataType } + /** Get raw value from a cache (payload) */ async _get(key: string): Promise { return this.client.cache.get(this.cacheName, key) } + /** Get a value from Cache */ async get(key: string): Promise { const raw = await this._get(key) if (raw === undefined) return return new this.DataType(this.client, raw) } + /** Set a value to Cache */ async set(key: string, value: T): Promise { return this.client.cache.set(this.cacheName, key, value) } + /** Delete a key from Cache */ async delete(key: string): Promise { return this.client.cache.delete(this.cacheName, key) } + /** Get an Array of values from Cache */ async array(): Promise { let arr = await (this.client.cache.array(this.cacheName) as T[]) if (arr === undefined) arr = [] return arr.map((e) => new this.DataType(this.client, e)) as any } + /** Get a Collection of values from Cache */ async collection(): Promise> { const arr = await this.array() if (arr === undefined) return new Collection() @@ -49,6 +60,7 @@ export class BaseManager { return collection } + /** Delete everything from Cache */ flush(): any { return this.client.cache.deleteCache(this.cacheName) } diff --git a/src/managers/baseChild.ts b/src/managers/baseChild.ts index a799fc8..adc96f8 100644 --- a/src/managers/baseChild.ts +++ b/src/managers/baseChild.ts @@ -2,8 +2,10 @@ import { Client } from '../models/client.ts' import { Collection } from '../utils/collection.ts' import { BaseManager } from './base.ts' +/** Child Managers validate data from their parents i.e. from Managers */ export class BaseChildManager { client: Client + /** Parent Manager */ parent: BaseManager constructor(client: Client, parent: BaseManager) { @@ -32,8 +34,8 @@ export class BaseChildManager { if (arr === undefined) return new Collection() const collection = new Collection() for (const elem of arr) { - // @ts-expect-error - collection.set(elem.id, elem) + // any is required here. Else you would need ts-ignore or expect-error. + collection.set((elem as any).id, elem) } return collection } diff --git a/src/managers/channels.ts b/src/managers/channels.ts index a068eb3..8c0fc78 100644 --- a/src/managers/channels.ts +++ b/src/managers/channels.ts @@ -43,6 +43,7 @@ export class ChannelsManager extends BaseManager { return result } + /** Fetch a Channel by ID, cache it, resolve it */ async fetch(id: string): Promise { return await new Promise((resolve, reject) => { this.client.rest diff --git a/src/managers/emojis.ts b/src/managers/emojis.ts index 028e4e8..e5638bf 100644 --- a/src/managers/emojis.ts +++ b/src/managers/emojis.ts @@ -20,6 +20,7 @@ export class EmojisManager extends BaseManager { return emoji } + /** Fetch an Emoji by Guild ID and Emoji ID, cache it and resolve it */ async fetch(guildID: string, id: string): Promise { return await new Promise((resolve, reject) => { this.client.rest diff --git a/src/managers/gatewayCache.ts b/src/managers/gatewayCache.ts index 4813d41..7441a6a 100644 --- a/src/managers/gatewayCache.ts +++ b/src/managers/gatewayCache.ts @@ -1,5 +1,10 @@ import { Client } from '../models/client.ts' +/** + * Cache Manager used for Caching values related to Gateway connection + * + * In case of Redis, this will persistently cache session ID and seq for fast resumes. + */ export class GatewayCache { client: Client cacheName: string = 'discord_gateway_cache' diff --git a/src/managers/reactionUsers.ts b/src/managers/reactionUsers.ts index 332c66b..be78e13 100644 --- a/src/managers/reactionUsers.ts +++ b/src/managers/reactionUsers.ts @@ -1,8 +1,8 @@ import { Client } from '../models/client.ts' import { MessageReaction } from '../structures/messageReaction.ts' -import { UserManager } from './users.ts' +import { UsersManager } from './users.ts' -export class ReactionUsersManager extends UserManager { +export class ReactionUsersManager extends UsersManager { reaction: MessageReaction constructor(client: Client, reaction: MessageReaction) { diff --git a/src/managers/users.ts b/src/managers/users.ts index 46b36e6..26bec17 100644 --- a/src/managers/users.ts +++ b/src/managers/users.ts @@ -4,7 +4,7 @@ import { USER } from '../types/endpoint.ts' import { UserPayload } from '../types/user.ts' import { BaseManager } from './base.ts' -export class UserManager extends BaseManager { +export class UsersManager extends BaseManager { constructor(client: Client) { super(client, 'users', User) } diff --git a/src/models/client.ts b/src/models/client.ts index e9d16a2..23ec8c9 100644 --- a/src/models/client.ts +++ b/src/models/client.ts @@ -4,7 +4,7 @@ import { Gateway } from '../gateway/index.ts' import { RESTManager } from './rest.ts' import EventEmitter from 'https://deno.land/std@0.74.0/node/events.ts' import { DefaultCacheAdapter, ICacheAdapter } from './cacheAdapter.ts' -import { UserManager } from '../managers/users.ts' +import { UsersManager } from '../managers/users.ts' import { GuildManager } from '../managers/guilds.ts' import { ChannelsManager } from '../managers/channels.ts' import { ClientPresence } from '../structures/presence.ts' @@ -72,7 +72,7 @@ export class Client extends EventEmitter { /** Whether to fetch Uncached Message of Reaction or not? */ fetchUncachedReactions: boolean = false - users: UserManager = new UserManager(this) + users: UsersManager = new UsersManager(this) guilds: GuildManager = new GuildManager(this) channels: ChannelsManager = new ChannelsManager(this) emojis: EmojisManager = new EmojisManager(this) diff --git a/src/structures/base.ts b/src/structures/base.ts index 387de3f..c3f3e8d 100644 --- a/src/structures/base.ts +++ b/src/structures/base.ts @@ -1,63 +1,9 @@ import { Client } from '../models/client.ts' -interface IInit { - useCache?: boolean - endpoint: (...restURLfuncArgs: string[]) => string - restURLfuncArgs: string[] -} - export class Base { client: Client - static cacheName?: string - propertyConverterOverride: { [k: string]: string } = {} - static useCache?: boolean = true - static restFunc?: (...restURLfuncArgs: string[]) => string constructor(client: Client, _data?: any) { this.client = client } - - static async autoInit( - client: Client, - { useCache, endpoint, restURLfuncArgs }: IInit - ): Promise { - this.useCache = useCache - const cacheID = restURLfuncArgs.join(':') - if (this.useCache !== undefined) { - const cached = await client.cache.get( - this.cacheName ?? this.name, - cacheID - ) - if (cached !== undefined) { - return cached - } - } - - const jsonParsed = await client.rest.get(endpoint(...restURLfuncArgs)) - - return new this(client, jsonParsed) - } - - async refreshFromAPI( - client: Client, - { endpoint, restURLfuncArgs }: IInit - ): Promise { - const oldOne = Object.assign(Object.create(this), this) - - const jsonParsed = await client.rest.get(endpoint(...restURLfuncArgs)) - - this.readFromData(jsonParsed) - - return oldOne - } - - refreshFromData(data: { [k: string]: any }): this { - const oldOne = Object.assign(Object.create(this), this) - this.readFromData(data) - return oldOne - } - - protected readFromData(data: { [k: string]: any }): void {} - - // toJSON() {} } diff --git a/src/structures/cdn.ts b/src/structures/cdn.ts index fc75238..83ff27c 100644 --- a/src/structures/cdn.ts +++ b/src/structures/cdn.ts @@ -1,11 +1,13 @@ import { ImageFormats, ImageSize } from '../types/cdn.ts' +/** Function to get Image URL from a resource on Discord CDN */ export const ImageURL = ( url: string, - format: ImageFormats, - size?: ImageSize | 128 + format: ImageFormats | undefined = 'png', + size: ImageSize | undefined = 128 ): string => { + size = size === undefined ? 128 : size if (url.includes('a_')) { - return `${url}.gif?size=${size}` - } else return `${url}.${format}?size=${size}` + return `${url}.${format === undefined ? 'gif' : format}?size=${size}` + } else return `${url}.${format === 'gif' ? 'png' : format}?size=${size}` } diff --git a/src/structures/channel.ts b/src/structures/channel.ts index 031183b..1caf307 100644 --- a/src/structures/channel.ts +++ b/src/structures/channel.ts @@ -14,12 +14,9 @@ export class Channel extends Base { super(client, data) this.type = data.type this.id = data.id - // TODO: Cache in Gateway Event Code - // this.client.channels.set(this.id, data) } protected readFromData(data: ChannelPayload): void { - super.readFromData(data) this.type = data.type ?? this.type this.id = data.id ?? this.id } diff --git a/src/structures/emoji.ts b/src/structures/emoji.ts index f400901..f648d68 100644 --- a/src/structures/emoji.ts +++ b/src/structures/emoji.ts @@ -39,7 +39,6 @@ export class Emoji extends Base { } protected readFromData(data: EmojiPayload): void { - super.readFromData(data) this.id = data.id ?? this.id this.name = data.name ?? this.name this.roles = data.roles ?? this.roles @@ -47,11 +46,6 @@ export class Emoji extends Base { this.managed = data.managed ?? this.managed this.animated = data.animated ?? this.animated this.available = data.available ?? this.available - if (data.user !== undefined && data.user.id !== this.user?.id) { - User.autoInit(this.client, { - endpoint: USER, - restURLfuncArgs: [data.user.id] - }).then((user) => (this.user = user)) - } + if (data.user !== undefined) this.user = new User(this.client, data.user) } } diff --git a/src/structures/guild.ts b/src/structures/guild.ts index cd3f574..fd47834 100644 --- a/src/structures/guild.ts +++ b/src/structures/guild.ts @@ -220,7 +220,6 @@ export class Guild extends Base { } protected readFromData(data: GuildPayload): void { - super.readFromData(data) this.id = data.id ?? this.id this.unavailable = data.unavailable ?? this.unavailable diff --git a/src/structures/invite.ts b/src/structures/invite.ts index d84ceb8..cc40490 100644 --- a/src/structures/invite.ts +++ b/src/structures/invite.ts @@ -32,7 +32,6 @@ export class Invite extends Base { } protected readFromData(data: InvitePayload): void { - super.readFromData(data) this.code = data.code ?? this.code this.guild = data.guild ?? this.guild this.channel = data.channel ?? this.channel diff --git a/src/structures/member.ts b/src/structures/member.ts index f74d3be..6958122 100644 --- a/src/structures/member.ts +++ b/src/structures/member.ts @@ -57,7 +57,6 @@ export class Member extends Base { } protected readFromData(data: MemberPayload): void { - super.readFromData(data.user) this.nick = data.nick ?? this.nick this.joinedAt = data.joined_at ?? this.joinedAt this.premiumSince = data.premium_since ?? this.premiumSince diff --git a/src/structures/message.ts b/src/structures/message.ts index 1a57a74..23d666d 100644 --- a/src/structures/message.ts +++ b/src/structures/message.ts @@ -82,7 +82,6 @@ export class Message extends Base { } protected readFromData(data: MessagePayload): void { - super.readFromData(data) this.channelID = data.channel_id ?? this.channelID this.guildID = data.guild_id ?? this.guildID this.content = data.content ?? this.content diff --git a/src/structures/role.ts b/src/structures/role.ts index f670cfa..de08e5d 100644 --- a/src/structures/role.ts +++ b/src/structures/role.ts @@ -34,7 +34,6 @@ export class Role extends Base { } protected readFromData(data: RolePayload): void { - super.readFromData(data) this.name = data.name ?? this.name this.color = data.color ?? this.color this.hoist = data.hoist ?? this.hoist diff --git a/src/structures/snowflake.ts b/src/structures/snowflake.ts index a88d5f8..d76e409 100644 --- a/src/structures/snowflake.ts +++ b/src/structures/snowflake.ts @@ -1,7 +1,12 @@ export class Snowflake { - snowflake: bigint + id: string + constructor(id: string) { - this.snowflake = BigInt.asUintN(64, BigInt(id)) + this.id = id + } + + get snowflake(): bigint { + return BigInt.asUintN(64, BigInt(this.id)) } get timestamp(): string { diff --git a/src/utils/bitfield.ts b/src/utils/bitfield.ts index 379d53d..54a595d 100644 --- a/src/utils/bitfield.ts +++ b/src/utils/bitfield.ts @@ -1,6 +1,7 @@ // Ported from https://github.com/discordjs/discord.js/blob/master/src/util/BitField.js export type BitFieldResolvable = number | BitField | string | BitField[] +/** Bit Field utility to work with Bits and Flags */ export class BitField { flags: { [name: string]: number } = {} bitfield: number diff --git a/src/utils/buildInfo.ts b/src/utils/buildInfo.ts index 3a29765..753d9a5 100644 --- a/src/utils/buildInfo.ts +++ b/src/utils/buildInfo.ts @@ -1,6 +1,7 @@ /* eslint-disable @typescript-eslint/naming-convention */ import { Client } from '../models/client.ts' +/** Gets Discord Build info for self-bot support */ export const getBuildInfo = ( client: Client ): { diff --git a/src/utils/collection.ts b/src/utils/collection.ts index 2af42f9..01f6b96 100644 --- a/src/utils/collection.ts +++ b/src/utils/collection.ts @@ -1,25 +1,32 @@ +/** Enhanced Map with various utility functions */ export class Collection extends Map { + /** Set a key to value in Collection */ set(key: K, value: V): this { return super.set(key, value) } + /** Get Array of values in Collection */ array(): V[] { return [...this.values()] } + /** Get first value in Collection */ first(): V { return this.values().next().value } + /** Get last value in Collection */ last(): V { return [...this.values()][this.size - 1] } + /** Get a random value from Collection */ random(): V { const arr = [...this.values()] return arr[Math.floor(Math.random() * arr.length)] } + /** Find a value from Collection using callback */ find(callback: (value: V, key: K) => boolean): V | undefined { for (const key of this.keys()) { const value = this.get(key) as V @@ -28,6 +35,7 @@ export class Collection extends Map { } } + /** Filter out the Collection using callback */ filter(callback: (value: V, key: K) => boolean): Collection { const relevant = new Collection() this.forEach((value, key) => { @@ -36,6 +44,7 @@ export class Collection extends Map { return relevant } + /** Map the collection */ map(callback: (value: V, key: K) => T): T[] { const results = [] for (const key of this.keys()) { @@ -45,6 +54,7 @@ export class Collection extends Map { return results } + /** Check if any of the values/keys in Collection satisfy callback */ some(callback: (value: V, key: K) => boolean): boolean { for (const key of this.keys()) { const value = this.get(key) as V @@ -53,6 +63,7 @@ export class Collection extends Map { return false } + /** Check if every value/key in Collection satisfy callback */ every(callback: (value: V, key: K) => boolean): boolean { for (const key of this.keys()) { const value = this.get(key) as V @@ -61,6 +72,7 @@ export class Collection extends Map { return true } + /** Reduce the Collection to a single value */ reduce( callback: (accumulator: T, value: V, key: K) => T, initialValue?: T @@ -75,10 +87,12 @@ export class Collection extends Map { return accumulator } + /** Create a Collection from an Object */ static fromObject(object: { [key: string]: V }): Collection { return new Collection(Object.entries(object)) } + /** Convert Collection to an object */ toObject(): { [name: string]: V } { return Object.fromEntries(this) } diff --git a/src/utils/delay.ts b/src/utils/delay.ts index 98acaee..d40dc24 100644 --- a/src/utils/delay.ts +++ b/src/utils/delay.ts @@ -1,3 +1,4 @@ +/** Delay by `ms` miliseconds */ export const delay = async (ms: number): Promise => await new Promise((resolve, reject) => { setTimeout(() => resolve(true), ms) diff --git a/src/utils/getChannelByType.ts b/src/utils/getChannelByType.ts index 1c320ea..3915d92 100644 --- a/src/utils/getChannelByType.ts +++ b/src/utils/getChannelByType.ts @@ -45,6 +45,7 @@ export type EveryChannelPayloadTypes = | GuildVoiceChannelPayload | EveryTextChannelPayloadTypes +/** Get appropriate Channel structure by its type */ const getChannelByType = ( client: Client, data: EveryChannelPayloadTypes, diff --git a/src/utils/intents.ts b/src/utils/intents.ts index 6106fdb..34a1cc1 100644 --- a/src/utils/intents.ts +++ b/src/utils/intents.ts @@ -2,7 +2,7 @@ import { GatewayIntents } from '../types/gateway.ts' export type PriviligedIntents = 'GUILD_MEMBERS' | 'GUILD_PRESENCES' -// eslint-disable-next-line @typescript-eslint/no-extraneous-class +/** Utility class for handling Gateway Intents */ export class Intents { static NonPriviliged: number[] = [ GatewayIntents.GUILD_MESSAGES, @@ -38,6 +38,7 @@ export class Intents { static None: number[] = [...Intents.NonPriviliged] + /** Create an Array of Intents easily passing Intents you're priviliged for and disable the ones you don't need */ static create( priviliged?: PriviligedIntents[], disable?: number[] diff --git a/src/utils/permissions.ts b/src/utils/permissions.ts index 88ea5d1..e5d35ad 100644 --- a/src/utils/permissions.ts +++ b/src/utils/permissions.ts @@ -8,6 +8,7 @@ export type PermissionResolvable = | Permissions | PermissionResolvable[] +/** Manages Discord's Bit-based Permissions */ export class Permissions extends BitField { static DEFAULT = 104324673 static ALL = Object.values(PermissionFlags).reduce((all, p) => all | p, 0) From db01e3eec1333d052921984291cb2966f80d1cf5 Mon Sep 17 00:00:00 2001 From: DjDeveloperr Date: Thu, 3 Dec 2020 09:42:21 +0530 Subject: [PATCH 4/9] feat(docs): remove not so useful exports --- mod.ts | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/mod.ts b/mod.ts index 5ee3e76..1c1b1b0 100644 --- a/mod.ts +++ b/mod.ts @@ -8,7 +8,8 @@ export { ExtensionCommands, ExtensionsManager } from './src/models/extensions.ts' -export * from './src/models/commandClient.ts' +export { CommandClient } from './src/models/commandClient.ts' +export type { CommandClientOptions } from './src/models/commandClient.ts' export { BaseManager } from './src/managers/base.ts' export { BaseChildManager } from './src/managers/baseChild.ts' export { ChannelsManager } from './src/managers/channels.ts' @@ -24,7 +25,7 @@ export { MessagesManager } from './src/managers/messages.ts' export { RolesManager } from './src/managers/roles.ts' export { UsersManager } from './src/managers/users.ts' export { Application } from './src/structures/application.ts' -export { ImageURL } from './src/structures/cdn.ts' +// export { ImageURL } from './src/structures/cdn.ts' export { Channel } from './src/structures/channel.ts' export { DMChannel } from './src/structures/dmChannel.ts' export { Embed } from './src/structures/embed.ts' @@ -52,7 +53,7 @@ export { User } from './src/structures/user.ts' export { Webhook } from './src/structures/webhook.ts' export { Collection } from './src/utils/collection.ts' export { Intents } from './src/utils/intents.ts' -export { getBuildInfo } from './src/utils/buildInfo.ts' +// export { getBuildInfo } from './src/utils/buildInfo.ts' export * from './src/utils/permissions.ts' export { UserFlagsManager } from './src/utils/userFlags.ts' export * from './src/utils/bitfield.ts' From caac5fc97e9b894ad2b05680225888927abe6a48 Mon Sep 17 00:00:00 2001 From: DjDeveloperr Date: Thu, 3 Dec 2020 10:12:26 +0530 Subject: [PATCH 5/9] feat(jsdoc): done --- mod.ts | 1 + src/models/client.ts | 11 ++++++++++- src/test/cmd.ts | 4 ++-- 3 files changed, 13 insertions(+), 3 deletions(-) diff --git a/mod.ts b/mod.ts index 1c1b1b0..33ffdfe 100644 --- a/mod.ts +++ b/mod.ts @@ -16,6 +16,7 @@ export { ChannelsManager } from './src/managers/channels.ts' export { EmojisManager } from './src/managers/emojis.ts' export { GatewayCache } from './src/managers/gatewayCache.ts' export { GuildChannelsManager } from './src/managers/guildChannels.ts' +export type { GuildChannel } from './src/managers/guildChannels.ts' export { GuildManager } from './src/managers/guilds.ts' export { GuildEmojisManager } from './src/managers/guildEmojis.ts' export { MembersManager } from './src/managers/members.ts' diff --git a/src/models/client.ts b/src/models/client.ts index 23ec8c9..e295fff 100644 --- a/src/models/client.ts +++ b/src/models/client.ts @@ -36,7 +36,7 @@ export interface ClientOptions { fetchUncachedReactions?: boolean } -export declare interface Client { +export interface ClientEventsEmitter { on: (event: U, listener: ClientEvents[U]) => this emit: ( @@ -84,6 +84,15 @@ export class Client extends EventEmitter { /** Client's presence. Startup one if set before connecting */ presence: ClientPresence = new ClientPresence() + private _untypedOn = this.on + private _untypedEmit = this.emit + public on = (event: K, listener: ClientEvents[K]): this => + this._untypedOn(event, listener) + public emit = ( + event: K, + ...args: Parameters + ): boolean => this._untypedEmit(event, ...args) + constructor(options: ClientOptions = {}) { super() this.token = options.token diff --git a/src/test/cmd.ts b/src/test/cmd.ts index 2530c83..ea2fec1 100644 --- a/src/test/cmd.ts +++ b/src/test/cmd.ts @@ -2,9 +2,9 @@ import { Command, CommandClient, Intents, - GuildChannel, CommandContext, - Extension + Extension, + GuildChannel } from '../../mod.ts' import { Invite } from '../structures/invite.ts' import { TOKEN } from './config.ts' From 66dbd7889c6b24d2058a55182d81b453309b2869 Mon Sep 17 00:00:00 2001 From: DjDeveloperr Date: Thu, 3 Dec 2020 10:19:09 +0530 Subject: [PATCH 6/9] feat(jsdoc): remove useless class --- src/models/client.ts | 9 --------- 1 file changed, 9 deletions(-) diff --git a/src/models/client.ts b/src/models/client.ts index e295fff..fc348c2 100644 --- a/src/models/client.ts +++ b/src/models/client.ts @@ -36,15 +36,6 @@ export interface ClientOptions { fetchUncachedReactions?: boolean } -export interface ClientEventsEmitter { - on: (event: U, listener: ClientEvents[U]) => this - - emit: ( - event: U, - ...args: Parameters - ) => boolean -} - /** * Discord Client. */ From 4858bde6bc20a669e7516daa38e83801c8989e37 Mon Sep 17 00:00:00 2001 From: DjDeveloperr Date: Thu, 3 Dec 2020 10:32:19 +0530 Subject: [PATCH 7/9] feat(jsdoc): test fix for deno doc --- src/models/client.ts | 16 ++++++++-------- src/test/config.ts.sample | 3 ++- 2 files changed, 10 insertions(+), 9 deletions(-) diff --git a/src/models/client.ts b/src/models/client.ts index fc348c2..0676db8 100644 --- a/src/models/client.ts +++ b/src/models/client.ts @@ -75,14 +75,14 @@ export class Client extends EventEmitter { /** Client's presence. Startup one if set before connecting */ presence: ClientPresence = new ClientPresence() - private _untypedOn = this.on - private _untypedEmit = this.emit - public on = (event: K, listener: ClientEvents[K]): this => - this._untypedOn(event, listener) - public emit = ( - event: K, - ...args: Parameters - ): boolean => this._untypedEmit(event, ...args) + // private _untypedOn = this.on + // private _untypedEmit = this.emit + // public on = (event: K, listener: ClientEvents[K]): this => + // this._untypedOn(event, listener) + // public emit = ( + // event: K, + // ...args: Parameters + // ): boolean => this._untypedEmit(event, ...args) constructor(options: ClientOptions = {}) { super() diff --git a/src/test/config.ts.sample b/src/test/config.ts.sample index 14fec6b..ce7d22b 100644 --- a/src/test/config.ts.sample +++ b/src/test/config.ts.sample @@ -1 +1,2 @@ -export const TOKEN = '' \ No newline at end of file +export const TOKEN = '' +export const WEBHOOK = '' \ No newline at end of file From a8935fc40383f2e72c2d4104b8bb0ae54cc1ca1a Mon Sep 17 00:00:00 2001 From: DjDeveloperr Date: Thu, 3 Dec 2020 10:35:29 +0530 Subject: [PATCH 8/9] fix(jsdoc) --- mod.ts | 11 ++++++++++- src/models/client.ts | 16 ++++++++-------- 2 files changed, 18 insertions(+), 9 deletions(-) diff --git a/mod.ts b/mod.ts index 33ffdfe..b6477cc 100644 --- a/mod.ts +++ b/mod.ts @@ -1,8 +1,17 @@ +export { default as EventEmitter } from 'https://deno.land/std@0.74.0/node/events.ts' +export { Base } from './src/structures/base.ts' export { Gateway } from './src/gateway/index.ts' export * from './src/models/client.ts' export { RESTManager } from './src/models/rest.ts' export * from './src/models/cacheAdapter.ts' -export * from './src/models/command.ts' +export { + Command, + CommandBuilder, + CommandCategory, + CommandsManager, + CategoriesManager +} from './src/models/command.ts' +export type { CommandContext } from './src/models/command.ts' export { Extension, ExtensionCommands, diff --git a/src/models/client.ts b/src/models/client.ts index 0676db8..fc348c2 100644 --- a/src/models/client.ts +++ b/src/models/client.ts @@ -75,14 +75,14 @@ export class Client extends EventEmitter { /** Client's presence. Startup one if set before connecting */ presence: ClientPresence = new ClientPresence() - // private _untypedOn = this.on - // private _untypedEmit = this.emit - // public on = (event: K, listener: ClientEvents[K]): this => - // this._untypedOn(event, listener) - // public emit = ( - // event: K, - // ...args: Parameters - // ): boolean => this._untypedEmit(event, ...args) + private _untypedOn = this.on + private _untypedEmit = this.emit + public on = (event: K, listener: ClientEvents[K]): this => + this._untypedOn(event, listener) + public emit = ( + event: K, + ...args: Parameters + ): boolean => this._untypedEmit(event, ...args) constructor(options: ClientOptions = {}) { super() From 174d3cb58c8d09a0bfb1b1875b263c3c5a2eda9e Mon Sep 17 00:00:00 2001 From: DjDeveloperr Date: Thu, 3 Dec 2020 10:58:20 +0530 Subject: [PATCH 9/9] fix(lint) & fix(code) --- src/gateway/handlers/channelPinsUpdate.ts | 14 ++++++++------ src/gateway/handlers/guildDelete.ts | 4 +--- src/gateway/handlers/guildUpdate.ts | 6 +++--- src/gateway/handlers/index.ts | 2 +- src/models/client.ts | 7 +++++-- src/structures/channel.ts | 2 +- src/structures/dmChannel.ts | 2 +- src/structures/emoji.ts | 3 +-- src/structures/groupChannel.ts | 2 +- src/structures/guild.ts | 2 +- src/structures/guildCategoryChannel.ts | 2 +- src/structures/guildNewsChannel.ts | 2 +- src/structures/guildVoiceChannel.ts | 2 +- src/structures/invite.ts | 2 +- src/structures/member.ts | 2 +- src/structures/message.ts | 2 +- src/structures/role.ts | 2 +- src/structures/textChannel.ts | 4 ++-- src/structures/user.ts | 3 +-- src/structures/voiceState.ts | 3 +-- src/utils/intents.ts | 1 + 21 files changed, 35 insertions(+), 34 deletions(-) diff --git a/src/gateway/handlers/channelPinsUpdate.ts b/src/gateway/handlers/channelPinsUpdate.ts index 1423787..088524c 100644 --- a/src/gateway/handlers/channelPinsUpdate.ts +++ b/src/gateway/handlers/channelPinsUpdate.ts @@ -6,18 +6,20 @@ export const channelPinsUpdate: GatewayEventHandler = async ( gateway: Gateway, d: ChannelPinsUpdatePayload ) => { - const after: + const before: | TextChannel | undefined = await gateway.client.channels.get(d.channel_id) - if (after !== undefined) { - const before = after.refreshFromData({ - last_pin_timestamp: d.last_pin_timestamp - }) + + if (before !== undefined) { const raw = await gateway.client.channels._get(d.channel_id) + if (raw === undefined) return await gateway.client.channels.set( - after.id, + raw.id, Object.assign(raw, { last_pin_timestamp: d.last_pin_timestamp }) ) + const after = ((await gateway.client.channels.get( + d.channel_id + )) as unknown) as TextChannel gateway.client.emit('channelPinsUpdate', before, after) } } diff --git a/src/gateway/handlers/guildDelete.ts b/src/gateway/handlers/guildDelete.ts index 286ffc7..9355ab2 100644 --- a/src/gateway/handlers/guildDelete.ts +++ b/src/gateway/handlers/guildDelete.ts @@ -2,15 +2,13 @@ import { Guild } from '../../structures/guild.ts' import { GuildPayload } from '../../types/guild.ts' import { Gateway, GatewayEventHandler } from '../index.ts' -export const guildDelte: GatewayEventHandler = async ( +export const guildDelete: GatewayEventHandler = async ( gateway: Gateway, d: GuildPayload ) => { const guild: Guild | undefined = await gateway.client.guilds.get(d.id) if (guild !== undefined) { - guild.refreshFromData(d) - await guild.members.flush() await guild.channels.flush() await guild.roles.flush() diff --git a/src/gateway/handlers/guildUpdate.ts b/src/gateway/handlers/guildUpdate.ts index c7e7cc4..7f2d168 100644 --- a/src/gateway/handlers/guildUpdate.ts +++ b/src/gateway/handlers/guildUpdate.ts @@ -6,9 +6,9 @@ export const guildUpdate: GatewayEventHandler = async ( gateway: Gateway, d: GuildPayload ) => { - const after: Guild | undefined = await gateway.client.guilds.get(d.id) - if (after === undefined) return - const before = after.refreshFromData(d) + const before: Guild | undefined = await gateway.client.guilds.get(d.id) + if (before === undefined) return await gateway.client.guilds.set(d.id, d) + const after = ((await gateway.client.guilds.get(d.id)) as unknown) as Guild gateway.client.emit('guildUpdate', before, after) } diff --git a/src/gateway/handlers/index.ts b/src/gateway/handlers/index.ts index 01282ef..8934068 100644 --- a/src/gateway/handlers/index.ts +++ b/src/gateway/handlers/index.ts @@ -5,7 +5,7 @@ import { channelDelete } from './channelDelete.ts' import { channelUpdate } from './channelUpdate.ts' import { channelPinsUpdate } from './channelPinsUpdate.ts' import { guildCreate } from './guildCreate.ts' -import { guildDelte as guildDelete } from './guildDelete.ts' +import { guildDelete } from './guildDelete.ts' import { guildUpdate } from './guildUpdate.ts' import { guildBanAdd } from './guildBanAdd.ts' import { ready } from './ready.ts' diff --git a/src/models/client.ts b/src/models/client.ts index fc348c2..7aa3a70 100644 --- a/src/models/client.ts +++ b/src/models/client.ts @@ -75,10 +75,13 @@ export class Client extends EventEmitter { /** Client's presence. Startup one if set before connecting */ presence: ClientPresence = new ClientPresence() - private _untypedOn = this.on - private _untypedEmit = this.emit + private readonly _untypedOn = this.on + + private readonly _untypedEmit = this.emit + public on = (event: K, listener: ClientEvents[K]): this => this._untypedOn(event, listener) + public emit = ( event: K, ...args: Parameters diff --git a/src/structures/channel.ts b/src/structures/channel.ts index 1caf307..3097330 100644 --- a/src/structures/channel.ts +++ b/src/structures/channel.ts @@ -16,7 +16,7 @@ export class Channel extends Base { this.id = data.id } - protected readFromData(data: ChannelPayload): void { + readFromData(data: ChannelPayload): void { this.type = data.type ?? this.type this.id = data.id ?? this.id } diff --git a/src/structures/dmChannel.ts b/src/structures/dmChannel.ts index 64e1896..dc3bbff 100644 --- a/src/structures/dmChannel.ts +++ b/src/structures/dmChannel.ts @@ -11,7 +11,7 @@ export class DMChannel extends TextChannel { this.recipients = data.recipients } - protected readFromData(data: DMChannelPayload): void { + readFromData(data: DMChannelPayload): void { super.readFromData(data) this.recipients = data.recipients ?? this.recipients } diff --git a/src/structures/emoji.ts b/src/structures/emoji.ts index f648d68..0fbc8fe 100644 --- a/src/structures/emoji.ts +++ b/src/structures/emoji.ts @@ -1,6 +1,5 @@ import { Client } from '../models/client.ts' import { EmojiPayload } from '../types/emoji.ts' -import { USER } from '../types/endpoint.ts' import { Base } from './base.ts' import { Guild } from './guild.ts' import { User } from './user.ts' @@ -38,7 +37,7 @@ export class Emoji extends Base { this.available = data.available } - protected readFromData(data: EmojiPayload): void { + readFromData(data: EmojiPayload): void { this.id = data.id ?? this.id this.name = data.name ?? this.name this.roles = data.roles ?? this.roles diff --git a/src/structures/groupChannel.ts b/src/structures/groupChannel.ts index ee7e1e4..33a31b2 100644 --- a/src/structures/groupChannel.ts +++ b/src/structures/groupChannel.ts @@ -17,7 +17,7 @@ export class GroupDMChannel extends Channel { // cache.set('groupchannel', this.id, this) } - protected readFromData(data: GroupDMChannelPayload): void { + readFromData(data: GroupDMChannelPayload): void { super.readFromData(data) this.name = data.name ?? this.name this.icon = data.icon ?? this.icon diff --git a/src/structures/guild.ts b/src/structures/guild.ts index fd47834..e515235 100644 --- a/src/structures/guild.ts +++ b/src/structures/guild.ts @@ -219,7 +219,7 @@ export class Guild extends Base { } } - protected readFromData(data: GuildPayload): void { + readFromData(data: GuildPayload): void { this.id = data.id ?? this.id this.unavailable = data.unavailable ?? this.unavailable diff --git a/src/structures/guildCategoryChannel.ts b/src/structures/guildCategoryChannel.ts index 186c2d2..104b70b 100644 --- a/src/structures/guildCategoryChannel.ts +++ b/src/structures/guildCategoryChannel.ts @@ -25,7 +25,7 @@ export class CategoryChannel extends Channel { // cache.set('guildcategorychannel', this.id, this) } - protected readFromData(data: GuildChannelCategoryPayload): void { + readFromData(data: GuildChannelCategoryPayload): void { super.readFromData(data) this.guildID = data.guild_id ?? this.guildID this.name = data.name ?? this.name diff --git a/src/structures/guildNewsChannel.ts b/src/structures/guildNewsChannel.ts index b5e750f..e9bbf72 100644 --- a/src/structures/guildNewsChannel.ts +++ b/src/structures/guildNewsChannel.ts @@ -25,7 +25,7 @@ export class NewsChannel extends TextChannel { this.topic = data.topic } - protected readFromData(data: GuildNewsChannelPayload): void { + readFromData(data: GuildNewsChannelPayload): void { super.readFromData(data) this.guildID = data.guild_id ?? this.guildID this.name = data.name ?? this.name diff --git a/src/structures/guildVoiceChannel.ts b/src/structures/guildVoiceChannel.ts index d64abf4..7eebdf4 100644 --- a/src/structures/guildVoiceChannel.ts +++ b/src/structures/guildVoiceChannel.ts @@ -29,7 +29,7 @@ export class VoiceChannel extends Channel { // cache.set('guildvoicechannel', this.id, this) } - protected readFromData(data: GuildVoiceChannelPayload): void { + readFromData(data: GuildVoiceChannelPayload): void { super.readFromData(data) this.bitrate = data.bitrate ?? this.bitrate this.userLimit = data.user_limit ?? this.userLimit diff --git a/src/structures/invite.ts b/src/structures/invite.ts index cc40490..59d172d 100644 --- a/src/structures/invite.ts +++ b/src/structures/invite.ts @@ -31,7 +31,7 @@ export class Invite extends Base { this.approximatePresenceCount = data.approximate_presence_count } - protected readFromData(data: InvitePayload): void { + readFromData(data: InvitePayload): void { this.code = data.code ?? this.code this.guild = data.guild ?? this.guild this.channel = data.channel ?? this.channel diff --git a/src/structures/member.ts b/src/structures/member.ts index 6958122..6996527 100644 --- a/src/structures/member.ts +++ b/src/structures/member.ts @@ -56,7 +56,7 @@ export class Member extends Base { return this.user.nickMention } - protected readFromData(data: MemberPayload): void { + readFromData(data: MemberPayload): void { this.nick = data.nick ?? this.nick this.joinedAt = data.joined_at ?? this.joinedAt this.premiumSince = data.premium_since ?? this.premiumSince diff --git a/src/structures/message.ts b/src/structures/message.ts index 23d666d..cf5a40d 100644 --- a/src/structures/message.ts +++ b/src/structures/message.ts @@ -81,7 +81,7 @@ export class Message extends Base { this.channel = channel } - protected readFromData(data: MessagePayload): void { + readFromData(data: MessagePayload): void { this.channelID = data.channel_id ?? this.channelID this.guildID = data.guild_id ?? this.guildID this.content = data.content ?? this.content diff --git a/src/structures/role.ts b/src/structures/role.ts index de08e5d..01ada1d 100644 --- a/src/structures/role.ts +++ b/src/structures/role.ts @@ -33,7 +33,7 @@ export class Role extends Base { this.mentionable = data.mentionable } - protected readFromData(data: RolePayload): void { + readFromData(data: RolePayload): void { this.name = data.name ?? this.name this.color = data.color ?? this.color this.hoist = data.hoist ?? this.hoist diff --git a/src/structures/textChannel.ts b/src/structures/textChannel.ts index db0ffec..5fed099 100644 --- a/src/structures/textChannel.ts +++ b/src/structures/textChannel.ts @@ -27,7 +27,7 @@ export class TextChannel extends Channel { this.lastPinTimestamp = data.last_pin_timestamp } - protected readFromData(data: TextChannelPayload): void { + readFromData(data: TextChannelPayload): void { super.readFromData(data) this.lastMessageID = data.last_message_id ?? this.lastMessageID this.lastPinTimestamp = data.last_pin_timestamp ?? this.lastPinTimestamp @@ -152,7 +152,7 @@ export class GuildTextChannel extends TextChannel { this.rateLimit = data.rate_limit_per_user } - protected readFromData(data: GuildTextChannelPayload): void { + readFromData(data: GuildTextChannelPayload): void { super.readFromData(data) this.guildID = data.guild_id ?? this.guildID this.name = data.name ?? this.name diff --git a/src/structures/user.ts b/src/structures/user.ts index c0b002b..d3e9f19 100644 --- a/src/structures/user.ts +++ b/src/structures/user.ts @@ -54,8 +54,7 @@ export class User extends Base { this.publicFlags = new UserFlagsManager(data.public_flags) } - protected readFromData(data: UserPayload): void { - super.readFromData(data) + readFromData(data: UserPayload): void { this.username = data.username ?? this.username this.discriminator = data.discriminator ?? this.discriminator this.avatar = data.avatar ?? this.avatar diff --git a/src/structures/voiceState.ts b/src/structures/voiceState.ts index ece1128..0633b13 100644 --- a/src/structures/voiceState.ts +++ b/src/structures/voiceState.ts @@ -43,8 +43,7 @@ export class VoiceState extends Base { this.suppress = data.suppress } - protected readFromData(data: VoiceStatePayload): void { - super.readFromData(data) + readFromData(data: VoiceStatePayload): void { this.sessionID = data.session_id ?? this.sessionID this.deaf = data.deaf ?? this.deaf this.mute = data.mute ?? this.mute diff --git a/src/utils/intents.ts b/src/utils/intents.ts index 34a1cc1..35503fc 100644 --- a/src/utils/intents.ts +++ b/src/utils/intents.ts @@ -2,6 +2,7 @@ import { GatewayIntents } from '../types/gateway.ts' export type PriviligedIntents = 'GUILD_MEMBERS' | 'GUILD_PRESENCES' +/* eslint-disable @typescript-eslint/no-extraneous-class */ /** Utility class for handling Gateway Intents */ export class Intents { static NonPriviliged: number[] = [