From 5b2a34151392d3dc8c75b795b7f12db0e37f1d2d Mon Sep 17 00:00:00 2001 From: Skillz4Killz <23035000+Skillz4Killz@users.noreply.github.com> Date: Sun, 4 Apr 2021 01:49:32 +0000 Subject: [PATCH 1/2] feat: add cache.emojis getter. closes #658 --- src/cache.ts | 34 ++++++++++++++++++++++++---------- 1 file changed, 24 insertions(+), 10 deletions(-) diff --git a/src/cache.ts b/src/cache.ts index e8678402c..598396cc2 100644 --- a/src/cache.ts +++ b/src/cache.ts @@ -1,24 +1,38 @@ // deno-lint-ignore-file require-await no-explicit-any prefer-const import { Channel, Guild, Member, Message } from "./structures/mod.ts"; +import { Emoji } from "./types/emojis/emoji.ts"; import { Collection } from "./util/collection.ts"; -export const cache: CacheData = { +export const cache = { isReady: false, /** All of the guild objects the bot has access to, mapped by their Ids */ - guilds: new Collection(), + guilds: new Collection(), /** All of the channel objects the bot has access to, mapped by their Ids */ - channels: new Collection(), + channels: new Collection(), /** All of the message objects the bot has cached since the bot acquired `READY` state, mapped by their Ids */ - messages: new Collection(), + messages: new Collection(), /** All of the member objects that have been cached since the bot acquired `READY` state, mapped by their Ids */ - members: new Collection(), - /** All of the unavailable guilds, mapped by their Ids (id, shardId) */ - unavailableGuilds: new Collection(), + members: new Collection(), + /** All of the unavailable guilds, mapped by their Ids (id, timestamp) */ + unavailableGuilds: new Collection(), /** All of the presence update objects received in PRESENCE_UPDATE gateway event, mapped by their user Id */ - presences: new Collection(), - fetchAllMembersProcessingRequests: new Collection(), - executedSlashCommands: new Collection(), + presences: new Collection(), + fetchAllMembersProcessingRequests: new Collection | PromiseLike>, + ) => void>(), + executedSlashCommands: new Collection(), + get emojis() { + const emojis = new Collection(); + + for (const guild of this.guilds.values()) { + for (const emoji of guild.emojis.values()) { + emojis.set(emoji.id, emoji); + } + } + + return emojis; + } }; export let cacheHandlers = { From 5b8f249a1f7d46da981d03ee1b69297e01028ee9 Mon Sep 17 00:00:00 2001 From: Skillz4Killz <23035000+Skillz4Killz@users.noreply.github.com> Date: Sun, 4 Apr 2021 03:13:43 +0000 Subject: [PATCH 2/2] refactor: code cleanup --- src/cache.ts | 68 +++++++++++++++++++++++++++------------------------- 1 file changed, 35 insertions(+), 33 deletions(-) diff --git a/src/cache.ts b/src/cache.ts index 598396cc2..ed6193880 100644 --- a/src/cache.ts +++ b/src/cache.ts @@ -18,21 +18,23 @@ export const cache = { unavailableGuilds: new Collection(), /** All of the presence update objects received in PRESENCE_UPDATE gateway event, mapped by their user Id */ presences: new Collection(), - fetchAllMembersProcessingRequests: new Collection | PromiseLike>, - ) => void>(), + fetchAllMembersProcessingRequests: new Collection< + string, + ( + value: + | Collection + | PromiseLike> + ) => void + >(), executedSlashCommands: new Collection(), get emojis() { - const emojis = new Collection(); - - for (const guild of this.guilds.values()) { - for (const emoji of guild.emojis.values()) { - emojis.set(emoji.id, emoji); - } - } - - return emojis; - } + return new Collection( + this.guilds.reduce( + (a, b) => [...a, ...b.emojis.map((e) => [e.id, e])], + [] as any[] + ) + ); + }, }; export let cacheHandlers = { @@ -76,32 +78,32 @@ export type TableName = function set( table: "guilds", key: string, - value: Guild, + value: Guild ): Promise>; function set( table: "channels", key: string, - value: Channel, + value: Channel ): Promise>; function set( table: "messages", key: string, - value: Message, + value: Message ): Promise>; function set( table: "members", key: string, - value: Member, + value: Member ): Promise>; function set( table: "presences", key: string, - value: PresenceUpdatePayload, + value: PresenceUpdatePayload ): Promise>; function set( table: "unavailableGuilds", key: string, - value: number, + value: number ): Promise>; async function set(table: TableName, key: string, value: any) { return cache[table].set(key, value); @@ -113,11 +115,11 @@ function get(table: "messages", key: string): Promise; function get(table: "members", key: string): Promise; function get( table: "presences", - key: string, + key: string ): Promise; function get( table: "unavailableGuilds", - key: string, + key: string ): Promise; async function get(table: TableName, key: string) { return cache[table].get(key); @@ -125,54 +127,54 @@ async function get(table: TableName, key: string) { function forEach( table: "guilds", - callback: (value: Guild, key: string, map: Map) => unknown, + callback: (value: Guild, key: string, map: Map) => unknown ): void; function forEach( table: "unavailableGuilds", - callback: (value: Guild, key: string, map: Map) => unknown, + callback: (value: Guild, key: string, map: Map) => unknown ): void; function forEach( table: "channels", - callback: (value: Channel, key: string, map: Map) => unknown, + callback: (value: Channel, key: string, map: Map) => unknown ): void; function forEach( table: "messages", - callback: (value: Message, key: string, map: Map) => unknown, + callback: (value: Message, key: string, map: Map) => unknown ): void; function forEach( table: "members", - callback: (value: Member, key: string, map: Map) => unknown, + callback: (value: Member, key: string, map: Map) => unknown ): void; function forEach( table: TableName, - callback: (value: any, key: string, map: Map) => unknown, + callback: (value: any, key: string, map: Map) => unknown ) { return cache[table].forEach(callback); } function filter( table: "guilds", - callback: (value: Guild, key: string) => boolean, + callback: (value: Guild, key: string) => boolean ): Promise>; function filter( table: "unavailableGuilds", - callback: (value: Guild, key: string) => boolean, + callback: (value: Guild, key: string) => boolean ): Promise>; function filter( table: "channels", - callback: (value: Channel, key: string) => boolean, + callback: (value: Channel, key: string) => boolean ): Promise>; function filter( table: "messages", - callback: (value: Message, key: string) => boolean, + callback: (value: Message, key: string) => boolean ): Promise>; function filter( table: "members", - callback: (value: Member, key: string) => boolean, + callback: (value: Member, key: string) => boolean ): Promise>; async function filter( table: TableName, - callback: (value: any, key: string) => boolean, + callback: (value: any, key: string) => boolean ) { return cache[table].filter(callback); }