From 7591c114460e1e48ee4abd937aa2696fe28b553f Mon Sep 17 00:00:00 2001 From: MARCROCK22 Date: Sat, 26 Oct 2024 16:17:43 +0000 Subject: [PATCH] feat: cacheResource.flush --- src/api/shared.ts | 2 +- src/builders/Attachment.ts | 5 +++-- src/cache/resources/default/base.ts | 8 ++++++++ src/cache/resources/default/guild-based.ts | 13 +++++++++++++ src/cache/resources/default/guild-related.ts | 8 ++++++++ .../_applicationCommands/permissions.ts | 1 - src/types/utils/index.ts | 4 ---- 7 files changed, 33 insertions(+), 8 deletions(-) diff --git a/src/api/shared.ts b/src/api/shared.ts index 54e07b3..8285315 100644 --- a/src/api/shared.ts +++ b/src/api/shared.ts @@ -22,7 +22,7 @@ export interface ApiHandlerInternalOptions extends MakeRequired { return this.adapter.removeToRelationship(this.namespace, id); } + flush() { + return fakePromise(this.adapter.keys(this.namespace)).then(keys => { + return fakePromise(this.adapter.bulkRemove(keys)).then(() => { + return this.adapter.removeRelationship(this.namespace); + }); + }); + } + hashId(id: string) { return id.startsWith(this.namespace) ? id : `${this.namespace}.${id}`; } diff --git a/src/cache/resources/default/guild-based.ts b/src/cache/resources/default/guild-based.ts index 0fbebe5..3d4dfac 100644 --- a/src/cache/resources/default/guild-based.ts +++ b/src/cache/resources/default/guild-based.ts @@ -149,4 +149,17 @@ export class GuildBasedResource { hashGuildId(guild: string, id: string) { return id.startsWith(this.namespace) ? id : `${this.namespace}.${guild}.${id}`; } + + flush(guild: '*' | (string & {}) = '*') { + return fakePromise(this.keys(guild)).then(keys => { + return fakePromise(this.adapter.bulkRemove(keys)).then(() => { + const maybePromises: (unknown | Promise)[] = []; + for (const i of keys) { + const guildId = i.split('.').at(-2)!; + maybePromises.push(this.removeRelationship(guildId)); + } + return this.adapter.isAsync ? Promise.all(maybePromises) : maybePromises; + }); + }); + } } diff --git a/src/cache/resources/default/guild-related.ts b/src/cache/resources/default/guild-related.ts index f29f0e6..2cb5069 100644 --- a/src/cache/resources/default/guild-related.ts +++ b/src/cache/resources/default/guild-related.ts @@ -164,4 +164,12 @@ export class GuildRelatedResource { hashId(id: string) { return id.startsWith(this.namespace) ? id : `${this.namespace}.${id}`; } + + flush(guild: string) { + return fakePromise(this.keys(guild)).then(keys => { + return fakePromise(this.adapter.bulkRemove(keys)).then(() => { + return this.removeRelationship(guild); + }); + }); + } } diff --git a/src/types/payloads/_interactions/_applicationCommands/permissions.ts b/src/types/payloads/_interactions/_applicationCommands/permissions.ts index eac4d58..6c37566 100644 --- a/src/types/payloads/_interactions/_applicationCommands/permissions.ts +++ b/src/types/payloads/_interactions/_applicationCommands/permissions.ts @@ -53,7 +53,6 @@ export enum ApplicationCommandPermissionType { * https://discord.com/developers/docs/interactions/application-commands#application-command-permissions-object-application-command-permissions-constants */ export const APIApplicationCommandPermissionsConstant = { - // eslint-disable-next-line unicorn/prefer-native-coercion-functions Everyone: (guildId: bigint | string): Snowflake => String(guildId), AllChannels: (guildId: bigint | string): Snowflake => String(BigInt(guildId) - 1n), }; diff --git a/src/types/utils/index.ts b/src/types/utils/index.ts index 07bd0f2..8c2145f 100644 --- a/src/types/utils/index.ts +++ b/src/types/utils/index.ts @@ -98,7 +98,6 @@ export const FormattingPatterns = { * The `fullName` (possibly including `name`, `subcommandOrGroup` and `subcommand`) and `id` group properties are present on the `exec` result of this expression */ SlashCommand: - // eslint-disable-next-line unicorn/no-unsafe-regex /<\/(?(?[-_\p{Letter}\p{Number}\p{sc=Deva}\p{sc=Thai}]{1,32})(?: (?[-_\p{Letter}\p{Number}\p{sc=Deva}\p{sc=Thai}]{1,32}))?(?: (?[-_\p{Letter}\p{Number}\p{sc=Deva}\p{sc=Thai}]{1,32}))?):(?\d{17,20})>/u, /** * Regular expression for matching a custom emoji, either static or animated @@ -123,7 +122,6 @@ export const FormattingPatterns = { * * The `timestamp` and `style` group properties are present on the `exec` result of this expression */ - // eslint-disable-next-line prefer-named-capture-group Timestamp: /-?\d{1,13})(:(?