diff --git a/src/cache.ts b/src/cache.ts index e8678402c..ed6193880 100644 --- a/src/cache.ts +++ b/src/cache.ts @@ -1,24 +1,40 @@ // 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< + string, + ( + value: + | Collection + | PromiseLike> + ) => void + >(), + executedSlashCommands: new Collection(), + get emojis() { + return new Collection( + this.guilds.reduce( + (a, b) => [...a, ...b.emojis.map((e) => [e.id, e])], + [] as any[] + ) + ); + }, }; export let cacheHandlers = { @@ -62,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); @@ -99,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); @@ -111,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); }