feat: cache.<resource>.filter

This commit is contained in:
MARCROCK22 2024-04-15 15:08:02 -04:00
parent 38ef5d91cf
commit 961eb04b37
7 changed files with 62 additions and 37 deletions

View File

@ -4,6 +4,9 @@ on:
push: push:
branches: branches:
- main - main
pull_request:
branches:
- main
jobs: jobs:
build: build:

29
src/cache/index.ts vendored
View File

@ -15,15 +15,7 @@ import { Stickers } from './resources/stickers';
import { Threads } from './resources/threads'; import { Threads } from './resources/threads';
import { VoiceStates } from './resources/voice-states'; import { VoiceStates } from './resources/voice-states';
import { import { ChannelType, GatewayIntentBits, type GatewayDispatchPayload } from 'discord-api-types/v10';
ChannelType,
GatewayIntentBits,
type APIEmoji,
type APISticker,
type APIThreadChannel,
type GatewayDispatchPayload,
type GatewayReadyDispatchData,
} from 'discord-api-types/v10';
import type { InternalOptions, UsingClient } from '../commands'; import type { InternalOptions, UsingClient } from '../commands';
import { Overwrites } from './resources/overwrites'; import { Overwrites } from './resources/overwrites';
@ -314,6 +306,7 @@ export class Cache {
case 'emojis': case 'emojis':
case 'overwrites': case 'overwrites':
{ {
if (!this[type]?.filter(data, id, guildId)) continue;
const hashId = this[type]?.hashId(guildId!); const hashId = this[type]?.hashId(guildId!);
if (!hashId) { if (!hashId) {
continue; continue;
@ -331,6 +324,7 @@ export class Cache {
case 'voiceStates': case 'voiceStates':
case 'members': case 'members':
{ {
if (!this[type]?.filter(data, id, guildId)) continue;
const hashId = this[type]?.hashId(guildId!); const hashId = this[type]?.hashId(guildId!);
if (!hashId) { if (!hashId) {
continue; continue;
@ -346,6 +340,7 @@ export class Cache {
case 'users': case 'users':
case 'guilds': case 'guilds':
{ {
if (!this[type]?.filter(data, id)) continue;
const hashId = this[type]?.namespace; const hashId = this[type]?.namespace;
if (!hashId) { if (!hashId) {
continue; continue;
@ -401,6 +396,7 @@ export class Cache {
case 'emojis': case 'emojis':
case 'overwrites': case 'overwrites':
{ {
if (!this[type]?.filter(data, id, guildId)) continue;
const hashId = this[type]?.hashId(guildId!); const hashId = this[type]?.hashId(guildId!);
if (!hashId) { if (!hashId) {
continue; continue;
@ -418,6 +414,7 @@ export class Cache {
case 'voiceStates': case 'voiceStates':
case 'members': case 'members':
{ {
if (!this[type]?.filter(data, id, guildId)) continue;
const hashId = this[type]?.hashId(guildId!); const hashId = this[type]?.hashId(guildId!);
if (!hashId) { if (!hashId) {
continue; continue;
@ -433,6 +430,7 @@ export class Cache {
case 'users': case 'users':
case 'guilds': case 'guilds':
{ {
if (!this[type]?.filter(data, id)) continue;
const hashId = this[type]?.namespace; const hashId = this[type]?.namespace;
if (!hashId) { if (!hashId) {
continue; continue;
@ -456,10 +454,7 @@ export class Cache {
async onPacket(event: GatewayDispatchPayload) { async onPacket(event: GatewayDispatchPayload) {
switch (event.t) { switch (event.t) {
case 'READY': case 'READY':
{ await this.users?.set(event.d.user.id, event.d.user);
const data = event.d as GatewayReadyDispatchData;
await this.users?.set(data.user.id, data.user);
}
break; break;
case 'GUILD_CREATE': case 'GUILD_CREATE':
case 'GUILD_UPDATE': case 'GUILD_UPDATE':
@ -498,14 +493,14 @@ export class Cache {
case 'GUILD_EMOJIS_UPDATE': case 'GUILD_EMOJIS_UPDATE':
await this.emojis?.remove(await this.emojis?.keys(event.d.guild_id), event.d.guild_id); await this.emojis?.remove(await this.emojis?.keys(event.d.guild_id), event.d.guild_id);
await this.emojis?.set( await this.emojis?.set(
(event.d.emojis as APIEmoji[]).map(x => [x.id!, x]), event.d.emojis.map(x => [x.id!, x]),
event.d.guild_id, event.d.guild_id,
); );
break; break;
case 'GUILD_STICKERS_UPDATE': case 'GUILD_STICKERS_UPDATE':
await this.stickers?.remove(await this.stickers?.keys(event.d.guild_id), event.d.guild_id); await this.stickers?.remove(await this.stickers?.keys(event.d.guild_id), event.d.guild_id);
await this.stickers?.set( await this.stickers?.set(
(event.d.stickers as APISticker[]).map(x => [x.id, x]), event.d.stickers.map(x => [x.id, x]),
event.d.guild_id, event.d.guild_id,
); );
break; break;
@ -524,11 +519,11 @@ export class Cache {
case 'THREAD_CREATE': case 'THREAD_CREATE':
case 'THREAD_UPDATE': case 'THREAD_UPDATE':
await this.threads?.set(event.d.id, (event.d as APIThreadChannel).guild_id!, event.d); if (event.d.guild_id) await this.threads?.set(event.d.id, event.d.guild_id, event.d);
break; break;
case 'THREAD_DELETE': case 'THREAD_DELETE':
await this.threads?.remove(event.d.id, (event.d as APIThreadChannel).guild_id!); await this.threads?.remove(event.d.id, event.d.guild_id);
break; break;
case 'USER_UPDATE': case 'USER_UPDATE':

View File

@ -22,8 +22,9 @@ export class BaseResource<T = any> {
this.client = client; this.client = client;
} }
get rest() { //@ts-expect-error
return this.client!.rest; filter(data: any, id: string) {
return true;
} }
get adapter() { get adapter() {
@ -39,7 +40,7 @@ export class BaseResource<T = any> {
setIfNI(intent: keyof typeof GatewayIntentBits, id: string, data: any) { setIfNI(intent: keyof typeof GatewayIntentBits, id: string, data: any) {
if (!this.cache.hasIntent(intent)) { if (!this.cache.hasIntent(intent)) {
return fakePromise(this.set(id, data)).then(() => data); return this.set(id, data);
} }
} }
@ -52,10 +53,12 @@ export class BaseResource<T = any> {
} }
set(id: string, data: any) { set(id: string, data: any) {
if (!this.filter(data, id)) return;
return fakePromise(this.addToRelationship(id)).then(() => this.adapter.set(this.hashId(id), data)); return fakePromise(this.addToRelationship(id)).then(() => this.adapter.set(this.hashId(id), data));
} }
patch<T extends Record<any, any> = Record<any, any>>(id: string, data: T) { patch(id: string, data: any) {
if (!this.filter(data, id)) return;
return fakePromise(this.addToRelationship(id)).then(() => this.adapter.patch(false, this.hashId(id), data)); return fakePromise(this.addToRelationship(id)).then(() => this.adapter.patch(false, this.hashId(id), data));
} }

View File

@ -22,6 +22,11 @@ export class GuildBasedResource<T = any> {
this.client = client; this.client = client;
} }
//@ts-expect-error
filter(data: any, id: string, guild_id: string) {
return true;
}
parse(data: any, id: string, guild_id: string) { parse(data: any, id: string, guild_id: string) {
if (!data.id) data.id = id; if (!data.id) data.id = id;
data.guild_id = guild_id; data.guild_id = guild_id;
@ -41,7 +46,7 @@ export class GuildBasedResource<T = any> {
setIfNI(intent: keyof typeof GatewayIntentBits, id: string, guildId: string, data: any) { setIfNI(intent: keyof typeof GatewayIntentBits, id: string, guildId: string, data: any) {
if (!this.cache.hasIntent(intent)) { if (!this.cache.hasIntent(intent)) {
return fakePromise(this.set(id, guildId, data)).then(() => data); return this.set(id, guildId, data);
} }
} }
@ -56,7 +61,10 @@ export class GuildBasedResource<T = any> {
set(__keys: string, guild: string, data: any): ReturnCache<void>; set(__keys: string, guild: string, data: any): ReturnCache<void>;
set(__keys: [string, any][], guild: string): ReturnCache<void>; set(__keys: [string, any][], guild: string): ReturnCache<void>;
set(__keys: string | [string, any][], guild: string, data?: any): ReturnCache<void> { set(__keys: string | [string, any][], guild: string, data?: any): ReturnCache<void> {
const keys: [string, any][] = Array.isArray(__keys) ? __keys : [[__keys, data]]; const keys = (Array.isArray(__keys) ? __keys : [[__keys, data]]).filter(x => this.filter(x[1], x[1], guild)) as [
string,
any,
][];
return fakePromise( return fakePromise(
this.addToRelationship( this.addToRelationship(
@ -66,7 +74,7 @@ export class GuildBasedResource<T = any> {
).then(() => ).then(() =>
this.adapter.set( this.adapter.set(
keys.map(([key, value]) => { keys.map(([key, value]) => {
return [this.hashGuildId(guild, key), this.parse(value, key, guild)]; return [this.hashGuildId(guild, key), this.parse(value, key, guild)] as const;
}), }),
), ),
) as void; ) as void;
@ -75,7 +83,11 @@ export class GuildBasedResource<T = any> {
patch(__keys: string, guild: string, data: any): ReturnCache<void>; patch(__keys: string, guild: string, data: any): ReturnCache<void>;
patch(__keys: [string, any][], guild: string): ReturnCache<void>; patch(__keys: [string, any][], guild: string): ReturnCache<void>;
patch(__keys: string | [string, any][], guild: string, data?: any): ReturnCache<void> { patch(__keys: string | [string, any][], guild: string, data?: any): ReturnCache<void> {
const keys: [string, any][] = Array.isArray(__keys) ? __keys : [[__keys, data]]; const keys = (Array.isArray(__keys) ? __keys : [[__keys, data]]).filter(x => this.filter(x[1], x[1], guild)) as [
string,
any,
][];
return fakePromise(this.adapter.get(keys.map(([key]) => this.hashGuildId(guild, key)))).then(oldDatas => return fakePromise(this.adapter.get(keys.map(([key]) => this.hashGuildId(guild, key)))).then(oldDatas =>
fakePromise( fakePromise(
this.addToRelationship( this.addToRelationship(

View File

@ -22,6 +22,11 @@ export class GuildRelatedResource<T = any> {
this.client = client; this.client = client;
} }
//@ts-expect-error
filter(data: any, id: string, guild_id?: string) {
return true;
}
parse(data: any, id: string, guild_id: string) { parse(data: any, id: string, guild_id: string) {
if (!data.id) data.id = id; if (!data.id) data.id = id;
data.guild_id = guild_id; data.guild_id = guild_id;
@ -40,7 +45,7 @@ export class GuildRelatedResource<T = any> {
setIfNI(intent: keyof typeof GatewayIntentBits, id: string, guildId: string, data: any) { setIfNI(intent: keyof typeof GatewayIntentBits, id: string, guildId: string, data: any) {
if (!this.cache.hasIntent(intent)) { if (!this.cache.hasIntent(intent)) {
return fakePromise(this.set(id, guildId, data)).then(() => data); return this.set(id, guildId, data);
} }
} }
@ -55,7 +60,11 @@ export class GuildRelatedResource<T = any> {
set(__keys: string, guild: string, data: any): ReturnCache<void>; set(__keys: string, guild: string, data: any): ReturnCache<void>;
set(__keys: [string, any][], guild: string): ReturnCache<void>; set(__keys: [string, any][], guild: string): ReturnCache<void>;
set(__keys: string | [string, any][], guild: string, data?: any): ReturnCache<void> { set(__keys: string | [string, any][], guild: string, data?: any): ReturnCache<void> {
const keys: [string, any][] = Array.isArray(__keys) ? __keys : [[__keys, data]]; const keys = (Array.isArray(__keys) ? __keys : [[__keys, data]]).filter(x => this.filter(x[1], x[1], guild)) as [
string,
any,
][];
return fakePromise( return fakePromise(
this.addToRelationship( this.addToRelationship(
keys.map(x => x[0]), keys.map(x => x[0]),
@ -65,7 +74,7 @@ export class GuildRelatedResource<T = any> {
() => () =>
this.adapter.set( this.adapter.set(
keys.map(([key, value]) => { keys.map(([key, value]) => {
return [this.hashId(key), this.parse(value, key, guild)]; return [this.hashId(key), this.parse(value, key, guild)] as const;
}), }),
) as void, ) as void,
); );
@ -74,7 +83,10 @@ export class GuildRelatedResource<T = any> {
patch(__keys: string, guild?: string, data?: any): ReturnCache<void>; patch(__keys: string, guild?: string, data?: any): ReturnCache<void>;
patch(__keys: [string, any][], guild?: string): ReturnCache<void>; patch(__keys: [string, any][], guild?: string): ReturnCache<void>;
patch(__keys: string | [string, any][], guild?: string, data?: any): ReturnCache<void> { patch(__keys: string | [string, any][], guild?: string, data?: any): ReturnCache<void> {
const keys: [string, any][] = Array.isArray(__keys) ? __keys : [[__keys, data]]; const keys = (Array.isArray(__keys) ? __keys : [[__keys, data]]).filter(x => this.filter(x[1], x[1], guild)) as [
string,
any,
][];
if (guild) { if (guild) {
return fakePromise( return fakePromise(
@ -87,7 +99,7 @@ export class GuildRelatedResource<T = any> {
this.adapter.patch( this.adapter.patch(
false, false,
keys.map(([key, value]) => { keys.map(([key, value]) => {
return [this.hashId(key), this.parse(value, key, guild)]; return [this.hashId(key), this.parse(value, key, guild)] as const;
}), }),
) as void, ) as void,
); );

View File

@ -7,6 +7,11 @@ import { GuildBasedResource } from './default/guild-based';
export class VoiceStates extends GuildBasedResource { export class VoiceStates extends GuildBasedResource {
namespace = 'voice_state'; namespace = 'voice_state';
override parse(data: any, id: string, guild_id: string) {
const { member, ...rest } = super.parse(data, id, guild_id);
return rest;
}
override get(memberId: string, guildId: string): ReturnCache<VoiceState | undefined> { override get(memberId: string, guildId: string): ReturnCache<VoiceState | undefined> {
return fakePromise(super.get(memberId, guildId)).then(state => return fakePromise(super.get(memberId, guildId)).then(state =>
state ? new VoiceState(this.client, state) : undefined, state ? new VoiceState(this.client, state) : undefined,
@ -23,11 +28,6 @@ export class VoiceStates extends GuildBasedResource {
override values(guildId: string): ReturnCache<VoiceState[]> { override values(guildId: string): ReturnCache<VoiceState[]> {
return fakePromise(super.values(guildId)).then(states => states.map(state => new VoiceState(this.client, state))); return fakePromise(super.values(guildId)).then(states => states.map(state => new VoiceState(this.client, state)));
} }
override parse(data: any, id: string, guild_id: string) {
const { member, ...rest } = super.parse(data, id, guild_id);
return rest;
}
} }
export type VoiceStateResource = Omit<GatewayVoiceState, 'member'> & { guild_id: string }; export type VoiceStateResource = Omit<GatewayVoiceState, 'member'> & { guild_id: string };

View File

@ -14,12 +14,12 @@ import type { ObjectToLower } from '..';
import { resolveFiles } from '../../builders'; import { resolveFiles } from '../../builders';
import { import {
AnonymousGuild, AnonymousGuild,
AutoModerationRule,
BaseChannel, BaseChannel,
Guild, Guild,
GuildMember, GuildMember,
Sticker, Sticker,
type CreateStickerBodyRequest, type CreateStickerBodyRequest,
AutoModerationRule,
} from '../../structures'; } from '../../structures';
import channelFrom from '../../structures/channels'; import channelFrom from '../../structures/channels';
import { BaseShorter } from './base'; import { BaseShorter } from './base';
@ -277,7 +277,7 @@ export class GuildShorter extends BaseShorter {
list: async (guildId: string) => { list: async (guildId: string) => {
const stickers = await this.client.proxy.guilds(guildId).stickers.get(); const stickers = await this.client.proxy.guilds(guildId).stickers.get();
await this.client.cache.stickers?.set( await this.client.cache.stickers?.set(
stickers.map(st => [st.id, st]), stickers.map(st => [st.id, st] as any),
guildId, guildId,
); );
return stickers.map(st => new Sticker(this.client, st)); return stickers.map(st => new Sticker(this.client, st));