mirror of
https://github.com/tiramisulabs/seyfert.git
synced 2025-07-01 20:46:08 +00:00
feat: cache.<resource>.filter
This commit is contained in:
parent
38ef5d91cf
commit
961eb04b37
3
.github/workflows/transpile.yml
vendored
3
.github/workflows/transpile.yml
vendored
@ -4,6 +4,9 @@ on:
|
||||
push:
|
||||
branches:
|
||||
- main
|
||||
pull_request:
|
||||
branches:
|
||||
- main
|
||||
|
||||
jobs:
|
||||
build:
|
||||
|
29
src/cache/index.ts
vendored
29
src/cache/index.ts
vendored
@ -15,15 +15,7 @@ import { Stickers } from './resources/stickers';
|
||||
import { Threads } from './resources/threads';
|
||||
import { VoiceStates } from './resources/voice-states';
|
||||
|
||||
import {
|
||||
ChannelType,
|
||||
GatewayIntentBits,
|
||||
type APIEmoji,
|
||||
type APISticker,
|
||||
type APIThreadChannel,
|
||||
type GatewayDispatchPayload,
|
||||
type GatewayReadyDispatchData,
|
||||
} from 'discord-api-types/v10';
|
||||
import { ChannelType, GatewayIntentBits, type GatewayDispatchPayload } from 'discord-api-types/v10';
|
||||
import type { InternalOptions, UsingClient } from '../commands';
|
||||
import { Overwrites } from './resources/overwrites';
|
||||
|
||||
@ -314,6 +306,7 @@ export class Cache {
|
||||
case 'emojis':
|
||||
case 'overwrites':
|
||||
{
|
||||
if (!this[type]?.filter(data, id, guildId)) continue;
|
||||
const hashId = this[type]?.hashId(guildId!);
|
||||
if (!hashId) {
|
||||
continue;
|
||||
@ -331,6 +324,7 @@ export class Cache {
|
||||
case 'voiceStates':
|
||||
case 'members':
|
||||
{
|
||||
if (!this[type]?.filter(data, id, guildId)) continue;
|
||||
const hashId = this[type]?.hashId(guildId!);
|
||||
if (!hashId) {
|
||||
continue;
|
||||
@ -346,6 +340,7 @@ export class Cache {
|
||||
case 'users':
|
||||
case 'guilds':
|
||||
{
|
||||
if (!this[type]?.filter(data, id)) continue;
|
||||
const hashId = this[type]?.namespace;
|
||||
if (!hashId) {
|
||||
continue;
|
||||
@ -401,6 +396,7 @@ export class Cache {
|
||||
case 'emojis':
|
||||
case 'overwrites':
|
||||
{
|
||||
if (!this[type]?.filter(data, id, guildId)) continue;
|
||||
const hashId = this[type]?.hashId(guildId!);
|
||||
if (!hashId) {
|
||||
continue;
|
||||
@ -418,6 +414,7 @@ export class Cache {
|
||||
case 'voiceStates':
|
||||
case 'members':
|
||||
{
|
||||
if (!this[type]?.filter(data, id, guildId)) continue;
|
||||
const hashId = this[type]?.hashId(guildId!);
|
||||
if (!hashId) {
|
||||
continue;
|
||||
@ -433,6 +430,7 @@ export class Cache {
|
||||
case 'users':
|
||||
case 'guilds':
|
||||
{
|
||||
if (!this[type]?.filter(data, id)) continue;
|
||||
const hashId = this[type]?.namespace;
|
||||
if (!hashId) {
|
||||
continue;
|
||||
@ -456,10 +454,7 @@ export class Cache {
|
||||
async onPacket(event: GatewayDispatchPayload) {
|
||||
switch (event.t) {
|
||||
case 'READY':
|
||||
{
|
||||
const data = event.d as GatewayReadyDispatchData;
|
||||
await this.users?.set(data.user.id, data.user);
|
||||
}
|
||||
await this.users?.set(event.d.user.id, event.d.user);
|
||||
break;
|
||||
case 'GUILD_CREATE':
|
||||
case 'GUILD_UPDATE':
|
||||
@ -498,14 +493,14 @@ export class Cache {
|
||||
case 'GUILD_EMOJIS_UPDATE':
|
||||
await this.emojis?.remove(await this.emojis?.keys(event.d.guild_id), event.d.guild_id);
|
||||
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,
|
||||
);
|
||||
break;
|
||||
case 'GUILD_STICKERS_UPDATE':
|
||||
await this.stickers?.remove(await this.stickers?.keys(event.d.guild_id), event.d.guild_id);
|
||||
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,
|
||||
);
|
||||
break;
|
||||
@ -524,11 +519,11 @@ export class Cache {
|
||||
|
||||
case 'THREAD_CREATE':
|
||||
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;
|
||||
|
||||
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;
|
||||
|
||||
case 'USER_UPDATE':
|
||||
|
11
src/cache/resources/default/base.ts
vendored
11
src/cache/resources/default/base.ts
vendored
@ -22,8 +22,9 @@ export class BaseResource<T = any> {
|
||||
this.client = client;
|
||||
}
|
||||
|
||||
get rest() {
|
||||
return this.client!.rest;
|
||||
//@ts-expect-error
|
||||
filter(data: any, id: string) {
|
||||
return true;
|
||||
}
|
||||
|
||||
get adapter() {
|
||||
@ -39,7 +40,7 @@ export class BaseResource<T = any> {
|
||||
|
||||
setIfNI(intent: keyof typeof GatewayIntentBits, id: string, data: any) {
|
||||
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) {
|
||||
if (!this.filter(data, id)) return;
|
||||
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));
|
||||
}
|
||||
|
||||
|
20
src/cache/resources/default/guild-based.ts
vendored
20
src/cache/resources/default/guild-based.ts
vendored
@ -22,6 +22,11 @@ export class GuildBasedResource<T = any> {
|
||||
this.client = client;
|
||||
}
|
||||
|
||||
//@ts-expect-error
|
||||
filter(data: any, id: string, guild_id: string) {
|
||||
return true;
|
||||
}
|
||||
|
||||
parse(data: any, id: string, guild_id: string) {
|
||||
if (!data.id) data.id = 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) {
|
||||
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, any][], guild: string): 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(
|
||||
this.addToRelationship(
|
||||
@ -66,7 +74,7 @@ export class GuildBasedResource<T = any> {
|
||||
).then(() =>
|
||||
this.adapter.set(
|
||||
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;
|
||||
@ -75,7 +83,11 @@ export class GuildBasedResource<T = any> {
|
||||
patch(__keys: string, guild: string, data: any): ReturnCache<void>;
|
||||
patch(__keys: [string, any][], guild: string): 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 =>
|
||||
fakePromise(
|
||||
this.addToRelationship(
|
||||
|
22
src/cache/resources/default/guild-related.ts
vendored
22
src/cache/resources/default/guild-related.ts
vendored
@ -22,6 +22,11 @@ export class GuildRelatedResource<T = any> {
|
||||
this.client = client;
|
||||
}
|
||||
|
||||
//@ts-expect-error
|
||||
filter(data: any, id: string, guild_id?: string) {
|
||||
return true;
|
||||
}
|
||||
|
||||
parse(data: any, id: string, guild_id: string) {
|
||||
if (!data.id) data.id = 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) {
|
||||
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, any][], guild: string): 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(
|
||||
this.addToRelationship(
|
||||
keys.map(x => x[0]),
|
||||
@ -65,7 +74,7 @@ export class GuildRelatedResource<T = any> {
|
||||
() =>
|
||||
this.adapter.set(
|
||||
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,
|
||||
);
|
||||
@ -74,7 +83,10 @@ export class GuildRelatedResource<T = any> {
|
||||
patch(__keys: string, guild?: string, data?: any): ReturnCache<void>;
|
||||
patch(__keys: [string, any][], guild?: string): 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) {
|
||||
return fakePromise(
|
||||
@ -87,7 +99,7 @@ export class GuildRelatedResource<T = any> {
|
||||
this.adapter.patch(
|
||||
false,
|
||||
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,
|
||||
);
|
||||
|
10
src/cache/resources/voice-states.ts
vendored
10
src/cache/resources/voice-states.ts
vendored
@ -7,6 +7,11 @@ import { GuildBasedResource } from './default/guild-based';
|
||||
export class VoiceStates extends GuildBasedResource {
|
||||
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> {
|
||||
return fakePromise(super.get(memberId, guildId)).then(state =>
|
||||
state ? new VoiceState(this.client, state) : undefined,
|
||||
@ -23,11 +28,6 @@ export class VoiceStates extends GuildBasedResource {
|
||||
override values(guildId: string): ReturnCache<VoiceState[]> {
|
||||
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 };
|
||||
|
@ -14,12 +14,12 @@ import type { ObjectToLower } from '..';
|
||||
import { resolveFiles } from '../../builders';
|
||||
import {
|
||||
AnonymousGuild,
|
||||
AutoModerationRule,
|
||||
BaseChannel,
|
||||
Guild,
|
||||
GuildMember,
|
||||
Sticker,
|
||||
type CreateStickerBodyRequest,
|
||||
AutoModerationRule,
|
||||
} from '../../structures';
|
||||
import channelFrom from '../../structures/channels';
|
||||
import { BaseShorter } from './base';
|
||||
@ -277,7 +277,7 @@ export class GuildShorter extends BaseShorter {
|
||||
list: async (guildId: string) => {
|
||||
const stickers = await this.client.proxy.guilds(guildId).stickers.get();
|
||||
await this.client.cache.stickers?.set(
|
||||
stickers.map(st => [st.id, st]),
|
||||
stickers.map(st => [st.id, st] as any),
|
||||
guildId,
|
||||
);
|
||||
return stickers.map(st => new Sticker(this.client, st));
|
||||
|
Loading…
x
Reference in New Issue
Block a user