refactor: replace util/cache with cache

This commit is contained in:
ayntee
2021-03-20 10:06:57 +04:00
parent e1a9565045
commit 5125ffd308
14 changed files with 88 additions and 92 deletions

2
mod.ts
View File

@@ -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";

View File

@@ -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<string, Guild>;
channels: Collection<string, Channel>;
messages: Collection<string, Message>;
members: Collection<string, Member>;
unavailableGuilds: Collection<string, number>;
presences: Collection<string, PresenceUpdatePayload>;
fetchAllMembersProcessingRequests: Collection<
string,
(
value:
| Collection<string, Member>
| PromiseLike<Collection<string, Member>>,
) => void
>;
executedSlashCommands: Collection<string, string>;
}

View File

@@ -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(

View File

@@ -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;

View File

@@ -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";

View File

@@ -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";
/**

View File

@@ -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";

View File

@@ -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";

View File

@@ -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";

View File

@@ -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";

View File

@@ -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";

View File

@@ -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";

View File

@@ -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<string, Guild>;
channels: Collection<string, Channel>;
messages: Collection<string, Message>;
members: Collection<string, Member>;
unavailableGuilds: Collection<string, number>;
presences: Collection<string, PresenceUpdatePayload>;
fetchAllMembersProcessingRequests: Collection<
string,
(
value:
| Collection<string, Member>
| PromiseLike<Collection<string, Member>>,
) => void
>;
executedSlashCommands: Collection<string, string>;
}
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(),
};

View File

@@ -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";