refactoring

This commit is contained in:
Skillz
2020-09-12 14:37:26 -04:00
parent 02badb1c97
commit 126d4f77ab
3 changed files with 16 additions and 41 deletions
+11
View File
@@ -1,9 +1,20 @@
import createClient from "./src/module/client.ts"; 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/client.ts";
export * from "./src/module/requestManager.ts"; export * from "./src/module/requestManager.ts";
export * from "./src/module/shardingManager.ts"; export * from "./src/module/shardingManager.ts";
export * from "./src/structures/mod.ts";
export * from "./src/structures/channel.ts"; export * from "./src/structures/channel.ts";
export * from "./src/structures/guild.ts"; export * from "./src/structures/guild.ts";
export * from "./src/structures/member.ts"; export * from "./src/structures/member.ts";
+5 -2
View File
@@ -78,8 +78,11 @@ export let controllers = {
WEBHOOKS_UPDATE: handleInternalWebhooksUpdate, WEBHOOKS_UPDATE: handleInternalWebhooksUpdate,
}; };
export type Controllers = typeof controllers export type Controllers = typeof controllers;
export function updateControllers(newControllers: Controllers) { export function updateControllers(newControllers: Controllers) {
controllers = newControllers controllers = {
...controllers,
...newControllers,
};
} }
-39
View File
@@ -2,7 +2,6 @@ import { Collection } from "./collection.ts";
import { Message } from "../structures/message.ts"; import { Message } from "../structures/message.ts";
import { Guild } from "../structures/guild.ts"; import { Guild } from "../structures/guild.ts";
import { Channel } from "../structures/channel.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"; import { PresenceUpdatePayload } from "../types/discord.ts";
export interface CacheData { export interface CacheData {
@@ -25,42 +24,4 @@ export const cache: CacheData = {
fetchAllMembersProcessingRequests: new Collection<string, Function>(), fetchAllMembersProcessingRequests: new Collection<string, Function>(),
}; };
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<string, Message[]>();
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();