feat: cacheResource.flush

This commit is contained in:
MARCROCK22 2024-10-26 16:17:43 +00:00
parent 4f8c1d09c7
commit 7591c11446
7 changed files with 33 additions and 8 deletions

View File

@ -22,7 +22,7 @@ export interface ApiHandlerInternalOptions extends MakeRequired<ApiHandlerOption
export interface RawFile {
contentType?: string;
data: ArrayBuffer | Buffer | Uint8Array | boolean | number | string;
data: ArrayBuffer | Buffer | Uint8Array | Uint8ClampedArray | boolean | number | string;
key?: string;
filename: string;
}

View File

@ -2,13 +2,14 @@ import { randomBytes } from 'node:crypto';
import { promises } from 'node:fs';
import path from 'node:path';
import type { RawFile, UsingClient } from '..';
import { isBufferLike } from '../api/utils/utils';
import type { ImageResolvable, ObjectToLower } from '../common';
import { Base } from '../structures/extra/Base';
import type { APIAttachment, RESTAPIAttachment } from '../types';
export interface AttachmentResolvableMap {
url: string;
buffer: Buffer | ArrayBuffer;
buffer: Buffer | ArrayBuffer | Uint8Array | Uint8ClampedArray;
path: string;
}
export type AttachmentResolvable =
@ -206,7 +207,7 @@ export async function resolveAttachmentData(
return { data: await promises.readFile(file) };
}
case 'buffer': {
if (Buffer.isBuffer(data)) return { data };
if (isBufferLike(data)) return { data };
// @ts-expect-error
if (typeof data[Symbol.asyncIterator] === 'function') {
const buffers: Buffer[] = [];

View File

@ -93,6 +93,14 @@ export class BaseResource<T = any, S = any> {
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}`;
}

View File

@ -149,4 +149,17 @@ export class GuildBasedResource<T = any, S = any> {
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<unknown>)[] = [];
for (const i of keys) {
const guildId = i.split('.').at(-2)!;
maybePromises.push(this.removeRelationship(guildId));
}
return this.adapter.isAsync ? Promise.all(maybePromises) : maybePromises;
});
});
}
}

View File

@ -164,4 +164,12 @@ export class GuildRelatedResource<T = any, S = any> {
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);
});
});
}
}

View File

@ -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),
};

View File

@ -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
/<\/(?<fullName>(?<name>[-_\p{Letter}\p{Number}\p{sc=Deva}\p{sc=Thai}]{1,32})(?: (?<subcommandOrGroup>[-_\p{Letter}\p{Number}\p{sc=Deva}\p{sc=Thai}]{1,32}))?(?: (?<subcommand>[-_\p{Letter}\p{Number}\p{sc=Deva}\p{sc=Thai}]{1,32}))?):(?<id>\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: /<t:(?<timestamp>-?\d{1,13})(:(?<style>[DFRTdft]))?>/,
/**
* Regular expression for matching strictly default styled timestamps
@ -415,7 +413,6 @@ export const PermissionFlagsBits = {
/**
* Allows kicking members
*/
// eslint-disable-next-line sonarjs/no-identical-expressions
KickMembers: 1n << 1n,
/**
* Allows banning members
@ -777,7 +774,6 @@ export enum ChannelType {
*
* @deprecated This is the old name for {@apilink ChannelType#AnnouncementThread}
*/
// eslint-disable-next-line @typescript-eslint/no-duplicate-enum-values
GuildNewsThread = 10,
/**
* A temporary sub-channel within a Guild Text channel