diff --git a/mod.ts b/mod.ts index 35de54e84..8198f2168 100644 --- a/mod.ts +++ b/mod.ts @@ -10,8 +10,8 @@ export * from "./src/structures/message.ts"; export * from "./src/structures/mod.ts"; export * from "./src/structures/role.ts"; export * from "./src/types/mod.ts"; -export * from "./src/util/cache.ts"; export * from "./src/util/collection.ts"; export * from "./src/util/permissions.ts"; export * from "./src/util/utils.ts"; export * from "./src/ws/mod.ts"; + diff --git a/src/cache.ts b/src/cache.ts index 98c741d7e..bde29c927 100644 --- a/src/cache.ts +++ b/src/cache.ts @@ -1,9 +1,56 @@ // deno-lint-ignore-file require-await no-explicit-any prefer-const -import { PresenceUpdatePayload } from "./types/mod.ts"; -import { cache } from "./util/cache.ts"; -import { Collection } from "./util/collection.ts"; import { Channel, Guild, Member, Message } from "./structures/mod.ts"; +import { PresenceUpdatePayload } from "./types/mod.ts"; +import { Collection } from "./util/collection.ts"; + +export const cache: CacheData = { + 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, shardID) */ + 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(), +}; + +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: string) { + return cache[table].delete(key); + }, + /** Check if something exists in cache with a key */ + async has(table: TableName, key: string) { + 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" @@ -117,32 +164,21 @@ async function filter( return cache[table].filter(callback); } -export let cacheHandlers = { - /** Deletes all items from the cache */ - clear: async function (table: TableName) { - return cache[table].clear(); - }, - /** Deletes 1 item from cache using the key */ - delete: async function (table: TableName, key: string) { - return cache[table].delete(key); - }, - /** Check if something exists in cache with a key */ - has: async function (table: TableName, key: string) { - return cache[table].has(key); - }, - - /** Get the number of key-value pairs */ - size: async (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 interface CacheData { + isReady: boolean; + guilds: Collection; + channels: Collection; + messages: Collection; + members: Collection; + unavailableGuilds: Collection; + presences: Collection; + fetchAllMembersProcessingRequests: Collection< + string, + ( + value: + | Collection + | PromiseLike>, + ) => void + >; + executedSlashCommands: Collection; +} \ No newline at end of file diff --git a/src/handlers/guilds/GUILD_CREATE.ts b/src/handlers/guilds/GUILD_CREATE.ts index c43ccf9db..51652a1ad 100644 --- a/src/handlers/guilds/GUILD_CREATE.ts +++ b/src/handlers/guilds/GUILD_CREATE.ts @@ -1,8 +1,7 @@ import { eventHandlers } from "../../bot.ts"; -import { cacheHandlers } from "../../cache.ts"; +import { cache, cacheHandlers } from "../../cache.ts"; import { structures } from "../../structures/mod.ts"; import { CreateGuildPayload, DiscordPayload } from "../../types/mod.ts"; -import { cache } from "../../util/cache.ts"; import { basicShards } from "../../ws/shard.ts"; export async function handleGuildCreate( diff --git a/src/handlers/members/GUILD_MEMBERS_CHUNK.ts b/src/handlers/members/GUILD_MEMBERS_CHUNK.ts index bb2bf8702..cf13d3143 100644 --- a/src/handlers/members/GUILD_MEMBERS_CHUNK.ts +++ b/src/handlers/members/GUILD_MEMBERS_CHUNK.ts @@ -1,8 +1,7 @@ -import { DiscordPayload, GuildMemberChunkPayload } from "../../types/mod.ts"; -import { cache } from "../../util/cache.ts"; -import { Collection } from "../../util/collection.ts"; +import { cache, cacheHandlers } from "../../cache.ts"; import { structures } from "../../structures/mod.ts"; -import { cacheHandlers } from "../../cache.ts"; +import { DiscordPayload, GuildMemberChunkPayload } from "../../types/mod.ts"; +import { Collection } from "../../util/collection.ts"; export async function handleGuildMembersChunk(data: DiscordPayload) { const payload = data.d as GuildMemberChunkPayload; diff --git a/src/handlers/misc/READY.ts b/src/handlers/misc/READY.ts index 80b946646..904beb7d9 100644 --- a/src/handlers/misc/READY.ts +++ b/src/handlers/misc/READY.ts @@ -2,13 +2,12 @@ import { eventHandlers, lastShardID, setApplicationID, - setBotID, + setBotID } from "../../bot.ts"; -import { cacheHandlers } from "../../cache.ts"; +import { cache, cacheHandlers } from "../../cache.ts"; import { initialMemberLoadQueue } from "../../structures/guild.ts"; import { structures } from "../../structures/mod.ts"; import { DiscordPayload, ReadyPayload } from "../../types/discord.ts"; -import { cache } from "../../util/cache.ts"; import { delay } from "../../util/utils.ts"; import { allowNextShard, basicShards } from "../../ws/mod.ts"; diff --git a/src/helpers/commands/send_interaction_response.ts b/src/helpers/commands/send_interaction_response.ts index 35567dced..a7f561f2c 100644 --- a/src/helpers/commands/send_interaction_response.ts +++ b/src/helpers/commands/send_interaction_response.ts @@ -1,7 +1,7 @@ import { applicationID } from "../../bot.ts"; +import { cache } from "../../cache.ts"; import { RequestManager } from "../../rest/request_manager.ts"; import { SlashCommandResponseOptions } from "../../types/mod.ts"; -import { cache } from "../../util/cache.ts"; import { endpoints } from "../../util/constants.ts"; /** diff --git a/src/structures/channel.ts b/src/structures/channel.ts index ce676a1d5..d9c2ea5fe 100644 --- a/src/structures/channel.ts +++ b/src/structures/channel.ts @@ -1,3 +1,4 @@ +import { cache } from "../cache.ts"; import { channelOverwriteHasPermission } from "../helpers/channels/channel_overwrite_has_permission.ts"; import { deleteChannel } from "../helpers/channels/delete_channel.ts"; import { deleteChannelOverwrite } from "../helpers/channels/delete_channel_overwrite.ts"; @@ -12,9 +13,8 @@ import { MessageContent, Overwrite, Permission, - RawOverwrite, + RawOverwrite } from "../types/mod.ts"; -import { cache } from "../util/cache.ts"; import { Collection } from "../util/collection.ts"; import { createNewProp } from "../util/utils.ts"; import { CleanVoiceState, Guild } from "./guild.ts"; diff --git a/src/structures/guild.ts b/src/structures/guild.ts index 1d6c7b4ca..1358b6ab9 100644 --- a/src/structures/guild.ts +++ b/src/structures/guild.ts @@ -1,5 +1,5 @@ import { botID } from "../bot.ts"; -import { cacheHandlers } from "../cache.ts"; +import { cache, cacheHandlers } from "../cache.ts"; import { deleteServer } from "../helpers/guilds/delete_server.ts"; import { editGuild } from "../helpers/guilds/edit_guild.ts"; import { getAuditLogs } from "../helpers/guilds/get_audit_logs.ts"; @@ -23,9 +23,8 @@ import { ImageSize, MemberCreatePayload, Presence, - VoiceState, + VoiceState } from "../types/mod.ts"; -import { cache } from "../util/cache.ts"; import { Collection } from "../util/collection.ts"; import { createNewProp } from "../util/utils.ts"; import { Member } from "./member.ts"; diff --git a/src/structures/member.ts b/src/structures/member.ts index 2b2da411e..c497779e7 100644 --- a/src/structures/member.ts +++ b/src/structures/member.ts @@ -1,4 +1,4 @@ -import { cacheHandlers } from "../cache.ts"; +import { cache, cacheHandlers } from "../cache.ts"; import { banMember } from "../helpers/members/ban_member.ts"; import { editMember } from "../helpers/members/edit_member.ts"; import { kickMember } from "../helpers/members/kick_member.ts"; @@ -13,9 +13,8 @@ import { ImageFormats, ImageSize, MemberCreatePayload, - MessageContent, + MessageContent } from "../types/mod.ts"; -import { cache } from "../util/cache.ts"; import { Collection } from "../util/collection.ts"; import { createNewProp } from "../util/utils.ts"; import { Guild } from "./guild.ts"; diff --git a/src/structures/message.ts b/src/structures/message.ts index 9b8b2c203..7f3be9439 100644 --- a/src/structures/message.ts +++ b/src/structures/message.ts @@ -1,4 +1,4 @@ -import { cacheHandlers } from "../cache.ts"; +import { cache, cacheHandlers } from "../cache.ts"; import { sendDirectMessage } from "../helpers/members/send_direct_message.ts"; import { addReaction } from "../helpers/messages/add_reaction.ts"; import { addReactions } from "../helpers/messages/add_reactions.ts"; @@ -20,9 +20,8 @@ import { MessageCreateOptions, MessageSticker, Reaction, - UserPayload, + UserPayload } from "../types/mod.ts"; -import { cache } from "../util/cache.ts"; import { createNewProp } from "../util/utils.ts"; import { Channel } from "./channel.ts"; import { Guild } from "./guild.ts"; diff --git a/src/structures/role.ts b/src/structures/role.ts index efad15416..584c01c94 100644 --- a/src/structures/role.ts +++ b/src/structures/role.ts @@ -1,7 +1,7 @@ +import { cache } from "../cache.ts"; import { deleteRole } from "../helpers/roles/delete_role.ts"; import { editRole } from "../helpers/roles/edit_role.ts"; import { CreateRoleOptions, RoleData } from "../types/mod.ts"; -import { cache } from "../util/cache.ts"; import { Collection } from "../util/collection.ts"; import { createNewProp } from "../util/utils.ts"; import { Guild } from "./guild.ts"; diff --git a/src/structures/template.ts b/src/structures/template.ts index dafb1e5e7..ff10f8bad 100644 --- a/src/structures/template.ts +++ b/src/structures/template.ts @@ -1,5 +1,5 @@ +import { cache } from "../cache.ts"; import { GuildTemplate, UserPayload } from "../types/mod.ts"; -import { cache } from "../util/cache.ts"; import { createNewProp } from "../util/utils.ts"; import { Guild } from "./guild.ts"; diff --git a/src/util/cache.ts b/src/util/cache.ts deleted file mode 100644 index 5a91ff01f..000000000 --- a/src/util/cache.ts +++ /dev/null @@ -1,34 +0,0 @@ -import { Channel, Guild, Member, Message } from "../structures/mod.ts"; -import { PresenceUpdatePayload } from "../types/mod.ts"; -import { Collection } from "./collection.ts"; - -export interface CacheData { - isReady: boolean; - guilds: Collection; - channels: Collection; - messages: Collection; - members: Collection; - unavailableGuilds: Collection; - presences: Collection; - fetchAllMembersProcessingRequests: Collection< - string, - ( - value: - | Collection - | PromiseLike>, - ) => void - >; - executedSlashCommands: Collection; -} - -export const cache: CacheData = { - isReady: false, - guilds: new Collection(), - channels: new Collection(), - messages: new Collection(), - members: new Collection(), - unavailableGuilds: new Collection(), - presences: new Collection(), - fetchAllMembersProcessingRequests: new Collection(), - executedSlashCommands: new Collection(), -}; diff --git a/src/ws/shard_manager.ts b/src/ws/shard_manager.ts index e4e7c8b02..0b682b3be 100644 --- a/src/ws/shard_manager.ts +++ b/src/ws/shard_manager.ts @@ -1,4 +1,5 @@ import { eventHandlers } from "../bot.ts"; +import { cache } from "../cache.ts"; import { handlers } from "../handlers/mod.ts"; import { Member } from "../structures/mod.ts"; import { @@ -6,9 +7,8 @@ import { DiscordIdentify, DiscordPayload, FetchMembersOptions, - GatewayOpcode, + GatewayOpcode } from "../types/mod.ts"; -import { cache } from "../util/cache.ts"; import { Collection } from "../util/collection.ts"; import { delay } from "../util/utils.ts"; import { createShard, requestGuildMembers } from "./mod.ts";