diff --git a/.github/workflows/transpile.yml b/.github/workflows/transpile.yml index 41150ff..edfe021 100644 --- a/.github/workflows/transpile.yml +++ b/.github/workflows/transpile.yml @@ -4,6 +4,9 @@ on: push: branches: - main + pull_request: + branches: + - main jobs: build: diff --git a/src/cache/index.ts b/src/cache/index.ts index 9ffe174..ea2be36 100644 --- a/src/cache/index.ts +++ b/src/cache/index.ts @@ -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': diff --git a/src/cache/resources/default/base.ts b/src/cache/resources/default/base.ts index 0704f32..9500a95 100644 --- a/src/cache/resources/default/base.ts +++ b/src/cache/resources/default/base.ts @@ -22,8 +22,9 @@ export class BaseResource { 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 { 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 { } 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 = Record>(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)); } diff --git a/src/cache/resources/default/guild-based.ts b/src/cache/resources/default/guild-based.ts index da13799..f70b763 100644 --- a/src/cache/resources/default/guild-based.ts +++ b/src/cache/resources/default/guild-based.ts @@ -22,6 +22,11 @@ export class GuildBasedResource { 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 { 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 { set(__keys: string, guild: string, data: any): ReturnCache; set(__keys: [string, any][], guild: string): ReturnCache; set(__keys: string | [string, any][], guild: string, data?: any): ReturnCache { - 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 { ).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 { patch(__keys: string, guild: string, data: any): ReturnCache; patch(__keys: [string, any][], guild: string): ReturnCache; patch(__keys: string | [string, any][], guild: string, data?: any): ReturnCache { - 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( diff --git a/src/cache/resources/default/guild-related.ts b/src/cache/resources/default/guild-related.ts index dd40449..e2850bd 100644 --- a/src/cache/resources/default/guild-related.ts +++ b/src/cache/resources/default/guild-related.ts @@ -22,6 +22,11 @@ export class GuildRelatedResource { 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 { 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 { set(__keys: string, guild: string, data: any): ReturnCache; set(__keys: [string, any][], guild: string): ReturnCache; set(__keys: string | [string, any][], guild: string, data?: any): ReturnCache { - 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 { () => 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 { patch(__keys: string, guild?: string, data?: any): ReturnCache; patch(__keys: [string, any][], guild?: string): ReturnCache; patch(__keys: string | [string, any][], guild?: string, data?: any): ReturnCache { - 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 { 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, ); diff --git a/src/cache/resources/voice-states.ts b/src/cache/resources/voice-states.ts index f343e64..1d39de9 100644 --- a/src/cache/resources/voice-states.ts +++ b/src/cache/resources/voice-states.ts @@ -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 { 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 { 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 & { guild_id: string }; diff --git a/src/common/shorters/guilds.ts b/src/common/shorters/guilds.ts index a0b06fb..8a99050 100644 --- a/src/common/shorters/guilds.ts +++ b/src/common/shorters/guilds.ts @@ -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));