From 8b2a554c911ba0ec1ec857fe16a3c96a0fac64be Mon Sep 17 00:00:00 2001 From: ITOH Date: Thu, 27 May 2021 16:38:34 +0200 Subject: [PATCH 01/15] add: message sweeper --- src/cache.ts | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/src/cache.ts b/src/cache.ts index 02f30ca09..b93bd7773 100644 --- a/src/cache.ts +++ b/src/cache.ts @@ -14,7 +14,7 @@ export const cache = { /** 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(), + messages: new Collection([], { sweeper: { filter: messageSweeper, interval: 300000 } }), /** 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, timestamp) */ @@ -33,6 +33,16 @@ export const cache = { }, }; +function messageSweeper(message: DiscordenoMessage) { + // DM messages aren't needed + if (!message.guildId) return true; + + // Only delete messages older than 10 minutes + if (Date.now() - message.timestamp > 600000) return true; + + return false; +} + export let cacheHandlers = { /** Deletes all items from the cache */ async clear(table: TableName) { From 5319ed23096ca2568509443df895b5e20f8bd070 Mon Sep 17 00:00:00 2001 From: ITOH Date: Thu, 27 May 2021 16:43:41 +0200 Subject: [PATCH 02/15] add: member sweeper --- src/cache.ts | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/src/cache.ts b/src/cache.ts index b93bd7773..4ff58d907 100644 --- a/src/cache.ts +++ b/src/cache.ts @@ -1,4 +1,5 @@ // deno-lint-ignore-file require-await no-explicit-any prefer-const +import { botId } from "./bot.ts"; import type { DiscordenoChannel } from "./structures/channel.ts"; import type { DiscordenoGuild } from "./structures/guild.ts"; import type { DiscordenoMember } from "./structures/member.ts"; @@ -16,7 +17,7 @@ export const cache = { /** All of the message objects the bot has cached since the bot acquired `READY` state, mapped by their Ids */ messages: new Collection([], { sweeper: { filter: messageSweeper, interval: 300000 } }), /** All of the member objects that have been cached since the bot acquired `READY` state, mapped by their Ids */ - members: new Collection(), + members: new Collection([], { sweeper: { filter: memberSweeper, interval: 1800000 } }), /** All of the unavailable guilds, mapped by their Ids (id, timestamp) */ unavailableGuilds: new Collection(), /** All of the presence update objects received in PRESENCE_UPDATE gateway event, mapped by their user Id */ @@ -43,6 +44,12 @@ function messageSweeper(message: DiscordenoMessage) { return false; } +function memberSweeper(member: DiscordenoMember) { + if (member.id === botId) return false; + + return true; +} + export let cacheHandlers = { /** Deletes all items from the cache */ async clear(table: TableName) { From 31e4929e6214277ae6f451e2edbc425703f2f9c2 Mon Sep 17 00:00:00 2001 From: ITOH Date: Thu, 27 May 2021 16:56:34 +0200 Subject: [PATCH 03/15] add: cachedAt property for member struct useful for sweeping --- src/structures/member.ts | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/structures/member.ts b/src/structures/member.ts index 7166f34ea..127f397a8 100644 --- a/src/structures/member.ts +++ b/src/structures/member.ts @@ -169,6 +169,7 @@ export async function createDiscordenoMember( /** The guild related data mapped by guild id */ guilds: createNewProp(new Collection()), bitfield: createNewProp(bitfield), + cachedAt: createNewProp(Date.now()), }); const cached = await cacheHandlers.get("members", snowflakeToBigint(user.id)); @@ -210,6 +211,8 @@ export interface DiscordenoMember extends Omit; /** Holds all the boolean toggles. */ bitfield: bigint; + /** When the member has been cached the last time. */ + cachedAt: number; // GETTERS /** The avatar url using the default format and size. */ @@ -230,7 +233,9 @@ export interface DiscordenoMember extends Omit & { joinedAt?: number; premiumSince?: number; From 58e20cc9253eb980d2b44d795492bca8ad7e6f33 Mon Sep 17 00:00:00 2001 From: ITOH Date: Thu, 27 May 2021 16:56:45 +0200 Subject: [PATCH 04/15] fix buggs --- src/cache.ts | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/cache.ts b/src/cache.ts index 4ff58d907..b388d29d8 100644 --- a/src/cache.ts +++ b/src/cache.ts @@ -17,7 +17,7 @@ export const cache = { /** All of the message objects the bot has cached since the bot acquired `READY` state, mapped by their Ids */ messages: new Collection([], { sweeper: { filter: messageSweeper, interval: 300000 } }), /** All of the member objects that have been cached since the bot acquired `READY` state, mapped by their Ids */ - members: new Collection([], { sweeper: { filter: memberSweeper, interval: 1800000 } }), + members: new Collection([], { sweeper: { filter: memberSweeper, interval: 300000 } }), /** All of the unavailable guilds, mapped by their Ids (id, timestamp) */ unavailableGuilds: new Collection(), /** All of the presence update objects received in PRESENCE_UPDATE gateway event, mapped by their user Id */ @@ -45,8 +45,12 @@ function messageSweeper(message: DiscordenoMessage) { } function memberSweeper(member: DiscordenoMember) { + // Don't sweep the bot else strange things will happen if (member.id === botId) return false; + // Only sweep members who were not active the last 30 minutes + if (member.cachedAt - Date.now() < 1800000) return false; + return true; } From 355d3cb582501ec33c0efdbcdce012e84a071a50 Mon Sep 17 00:00:00 2001 From: ITOH Date: Thu, 27 May 2021 16:57:34 +0200 Subject: [PATCH 05/15] better explanation --- src/handlers/misc/READY.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/handlers/misc/READY.ts b/src/handlers/misc/READY.ts index 8bee0fbdf..1109fb1d5 100644 --- a/src/handlers/misc/READY.ts +++ b/src/handlers/misc/READY.ts @@ -38,7 +38,7 @@ function checkReady(payload: Ready, shard: DiscordenoShard) { // Check if all guilds were loaded if (!shard.unavailableGuildIds.size) return loaded(shard); - // If the last GUILD_CREATE has been received before 5 seconds if so most likely the remaining guilds are unavailable + // If the last GUILD_CREATE was received 5 seconds ago, the remaining guilds are most likely not available if (shard.lastAvailable + 5000 < Date.now()) { eventHandlers.shardFailedToLoad?.(shard.id, shard.unavailableGuildIds); // Force execute the loaded function to prevent infinite loop From 6a8b183fbff99ead18d5743069fa87ac20ca9047 Mon Sep 17 00:00:00 2001 From: ITOH Date: Thu, 27 May 2021 16:57:45 +0200 Subject: [PATCH 06/15] add: presence sweeper --- src/cache.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/cache.ts b/src/cache.ts index b388d29d8..7d51916e6 100644 --- a/src/cache.ts +++ b/src/cache.ts @@ -21,7 +21,7 @@ export const cache = { /** All of the unavailable guilds, mapped by their Ids (id, timestamp) */ unavailableGuilds: new Collection(), /** All of the presence update objects received in PRESENCE_UPDATE gateway event, mapped by their user Id */ - presences: new Collection(), + presences: new Collection([], { sweeper: { filter: () => true, interval: 300000 } }), fetchAllMembersProcessingRequests: new Collection< string, (value: Collection | PromiseLike>) => void From 7f280bd17be2cd7c80b79a87be5e2f9693ec588b Mon Sep 17 00:00:00 2001 From: ITOH Date: Thu, 27 May 2021 17:17:09 +0200 Subject: [PATCH 07/15] add: guild dispatch --- src/cache.ts | 25 ++++++++- src/util/dispatch_requirements.ts | 93 +++++++++++++++++++++++++++++++ 2 files changed, 117 insertions(+), 1 deletion(-) create mode 100644 src/util/dispatch_requirements.ts diff --git a/src/cache.ts b/src/cache.ts index 7d51916e6..f765c5ec9 100644 --- a/src/cache.ts +++ b/src/cache.ts @@ -11,7 +11,7 @@ import { Collection } from "./util/collection.ts"; 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([], { sweeper: { filter: guildSweeper, interval: 3600000 } }), /** 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 */ @@ -32,6 +32,9 @@ export const cache = { this.guilds.reduce((a, b) => [...a, ...b.emojis.map((e) => [e.id, e])], [] as any[]) ); }, + activeGuildIds: new Set(), + dispatchedGuildIds: new Set(), + dispatchedChannelIds: new Set(), }; function messageSweeper(message: DiscordenoMessage) { @@ -54,6 +57,26 @@ function memberSweeper(member: DiscordenoMember) { return true; } +export function guildSweeper(guild: DiscordenoGuild) { + if (cache.activeGuildIds.has(guild.id)) return false; + + // This is inactive guild. Not a single thing has happened for atleast 30 minutes. + // Not a reaction, not a message, not any event! + cache.guilds.delete(guild.id); + cache.dispatchedGuildIds.add(guild.id); + + // Remove all channel if they were dispatched + cache.channels.forEach((channel) => { + if (!cache.dispatchedGuildIds.has(channel.guildId)) return; + + cache.channels.delete(channel.id); + cache.dispatchedChannelIds.add(channel.id); + }); + + // Reset activity for next interval + cache.activeGuildIds.clear(); +} + export let cacheHandlers = { /** Deletes all items from the cache */ async clear(table: TableName) { diff --git a/src/util/dispatch_requirements.ts b/src/util/dispatch_requirements.ts new file mode 100644 index 000000000..90221e515 --- /dev/null +++ b/src/util/dispatch_requirements.ts @@ -0,0 +1,93 @@ +import { botId } from "../bot.ts"; +import { cache } from "../cache.ts"; +import { getChannels } from "../helpers/channels/get_channels.ts"; +import { getGuild } from "../helpers/guilds/get_guild.ts"; +import { getMember } from "../helpers/members/get_member.ts"; +import { structures } from "../structures/mod.ts"; +import type { DiscordGatewayPayload } from "../types/gateway/gateway_payload.ts"; +import type { Guild } from "../types/guilds/guild.ts"; +import { snowflakeToBigint } from "./bigint.ts"; +import { delay } from "./utils.ts"; + +const processing = new Set(); + +export async function dispatchRequirements(data: DiscordGatewayPayload, shardId: number) { + if (!cache.isReady) return; + + // DELETE MEANS WE DONT NEED TO FETCH. CREATE SHOULD HAVE DATA TO CACHE + if (data.t && ["GUILD_CREATE", "GUILD_DELETE"].includes(data.t)) return; + + const id = snowflakeToBigint( + (data.t && ["GUILD_UPDATE"].includes(data.t) + ? // deno-lint-ignore no-explicit-any + (data.d as any)?.id + : // deno-lint-ignore no-explicit-any + (data.d as any)?.guild_id) ?? "" + ); + + if (!id || cache.activeGuildIds.has(id)) return; + + // If this guild is in cache, it has not been swept and we can cancel + if (cache.guilds.has(id)) { + cache.activeGuildIds.add(id); + return; + } + + if (processing.has(id)) { + console.info(`[DISPATCH] New Guild ID already being processed: ${id} in ${data.t} event`); + + let runs = 0; + do { + await delay(500); + runs++; + } while (processing.has(id) && runs < 40); + + if (!processing.has(id)) return; + + return console.warn(`[DISPATCH] Already processed guild was not successfully fetched: ${id} in ${data.t} event`); + } + + processing.add(id); + + // New guild id has appeared, fetch all relevant data + console.info(`[DISPATCH] New Guild ID has appeared: ${id} in ${data.t} event`); + + const rawGuild = (await getGuild(id, { + counts: true, + addToCache: false, + }).catch(console.info)) as Guild | undefined; + + if (!rawGuild) { + processing.delete(id); + return console.warn(`[DISPATCH] Guild ID ${id} failed to fetch.`); + } + + console.info(`[DISPATCH] Guild ID ${id} has been found. ${rawGuild.name}`); + + const [channels, botMember] = await Promise.all([ + getChannels(id, false), + getMember(id, botId, { force: true }), + ]).catch((error) => { + console.warn(error); + return []; + }); + + if (!botMember || !channels) { + processing.delete(id); + return console.info(`[DISPATCH] Guild ID ${id} Name: ${rawGuild.name} failed. Unable to get botMember or channels`); + } + + const guild = await structures.createDiscordenoGuild(rawGuild, shardId); + + // Add to cache + cache.guilds.set(id, guild); + cache.dispatchedGuildIds.delete(id); + channels.forEach((channel) => { + cache.dispatchedChannelIds.delete(channel.id); + cache.channels.set(channel.id, channel); + }); + + processing.delete(id); + + console.info(`[DISPATCH] Guild ID ${id} Name: ${guild.name} completely loaded.`); +} From 12691f2567acbc19f3ed1fb54bdeacdf8a82d406 Mon Sep 17 00:00:00 2001 From: ITOH Date: Thu, 27 May 2021 17:19:42 +0200 Subject: [PATCH 08/15] fix buggs --- src/bot.ts | 3 ++- src/cache.ts | 5 +++-- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/src/bot.ts b/src/bot.ts index 637df063d..2f023e0a1 100644 --- a/src/bot.ts +++ b/src/bot.ts @@ -5,13 +5,14 @@ import { DiscordGatewayIntents } from "./types/gateway/gateway_intents.ts"; import { snowflakeToBigint } from "./util/bigint.ts"; import { GATEWAY_VERSION } from "./util/constants.ts"; import { ws } from "./ws/ws.ts"; +import { dispatchRequirements } from "./util/dispatch_requirements.ts"; // deno-lint-ignore prefer-const export let secretKey = ""; export let botId = 0n; export let applicationId = 0n; -export let eventHandlers: EventHandlers = {}; +export let eventHandlers: EventHandlers = { dispatchRequirements }; export let proxyWSURL = `wss://gateway.discord.gg`; diff --git a/src/cache.ts b/src/cache.ts index f765c5ec9..ab88f2537 100644 --- a/src/cache.ts +++ b/src/cache.ts @@ -62,7 +62,6 @@ export function guildSweeper(guild: DiscordenoGuild) { // This is inactive guild. Not a single thing has happened for atleast 30 minutes. // Not a reaction, not a message, not any event! - cache.guilds.delete(guild.id); cache.dispatchedGuildIds.add(guild.id); // Remove all channel if they were dispatched @@ -74,7 +73,9 @@ export function guildSweeper(guild: DiscordenoGuild) { }); // Reset activity for next interval - cache.activeGuildIds.clear(); + cache.activeGuildIds.delete(guild.id); + + return true; } export let cacheHandlers = { From c7e3a05816294dba687ee0187c33919e28e4e09d Mon Sep 17 00:00:00 2001 From: ITOH Date: Thu, 27 May 2021 17:20:37 +0200 Subject: [PATCH 09/15] return condition --- src/cache.ts | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/src/cache.ts b/src/cache.ts index ab88f2537..e19c393d5 100644 --- a/src/cache.ts +++ b/src/cache.ts @@ -42,9 +42,7 @@ function messageSweeper(message: DiscordenoMessage) { if (!message.guildId) return true; // Only delete messages older than 10 minutes - if (Date.now() - message.timestamp > 600000) return true; - - return false; + return Date.now() - message.timestamp > 600000; } function memberSweeper(member: DiscordenoMember) { @@ -52,9 +50,7 @@ function memberSweeper(member: DiscordenoMember) { if (member.id === botId) return false; // Only sweep members who were not active the last 30 minutes - if (member.cachedAt - Date.now() < 1800000) return false; - - return true; + return member.cachedAt - Date.now() < 1800000; } export function guildSweeper(guild: DiscordenoGuild) { From cccde10fd2df38fa850fe9f1a567e3679cdb9624 Mon Sep 17 00:00:00 2001 From: ITOH Date: Thu, 27 May 2021 18:04:05 +0200 Subject: [PATCH 10/15] Update collection.ts --- src/util/collection.ts | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/src/util/collection.ts b/src/util/collection.ts index 97173eebb..894542077 100644 --- a/src/util/collection.ts +++ b/src/util/collection.ts @@ -31,6 +31,18 @@ export class Collection extends Map { return clearInterval(this.sweeper?.intervalId); } + changeSweeperInterval(newInterval: number) { + if (!this.sweeper) return; + + this.startSweeper({ filter: this.sweeper.filter, interval: newInterval }); + } + + changeSweeperFilter(newFilter: (value: V, key: K) => boolean | Promise) { + if (!this.sweeper) return; + + this.startSweeper({ filter: newFilter, interval: this.sweeper.interval }); + } + set(key: K, value: V) { // When this collection is maxSizeed make sure we can add first if ((this.maxSize || this.maxSize === 0) && this.size >= this.maxSize) { From 0efa060380cbcd8b1579ca7ff5bb1e0edcc3eb33 Mon Sep 17 00:00:00 2001 From: ITOH Date: Sat, 29 May 2021 15:42:35 +0200 Subject: [PATCH 11/15] better guild & channel dispatch --- src/cache.ts | 19 +++++++------------ 1 file changed, 7 insertions(+), 12 deletions(-) diff --git a/src/cache.ts b/src/cache.ts index e19c393d5..745ee39df 100644 --- a/src/cache.ts +++ b/src/cache.ts @@ -53,23 +53,18 @@ function memberSweeper(member: DiscordenoMember) { return member.cachedAt - Date.now() < 1800000; } -export function guildSweeper(guild: DiscordenoGuild) { - if (cache.activeGuildIds.has(guild.id)) return false; - - // This is inactive guild. Not a single thing has happened for atleast 30 minutes. - // Not a reaction, not a message, not any event! - cache.dispatchedGuildIds.add(guild.id); - - // Remove all channel if they were dispatched - cache.channels.forEach((channel) => { - if (!cache.dispatchedGuildIds.has(channel.guildId)) return; +function guildSweeper(guild: DiscordenoGuild) { + // Reset activity for next interval + if (!cache.activeGuildIds.delete(guild.id)) return false; + guild.channels.forEach((channel) => { cache.channels.delete(channel.id); cache.dispatchedChannelIds.add(channel.id); }); - // Reset activity for next interval - cache.activeGuildIds.delete(guild.id); + // This is inactive guild. Not a single thing has happened for atleast 30 minutes. + // Not a reaction, not a message, not any event! + cache.dispatchedGuildIds.add(guild.id); return true; } From 5e616f14b30487587fc02d95dbad13bea86d5b0c Mon Sep 17 00:00:00 2001 From: ITOH Date: Sat, 29 May 2021 19:29:43 +0200 Subject: [PATCH 12/15] fix: endless tests --- tests/ws/ws_close.ts | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/tests/ws/ws_close.ts b/tests/ws/ws_close.ts index fdc7d2493..df8a8c40d 100644 --- a/tests/ws/ws_close.ts +++ b/tests/ws/ws_close.ts @@ -1,3 +1,4 @@ +import { cache } from "../../src/cache.ts"; import { delay } from "../../src/util/utils.ts"; import { ws } from "../../src/ws/ws.ts"; import { defaultTestOptions } from "./start_bot.ts"; @@ -12,6 +13,13 @@ Deno.test({ }); await delay(3000); + + // clear all the sweeper intervals + for (const c of Object.values(cache)) { + if (!(c instanceof Map)) continue; + + c.stopSweeper(); + } }, ...defaultTestOptions, }); From 15442a54e32168e4e0a35086b82134d274fc9f83 Mon Sep 17 00:00:00 2001 From: ITOH Date: Sat, 29 May 2021 20:40:37 +0200 Subject: [PATCH 13/15] fix more --- tests/local.ts | 6 ++++++ tests/mod.ts | 1 + tests/stop_sweepers.ts | 16 ++++++++++++++++ tests/ws/ws_close.ts | 8 -------- 4 files changed, 23 insertions(+), 8 deletions(-) create mode 100644 tests/stop_sweepers.ts diff --git a/tests/local.ts b/tests/local.ts index 6676768cb..41f77367a 100644 --- a/tests/local.ts +++ b/tests/local.ts @@ -1,3 +1,9 @@ +// THE ORDER OF THE IMPORTS IN THIS FILE MATTER! +// DO NOT MOVE THEM UNLESS YOU KNOW WHAT YOUR DOING! + import "./util/utils.ts"; import "./util/validate_length.ts"; import "./util/loop_object.ts"; + +// Final cleanup +import "./stop_sweepers.ts"; diff --git a/tests/mod.ts b/tests/mod.ts index d804f71f1..e0dba20da 100644 --- a/tests/mod.ts +++ b/tests/mod.ts @@ -74,3 +74,4 @@ import "./discoveries/valid_discovery_term.ts"; // Final cleanup import "./guilds/delete_guild.ts"; import "./ws/ws_close.ts"; +import "./stop_sweepers.ts"; diff --git a/tests/stop_sweepers.ts b/tests/stop_sweepers.ts new file mode 100644 index 000000000..0e7a807f4 --- /dev/null +++ b/tests/stop_sweepers.ts @@ -0,0 +1,16 @@ +import { cache } from "../src/cache.ts"; +import { defaultTestOptions } from "./ws/start_bot.ts"; + +// Exit the Deno process once all tests are done. +Deno.test({ + name: "[chache] Stop all sweepers manually.", + fn() { + // clear all the sweeper intervals + for (const c of Object.values(cache)) { + if (!(c instanceof Map)) continue; + + c.stopSweeper(); + } + }, + ...defaultTestOptions, +}); diff --git a/tests/ws/ws_close.ts b/tests/ws/ws_close.ts index df8a8c40d..fdc7d2493 100644 --- a/tests/ws/ws_close.ts +++ b/tests/ws/ws_close.ts @@ -1,4 +1,3 @@ -import { cache } from "../../src/cache.ts"; import { delay } from "../../src/util/utils.ts"; import { ws } from "../../src/ws/ws.ts"; import { defaultTestOptions } from "./start_bot.ts"; @@ -13,13 +12,6 @@ Deno.test({ }); await delay(3000); - - // clear all the sweeper intervals - for (const c of Object.values(cache)) { - if (!(c instanceof Map)) continue; - - c.stopSweeper(); - } }, ...defaultTestOptions, }); From 63b42112aecdac1e16221794a8660af712eaec68 Mon Sep 17 00:00:00 2001 From: ITOH Date: Sat, 29 May 2021 20:52:27 +0200 Subject: [PATCH 14/15] Update stop_sweepers.ts --- tests/stop_sweepers.ts | 2 -- 1 file changed, 2 deletions(-) diff --git a/tests/stop_sweepers.ts b/tests/stop_sweepers.ts index 0e7a807f4..2ecf30762 100644 --- a/tests/stop_sweepers.ts +++ b/tests/stop_sweepers.ts @@ -1,5 +1,4 @@ import { cache } from "../src/cache.ts"; -import { defaultTestOptions } from "./ws/start_bot.ts"; // Exit the Deno process once all tests are done. Deno.test({ @@ -12,5 +11,4 @@ Deno.test({ c.stopSweeper(); } }, - ...defaultTestOptions, }); From d5e3315b984c99b1ca69b8964b3dd7838bc35d47 Mon Sep 17 00:00:00 2001 From: ITOH Date: Sat, 29 May 2021 21:09:07 +0200 Subject: [PATCH 15/15] some hacky fix --- tests/local.ts | 15 ++++++++++++++- tests/mod.ts | 15 ++++++++++++++- tests/stop_sweepers.ts | 14 -------------- 3 files changed, 28 insertions(+), 16 deletions(-) delete mode 100644 tests/stop_sweepers.ts diff --git a/tests/local.ts b/tests/local.ts index 41f77367a..85e15eef6 100644 --- a/tests/local.ts +++ b/tests/local.ts @@ -6,4 +6,17 @@ import "./util/validate_length.ts"; import "./util/loop_object.ts"; // Final cleanup -import "./stop_sweepers.ts"; + +import { cache } from "../src/cache.ts"; +import { delay } from "../src/util/utils.ts"; +if (import.meta.main) { + // clear all the sweeper intervals + for (const c of Object.values(cache)) { + if (!(c instanceof Map)) continue; + + c.stopSweeper(); + console.log("Cleaned"); + } + + await delay(3000); +} diff --git a/tests/mod.ts b/tests/mod.ts index e0dba20da..13c4dc1a2 100644 --- a/tests/mod.ts +++ b/tests/mod.ts @@ -74,4 +74,17 @@ import "./discoveries/valid_discovery_term.ts"; // Final cleanup import "./guilds/delete_guild.ts"; import "./ws/ws_close.ts"; -import "./stop_sweepers.ts"; + +import { cache } from "../src/cache.ts"; +import { delay } from "../src/util/utils.ts"; +if (import.meta.main) { + // clear all the sweeper intervals + for (const c of Object.values(cache)) { + if (!(c instanceof Map)) continue; + + c.stopSweeper(); + console.log("Cleaned"); + } + + await delay(3000); +} diff --git a/tests/stop_sweepers.ts b/tests/stop_sweepers.ts deleted file mode 100644 index 2ecf30762..000000000 --- a/tests/stop_sweepers.ts +++ /dev/null @@ -1,14 +0,0 @@ -import { cache } from "../src/cache.ts"; - -// Exit the Deno process once all tests are done. -Deno.test({ - name: "[chache] Stop all sweepers manually.", - fn() { - // clear all the sweeper intervals - for (const c of Object.values(cache)) { - if (!(c instanceof Map)) continue; - - c.stopSweeper(); - } - }, -});