// Loaded from https://deno.land/x/discordeno@11.0.0-rc.2/src/cache.ts // deno-lint-ignore-file require-await no-explicit-any prefer-const import type { DiscordenoChannel } from "./structures/channel.ts"; import type { DiscordenoGuild } from "./structures/guild.ts"; import type { DiscordenoMember } from "./structures/member.ts"; import type { DiscordenoMessage } from "./structures/message.ts"; import type { PresenceUpdate } from "./types/activity/presence_update.ts"; import type { Emoji } from "./types/emojis/emoji.ts"; import { Collection } from "./util/collection.ts"; export const cache = { isReady: false, /** All of the guild objects the bot has access to, mapped by their Ids */ guilds: new Collection(), /** All of the channel objects the bot has access to, mapped by their Ids */ 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(), /** 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, 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< string, (value: Collection | PromiseLike>) => void >(), executedSlashCommands: new Set(), get emojis() { return new Collection( this.guilds.reduce((a, b) => [...a, ...b.emojis.map((e) => [e.id, e])], [] as any[]) ); }, }; export let cacheHandlers = { /** Deletes all items from the cache */ async clear(table: TableName) { return cache[table].clear(); }, /** Deletes 1 item from cache using the key */ async delete(table: TableName, key: bigint) { return cache[table].delete(key); }, /** Check if something exists in cache with a key */ async has(table: TableName, key: bigint) { return cache[table].has(key); }, /** Get the number of key-value pairs */ async size(table: TableName) { return cache[table].size; }, // Done differently to have overloads /** Add a key value pair to the cache */ set, /** Get the value from the cache using its key */ get, /** Run a function on all items in this cache */ forEach, /** Allows you to filter our all items in this cache. */ filter, }; export type TableName = "guilds" | "unavailableGuilds" | "channels" | "messages" | "members" | "presences"; function set(table: "guilds", key: bigint, value: DiscordenoGuild): Promise>; function set(table: "channels", key: bigint, value: DiscordenoChannel): Promise>; function set(table: "messages", key: bigint, value: DiscordenoMessage): Promise>; function set(table: "members", key: bigint, value: DiscordenoMember): Promise>; function set(table: "presences", key: bigint, value: PresenceUpdate): Promise>; function set(table: "unavailableGuilds", key: bigint, value: number): Promise>; async function set(table: TableName, key: bigint, value: any) { return cache[table].set(key, value); } function get(table: "guilds", key: bigint): Promise; function get(table: "channels", key: bigint): Promise; function get(table: "messages", key: bigint): Promise; function get(table: "members", key: bigint): Promise; function get(table: "presences", key: bigint): Promise; function get(table: "unavailableGuilds", key: bigint): Promise; async function get(table: TableName, key: bigint) { return cache[table].get(key); } function forEach( table: "guilds", callback: (value: DiscordenoGuild, key: bigint, map: Map) => unknown ): void; function forEach( table: "unavailableGuilds", callback: (value: number, key: bigint, map: Map) => unknown ): void; function forEach( table: "channels", callback: (value: DiscordenoChannel, key: bigint, map: Map) => unknown ): void; function forEach( table: "messages", callback: (value: DiscordenoMessage, key: bigint, map: Map) => unknown ): void; function forEach( table: "members", callback: (value: DiscordenoMember, key: bigint, map: Map) => unknown ): void; function forEach(table: TableName, callback: (value: any, key: bigint, map: Map) => unknown) { return cache[table].forEach(callback); } function filter( table: "guilds", callback: (value: DiscordenoGuild, key: bigint) => boolean ): Promise>; function filter( table: "unavailableGuilds", callback: (value: number, key: bigint) => boolean ): Promise>; function filter( table: "channels", callback: (value: DiscordenoChannel, key: bigint) => boolean ): Promise>; function filter( table: "messages", callback: (value: DiscordenoMessage, key: bigint) => boolean ): Promise>; function filter( table: "members", callback: (value: DiscordenoMember, key: bigint) => boolean ): Promise>; async function filter(table: TableName, callback: (value: any, key: bigint) => boolean) { return cache[table].filter(callback); }