Skip to content

Commit 961eb04

Browse files
committed
feat: cache.<resource>.filter
1 parent 38ef5d9 commit 961eb04

File tree

7 files changed

+62
-37
lines changed

7 files changed

+62
-37
lines changed

.github/workflows/transpile.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@ on:
44
push:
55
branches:
66
- main
7+
pull_request:
8+
branches:
9+
- main
710

811
jobs:
912
build:

src/cache/index.ts

Lines changed: 12 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,7 @@ import { Stickers } from './resources/stickers';
1515
import { Threads } from './resources/threads';
1616
import { VoiceStates } from './resources/voice-states';
1717

18-
import {
19-
ChannelType,
20-
GatewayIntentBits,
21-
type APIEmoji,
22-
type APISticker,
23-
type APIThreadChannel,
24-
type GatewayDispatchPayload,
25-
type GatewayReadyDispatchData,
26-
} from 'discord-api-types/v10';
18+
import { ChannelType, GatewayIntentBits, type GatewayDispatchPayload } from 'discord-api-types/v10';
2719
import type { InternalOptions, UsingClient } from '../commands';
2820
import { Overwrites } from './resources/overwrites';
2921

@@ -314,6 +306,7 @@ export class Cache {
314306
case 'emojis':
315307
case 'overwrites':
316308
{
309+
if (!this[type]?.filter(data, id, guildId)) continue;
317310
const hashId = this[type]?.hashId(guildId!);
318311
if (!hashId) {
319312
continue;
@@ -331,6 +324,7 @@ export class Cache {
331324
case 'voiceStates':
332325
case 'members':
333326
{
327+
if (!this[type]?.filter(data, id, guildId)) continue;
334328
const hashId = this[type]?.hashId(guildId!);
335329
if (!hashId) {
336330
continue;
@@ -346,6 +340,7 @@ export class Cache {
346340
case 'users':
347341
case 'guilds':
348342
{
343+
if (!this[type]?.filter(data, id)) continue;
349344
const hashId = this[type]?.namespace;
350345
if (!hashId) {
351346
continue;
@@ -401,6 +396,7 @@ export class Cache {
401396
case 'emojis':
402397
case 'overwrites':
403398
{
399+
if (!this[type]?.filter(data, id, guildId)) continue;
404400
const hashId = this[type]?.hashId(guildId!);
405401
if (!hashId) {
406402
continue;
@@ -418,6 +414,7 @@ export class Cache {
418414
case 'voiceStates':
419415
case 'members':
420416
{
417+
if (!this[type]?.filter(data, id, guildId)) continue;
421418
const hashId = this[type]?.hashId(guildId!);
422419
if (!hashId) {
423420
continue;
@@ -433,6 +430,7 @@ export class Cache {
433430
case 'users':
434431
case 'guilds':
435432
{
433+
if (!this[type]?.filter(data, id)) continue;
436434
const hashId = this[type]?.namespace;
437435
if (!hashId) {
438436
continue;
@@ -456,10 +454,7 @@ export class Cache {
456454
async onPacket(event: GatewayDispatchPayload) {
457455
switch (event.t) {
458456
case 'READY':
459-
{
460-
const data = event.d as GatewayReadyDispatchData;
461-
await this.users?.set(data.user.id, data.user);
462-
}
457+
await this.users?.set(event.d.user.id, event.d.user);
463458
break;
464459
case 'GUILD_CREATE':
465460
case 'GUILD_UPDATE':
@@ -498,14 +493,14 @@ export class Cache {
498493
case 'GUILD_EMOJIS_UPDATE':
499494
await this.emojis?.remove(await this.emojis?.keys(event.d.guild_id), event.d.guild_id);
500495
await this.emojis?.set(
501-
(event.d.emojis as APIEmoji[]).map(x => [x.id!, x]),
496+
event.d.emojis.map(x => [x.id!, x]),
502497
event.d.guild_id,
503498
);
504499
break;
505500
case 'GUILD_STICKERS_UPDATE':
506501
await this.stickers?.remove(await this.stickers?.keys(event.d.guild_id), event.d.guild_id);
507502
await this.stickers?.set(
508-
(event.d.stickers as APISticker[]).map(x => [x.id, x]),
503+
event.d.stickers.map(x => [x.id, x]),
509504
event.d.guild_id,
510505
);
511506
break;
@@ -524,11 +519,11 @@ export class Cache {
524519

525520
case 'THREAD_CREATE':
526521
case 'THREAD_UPDATE':
527-
await this.threads?.set(event.d.id, (event.d as APIThreadChannel).guild_id!, event.d);
522+
if (event.d.guild_id) await this.threads?.set(event.d.id, event.d.guild_id, event.d);
528523
break;
529524

530525
case 'THREAD_DELETE':
531-
await this.threads?.remove(event.d.id, (event.d as APIThreadChannel).guild_id!);
526+
await this.threads?.remove(event.d.id, event.d.guild_id);
532527
break;
533528

534529
case 'USER_UPDATE':

src/cache/resources/default/base.ts

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,9 @@ export class BaseResource<T = any> {
2222
this.client = client;
2323
}
2424

25-
get rest() {
26-
return this.client!.rest;
25+
//@ts-expect-error
26+
filter(data: any, id: string) {
27+
return true;
2728
}
2829

2930
get adapter() {
@@ -39,7 +40,7 @@ export class BaseResource<T = any> {
3940

4041
setIfNI(intent: keyof typeof GatewayIntentBits, id: string, data: any) {
4142
if (!this.cache.hasIntent(intent)) {
42-
return fakePromise(this.set(id, data)).then(() => data);
43+
return this.set(id, data);
4344
}
4445
}
4546

@@ -52,10 +53,12 @@ export class BaseResource<T = any> {
5253
}
5354

5455
set(id: string, data: any) {
56+
if (!this.filter(data, id)) return;
5557
return fakePromise(this.addToRelationship(id)).then(() => this.adapter.set(this.hashId(id), data));
5658
}
5759

58-
patch<T extends Record<any, any> = Record<any, any>>(id: string, data: T) {
60+
patch(id: string, data: any) {
61+
if (!this.filter(data, id)) return;
5962
return fakePromise(this.addToRelationship(id)).then(() => this.adapter.patch(false, this.hashId(id), data));
6063
}
6164

src/cache/resources/default/guild-based.ts

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,11 @@ export class GuildBasedResource<T = any> {
2222
this.client = client;
2323
}
2424

25+
//@ts-expect-error
26+
filter(data: any, id: string, guild_id: string) {
27+
return true;
28+
}
29+
2530
parse(data: any, id: string, guild_id: string) {
2631
if (!data.id) data.id = id;
2732
data.guild_id = guild_id;
@@ -41,7 +46,7 @@ export class GuildBasedResource<T = any> {
4146

4247
setIfNI(intent: keyof typeof GatewayIntentBits, id: string, guildId: string, data: any) {
4348
if (!this.cache.hasIntent(intent)) {
44-
return fakePromise(this.set(id, guildId, data)).then(() => data);
49+
return this.set(id, guildId, data);
4550
}
4651
}
4752

@@ -56,7 +61,10 @@ export class GuildBasedResource<T = any> {
5661
set(__keys: string, guild: string, data: any): ReturnCache<void>;
5762
set(__keys: [string, any][], guild: string): ReturnCache<void>;
5863
set(__keys: string | [string, any][], guild: string, data?: any): ReturnCache<void> {
59-
const keys: [string, any][] = Array.isArray(__keys) ? __keys : [[__keys, data]];
64+
const keys = (Array.isArray(__keys) ? __keys : [[__keys, data]]).filter(x => this.filter(x[1], x[1], guild)) as [
65+
string,
66+
any,
67+
][];
6068

6169
return fakePromise(
6270
this.addToRelationship(
@@ -66,7 +74,7 @@ export class GuildBasedResource<T = any> {
6674
).then(() =>
6775
this.adapter.set(
6876
keys.map(([key, value]) => {
69-
return [this.hashGuildId(guild, key), this.parse(value, key, guild)];
77+
return [this.hashGuildId(guild, key), this.parse(value, key, guild)] as const;
7078
}),
7179
),
7280
) as void;
@@ -75,7 +83,11 @@ export class GuildBasedResource<T = any> {
7583
patch(__keys: string, guild: string, data: any): ReturnCache<void>;
7684
patch(__keys: [string, any][], guild: string): ReturnCache<void>;
7785
patch(__keys: string | [string, any][], guild: string, data?: any): ReturnCache<void> {
78-
const keys: [string, any][] = Array.isArray(__keys) ? __keys : [[__keys, data]];
86+
const keys = (Array.isArray(__keys) ? __keys : [[__keys, data]]).filter(x => this.filter(x[1], x[1], guild)) as [
87+
string,
88+
any,
89+
][];
90+
7991
return fakePromise(this.adapter.get(keys.map(([key]) => this.hashGuildId(guild, key)))).then(oldDatas =>
8092
fakePromise(
8193
this.addToRelationship(

src/cache/resources/default/guild-related.ts

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,11 @@ export class GuildRelatedResource<T = any> {
2222
this.client = client;
2323
}
2424

25+
//@ts-expect-error
26+
filter(data: any, id: string, guild_id?: string) {
27+
return true;
28+
}
29+
2530
parse(data: any, id: string, guild_id: string) {
2631
if (!data.id) data.id = id;
2732
data.guild_id = guild_id;
@@ -40,7 +45,7 @@ export class GuildRelatedResource<T = any> {
4045

4146
setIfNI(intent: keyof typeof GatewayIntentBits, id: string, guildId: string, data: any) {
4247
if (!this.cache.hasIntent(intent)) {
43-
return fakePromise(this.set(id, guildId, data)).then(() => data);
48+
return this.set(id, guildId, data);
4449
}
4550
}
4651

@@ -55,7 +60,11 @@ export class GuildRelatedResource<T = any> {
5560
set(__keys: string, guild: string, data: any): ReturnCache<void>;
5661
set(__keys: [string, any][], guild: string): ReturnCache<void>;
5762
set(__keys: string | [string, any][], guild: string, data?: any): ReturnCache<void> {
58-
const keys: [string, any][] = Array.isArray(__keys) ? __keys : [[__keys, data]];
63+
const keys = (Array.isArray(__keys) ? __keys : [[__keys, data]]).filter(x => this.filter(x[1], x[1], guild)) as [
64+
string,
65+
any,
66+
][];
67+
5968
return fakePromise(
6069
this.addToRelationship(
6170
keys.map(x => x[0]),
@@ -65,7 +74,7 @@ export class GuildRelatedResource<T = any> {
6574
() =>
6675
this.adapter.set(
6776
keys.map(([key, value]) => {
68-
return [this.hashId(key), this.parse(value, key, guild)];
77+
return [this.hashId(key), this.parse(value, key, guild)] as const;
6978
}),
7079
) as void,
7180
);
@@ -74,7 +83,10 @@ export class GuildRelatedResource<T = any> {
7483
patch(__keys: string, guild?: string, data?: any): ReturnCache<void>;
7584
patch(__keys: [string, any][], guild?: string): ReturnCache<void>;
7685
patch(__keys: string | [string, any][], guild?: string, data?: any): ReturnCache<void> {
77-
const keys: [string, any][] = Array.isArray(__keys) ? __keys : [[__keys, data]];
86+
const keys = (Array.isArray(__keys) ? __keys : [[__keys, data]]).filter(x => this.filter(x[1], x[1], guild)) as [
87+
string,
88+
any,
89+
][];
7890

7991
if (guild) {
8092
return fakePromise(
@@ -87,7 +99,7 @@ export class GuildRelatedResource<T = any> {
8799
this.adapter.patch(
88100
false,
89101
keys.map(([key, value]) => {
90-
return [this.hashId(key), this.parse(value, key, guild)];
102+
return [this.hashId(key), this.parse(value, key, guild)] as const;
91103
}),
92104
) as void,
93105
);

src/cache/resources/voice-states.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,11 @@ import { GuildBasedResource } from './default/guild-based';
77
export class VoiceStates extends GuildBasedResource {
88
namespace = 'voice_state';
99

10+
override parse(data: any, id: string, guild_id: string) {
11+
const { member, ...rest } = super.parse(data, id, guild_id);
12+
return rest;
13+
}
14+
1015
override get(memberId: string, guildId: string): ReturnCache<VoiceState | undefined> {
1116
return fakePromise(super.get(memberId, guildId)).then(state =>
1217
state ? new VoiceState(this.client, state) : undefined,
@@ -23,11 +28,6 @@ export class VoiceStates extends GuildBasedResource {
2328
override values(guildId: string): ReturnCache<VoiceState[]> {
2429
return fakePromise(super.values(guildId)).then(states => states.map(state => new VoiceState(this.client, state)));
2530
}
26-
27-
override parse(data: any, id: string, guild_id: string) {
28-
const { member, ...rest } = super.parse(data, id, guild_id);
29-
return rest;
30-
}
3131
}
3232

3333
export type VoiceStateResource = Omit<GatewayVoiceState, 'member'> & { guild_id: string };

src/common/shorters/guilds.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,12 @@ import type { ObjectToLower } from '..';
1414
import { resolveFiles } from '../../builders';
1515
import {
1616
AnonymousGuild,
17+
AutoModerationRule,
1718
BaseChannel,
1819
Guild,
1920
GuildMember,
2021
Sticker,
2122
type CreateStickerBodyRequest,
22-
AutoModerationRule,
2323
} from '../../structures';
2424
import channelFrom from '../../structures/channels';
2525
import { BaseShorter } from './base';
@@ -277,7 +277,7 @@ export class GuildShorter extends BaseShorter {
277277
list: async (guildId: string) => {
278278
const stickers = await this.client.proxy.guilds(guildId).stickers.get();
279279
await this.client.cache.stickers?.set(
280-
stickers.map(st => [st.id, st]),
280+
stickers.map(st => [st.id, st] as any),
281281
guildId,
282282
);
283283
return stickers.map(st => new Sticker(this.client, st));

0 commit comments

Comments
 (0)