mirror of
https://github.com/discordeno/discordeno.git
synced 2026-06-02 17:00:08 +00:00
Merge pull request #744 from discordeno/cache-emojis
feat: add cache.emojis getter.
This commit is contained in:
76
src/cache.ts
76
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<string, Guild>(),
|
||||
/** All of the channel objects the bot has access to, mapped by their Ids */
|
||||
channels: new Collection(),
|
||||
channels: new Collection<string, Channel>(),
|
||||
/** 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<string, Message>(),
|
||||
/** 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<string, Member>(),
|
||||
/** All of the unavailable guilds, mapped by their Ids (id, timestamp) */
|
||||
unavailableGuilds: new Collection<string, number>(),
|
||||
/** 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<string, Presence>(),
|
||||
fetchAllMembersProcessingRequests: new Collection<
|
||||
string,
|
||||
(
|
||||
value:
|
||||
| Collection<string, Member>
|
||||
| PromiseLike<Collection<string, Member>>
|
||||
) => void
|
||||
>(),
|
||||
executedSlashCommands: new Collection<string, string>(),
|
||||
get emojis() {
|
||||
return new Collection<string, Emoji>(
|
||||
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<Collection<string, Guild>>;
|
||||
function set(
|
||||
table: "channels",
|
||||
key: string,
|
||||
value: Channel,
|
||||
value: Channel
|
||||
): Promise<Collection<string, Channel>>;
|
||||
function set(
|
||||
table: "messages",
|
||||
key: string,
|
||||
value: Message,
|
||||
value: Message
|
||||
): Promise<Collection<string, Message>>;
|
||||
function set(
|
||||
table: "members",
|
||||
key: string,
|
||||
value: Member,
|
||||
value: Member
|
||||
): Promise<Collection<string, Member>>;
|
||||
function set(
|
||||
table: "presences",
|
||||
key: string,
|
||||
value: PresenceUpdatePayload,
|
||||
value: PresenceUpdatePayload
|
||||
): Promise<Collection<string, PresenceUpdatePayload>>;
|
||||
function set(
|
||||
table: "unavailableGuilds",
|
||||
key: string,
|
||||
value: number,
|
||||
value: number
|
||||
): Promise<Collection<string, number>>;
|
||||
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<Message | undefined>;
|
||||
function get(table: "members", key: string): Promise<Member | undefined>;
|
||||
function get(
|
||||
table: "presences",
|
||||
key: string,
|
||||
key: string
|
||||
): Promise<PresenceUpdatePayload | undefined>;
|
||||
function get(
|
||||
table: "unavailableGuilds",
|
||||
key: string,
|
||||
key: string
|
||||
): Promise<Guild | undefined>;
|
||||
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<string, Guild>) => unknown,
|
||||
callback: (value: Guild, key: string, map: Map<string, Guild>) => unknown
|
||||
): void;
|
||||
function forEach(
|
||||
table: "unavailableGuilds",
|
||||
callback: (value: Guild, key: string, map: Map<string, Guild>) => unknown,
|
||||
callback: (value: Guild, key: string, map: Map<string, Guild>) => unknown
|
||||
): void;
|
||||
function forEach(
|
||||
table: "channels",
|
||||
callback: (value: Channel, key: string, map: Map<string, Channel>) => unknown,
|
||||
callback: (value: Channel, key: string, map: Map<string, Channel>) => unknown
|
||||
): void;
|
||||
function forEach(
|
||||
table: "messages",
|
||||
callback: (value: Message, key: string, map: Map<string, Message>) => unknown,
|
||||
callback: (value: Message, key: string, map: Map<string, Message>) => unknown
|
||||
): void;
|
||||
function forEach(
|
||||
table: "members",
|
||||
callback: (value: Member, key: string, map: Map<string, Member>) => unknown,
|
||||
callback: (value: Member, key: string, map: Map<string, Member>) => unknown
|
||||
): void;
|
||||
function forEach(
|
||||
table: TableName,
|
||||
callback: (value: any, key: string, map: Map<string, any>) => unknown,
|
||||
callback: (value: any, key: string, map: Map<string, any>) => unknown
|
||||
) {
|
||||
return cache[table].forEach(callback);
|
||||
}
|
||||
|
||||
function filter(
|
||||
table: "guilds",
|
||||
callback: (value: Guild, key: string) => boolean,
|
||||
callback: (value: Guild, key: string) => boolean
|
||||
): Promise<Collection<string, Guild>>;
|
||||
function filter(
|
||||
table: "unavailableGuilds",
|
||||
callback: (value: Guild, key: string) => boolean,
|
||||
callback: (value: Guild, key: string) => boolean
|
||||
): Promise<Collection<string, Guild>>;
|
||||
function filter(
|
||||
table: "channels",
|
||||
callback: (value: Channel, key: string) => boolean,
|
||||
callback: (value: Channel, key: string) => boolean
|
||||
): Promise<Collection<string, Channel>>;
|
||||
function filter(
|
||||
table: "messages",
|
||||
callback: (value: Message, key: string) => boolean,
|
||||
callback: (value: Message, key: string) => boolean
|
||||
): Promise<Collection<string, Message>>;
|
||||
function filter(
|
||||
table: "members",
|
||||
callback: (value: Member, key: string) => boolean,
|
||||
callback: (value: Member, key: string) => boolean
|
||||
): Promise<Collection<string, Member>>;
|
||||
async function filter(
|
||||
table: TableName,
|
||||
callback: (value: any, key: string) => boolean,
|
||||
callback: (value: any, key: string) => boolean
|
||||
) {
|
||||
return cache[table].filter(callback);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user