From 126d4f77ab6eb758cdb909ef312c33c27bc21018 Mon Sep 17 00:00:00 2001 From: Skillz Date: Sat, 12 Sep 2020 14:37:26 -0400 Subject: [PATCH] refactoring --- mod.ts | 11 +++++++++++ src/controllers/mod.ts | 7 +++++-- src/utils/cache.ts | 39 --------------------------------------- 3 files changed, 16 insertions(+), 41 deletions(-) diff --git a/mod.ts b/mod.ts index 62f4feb00..7aeed6ffc 100644 --- a/mod.ts +++ b/mod.ts @@ -1,9 +1,20 @@ import createClient from "./src/module/client.ts"; +export * from "./src/controllers/mod.ts"; +export * from "./src/controllers/bans.ts"; +export * from "./src/controllers/channels.ts"; +export * from "./src/controllers/guilds.ts"; +export * from "./src/controllers/members.ts"; +export * from "./src/controllers/messages.ts"; +export * from "./src/controllers/misc.ts"; +export * from "./src/controllers/reactions.ts"; +export * from "./src/controllers/roles.ts"; + export * from "./src/module/client.ts"; export * from "./src/module/requestManager.ts"; export * from "./src/module/shardingManager.ts"; +export * from "./src/structures/mod.ts"; export * from "./src/structures/channel.ts"; export * from "./src/structures/guild.ts"; export * from "./src/structures/member.ts"; diff --git a/src/controllers/mod.ts b/src/controllers/mod.ts index 85f9dee42..b11d9f1dd 100644 --- a/src/controllers/mod.ts +++ b/src/controllers/mod.ts @@ -78,8 +78,11 @@ export let controllers = { WEBHOOKS_UPDATE: handleInternalWebhooksUpdate, }; -export type Controllers = typeof controllers +export type Controllers = typeof controllers; export function updateControllers(newControllers: Controllers) { - controllers = newControllers + controllers = { + ...controllers, + ...newControllers, + }; } diff --git a/src/utils/cache.ts b/src/utils/cache.ts index 7dc3fe88b..0797c6769 100644 --- a/src/utils/cache.ts +++ b/src/utils/cache.ts @@ -2,7 +2,6 @@ import { Collection } from "./collection.ts"; import { Message } from "../structures/message.ts"; import { Guild } from "../structures/guild.ts"; import { Channel } from "../structures/channel.ts"; -import { delay } from "https://deno.land/std@0.67.0/async/delay.ts"; import { PresenceUpdatePayload } from "../types/discord.ts"; export interface CacheData { @@ -25,42 +24,4 @@ export const cache: CacheData = { fetchAllMembersProcessingRequests: new Collection(), }; -async function cleanMessageCache() { - // Find all messages for each channel and if more than 100 we need to remove the oldest messages. - const messagesPerChannel = new Map(); - for (const message of cache.messages.values()) { - if ( - // If the guild isn't in cache the message is useless to cache - (message.guildID && !cache.guilds.has(message.guildID)) || - // If the channel isn't in cache the message is useless in cache - !cache.channels.has(message.channelID) - ) { - cache.messages.delete(message.id); - } - - const channel = messagesPerChannel.get(message.channelID); - if (!channel) { - messagesPerChannel.set(message.channelID, [message]); - } else { - channel.push(message); - } - } - - messagesPerChannel.forEach((messages) => { - if (messages.length < 100) return; - - // This channel has more than 100 messages in cache. Delete the oldest - const sortedMessages = messages.sort((a, b) => b.timestamp - a.timestamp); - - sortedMessages.slice(100).forEach((message) => - cache.messages.delete(message.id) - ); - }); - - // Check once per minute - await delay(60000); - cleanMessageCache(); -} - -cleanMessageCache();