From 7f4044469bee573b4fba5fe3e88c9abddecd230d Mon Sep 17 00:00:00 2001 From: MARCROCK22 <57925328+MARCROCK22@users.noreply.github.com> Date: Thu, 14 Nov 2024 13:45:14 -0400 Subject: [PATCH] fix: default callbacks & cache update (#294) --- src/cache/index.ts | 100 ++++++------------- src/cache/resources/default/base.ts | 14 +-- src/cache/resources/default/guild-based.ts | 14 +-- src/cache/resources/default/guild-related.ts | 14 +-- src/client/base.ts | 15 ++- src/client/workerclient.ts | 14 +-- src/commands/applications/menu.ts | 16 +-- src/commands/handle.ts | 6 +- src/commands/handler.ts | 12 +-- 9 files changed, 58 insertions(+), 147 deletions(-) diff --git a/src/cache/index.ts b/src/cache/index.ts index f2feb55..1057312 100644 --- a/src/cache/index.ts +++ b/src/cache/index.ts @@ -124,79 +124,37 @@ export class Cache { constructor( public intents: number, public adapter: Adapter, - readonly disabledCache: DisabledCache = {}, - client?: UsingClient, + disabledCache: DisabledCache, + client: UsingClient, ) { - // non-guild based - if (!this.disabledCache.users) { - this.users = new Users(this, client); - } - if (!this.disabledCache.guilds) { - this.guilds = new Guilds(this, client); - } - - // guild related - if (!this.disabledCache.members) { - this.members = new Members(this, client); - } - if (!this.disabledCache.voiceStates) { - this.voiceStates = new VoiceStates(this, client); - } - - // guild based - if (!this.disabledCache.roles) { - this.roles = new Roles(this, client); - } - if (!this.disabledCache.overwrites) { - this.overwrites = new Overwrites(this, client); - } - if (!this.disabledCache.channels) { - this.channels = new Channels(this, client); - } - if (!this.disabledCache.emojis) { - this.emojis = new Emojis(this, client); - } - if (!this.disabledCache.stickers) { - this.stickers = new Stickers(this, client); - } - if (!this.disabledCache.presences) { - this.presences = new Presences(this, client); - } - if (!this.disabledCache.stageInstances) { - this.stageInstances = new StageInstances(this, client); - } - if (!this.disabledCache.messages) { - this.messages = new Messages(this, client); - } - if (!this.disabledCache.bans) { - this.bans = new Bans(this, client); - } - - if (this.disabledCache.onPacket) { - //@ts-expect-error - this.onPacket = () => { - // disable cache - }; - } + this.buildCache(disabledCache, client); } - /** @internal */ - __setClient(client: UsingClient) { - this.users?.__setClient(client); - this.guilds?.__setClient(client); + buildCache(disabledCache: DisabledCache, client: UsingClient) { + // non-guild based + this.users = disabledCache.users ? undefined : new Users(this, client); + this.guilds = disabledCache.guilds ? undefined : new Guilds(this, client); - this.members?.__setClient(client); - this.voiceStates?.__setClient(client); + // guild related + this.members = disabledCache.members ? undefined : new Members(this, client); + this.voiceStates = disabledCache.voiceStates ? undefined : new VoiceStates(this, client); - this.roles?.__setClient(client); - this.overwrites?.__setClient(client); - this.channels?.__setClient(client); - this.emojis?.__setClient(client); - this.stickers?.__setClient(client); - this.presences?.__setClient(client); - this.stageInstances?.__setClient(client); - this.messages?.__setClient(client); - this.bans?.__setClient(client); + // guild based + this.roles = disabledCache.roles ? undefined : new Roles(this, client); + this.overwrites = disabledCache.overwrites ? undefined : new Overwrites(this, client); + this.channels = disabledCache.channels ? undefined : new Channels(this, client); + this.emojis = disabledCache.emojis ? undefined : new Emojis(this, client); + this.stickers = disabledCache.stickers ? undefined : new Stickers(this, client); + this.presences = disabledCache.presences ? undefined : new Presences(this, client); + this.stageInstances = disabledCache.stageInstances ? undefined : new StageInstances(this, client); + this.messages = disabledCache.messages ? undefined : new Messages(this, client); + this.bans = disabledCache.bans ? undefined : new Bans(this, client); + + this.onPacket = disabledCache.onPacket + ? ((() => { + // + }) as any as () => Promise) + : this.onPacketDefault.bind(this); } flush(): ReturnCache { @@ -499,7 +457,11 @@ export class Cache { await this.adapter.bulkSet(allData); } - async onPacket(event: GatewayDispatchPayload) { + onPacket(event: GatewayDispatchPayload) { + return this.onPacketDefault(event); + } + + protected async onPacketDefault(event: GatewayDispatchPayload) { switch (event.t) { case 'READY': await this.users?.set(event.d.user.id, event.d.user); diff --git a/src/cache/resources/default/base.ts b/src/cache/resources/default/base.ts index cb15a6e..7f87d89 100644 --- a/src/cache/resources/default/base.ts +++ b/src/cache/resources/default/base.ts @@ -4,22 +4,12 @@ import type { GatewayIntentBits } from '../../../types'; import type { Cache, ReturnCache } from '../../index'; export class BaseResource { - client!: UsingClient; namespace = 'base'; constructor( protected cache: Cache, - client?: UsingClient, - ) { - if (client) { - this.client = client; - } - } - - /** @internal */ - __setClient(client: UsingClient) { - this.client = client; - } + readonly client: UsingClient, + ) {} //@ts-expect-error filter(data: any, id: string) { diff --git a/src/cache/resources/default/guild-based.ts b/src/cache/resources/default/guild-based.ts index 3d4dfac..edd3aa5 100644 --- a/src/cache/resources/default/guild-based.ts +++ b/src/cache/resources/default/guild-based.ts @@ -4,22 +4,12 @@ import type { GatewayIntentBits } from '../../../types'; import type { Cache, ReturnCache } from '../../index'; export class GuildBasedResource { - client!: UsingClient; namespace = 'base'; constructor( protected cache: Cache, - client?: UsingClient, - ) { - if (client) { - this.client = client; - } - } - - /** @internal */ - __setClient(client: UsingClient) { - this.client = client; - } + readonly client: UsingClient, + ) {} //@ts-expect-error filter(data: any, id: string, guild_id: string) { diff --git a/src/cache/resources/default/guild-related.ts b/src/cache/resources/default/guild-related.ts index 2cb5069..eecf194 100644 --- a/src/cache/resources/default/guild-related.ts +++ b/src/cache/resources/default/guild-related.ts @@ -4,22 +4,12 @@ import type { GatewayIntentBits } from '../../../types'; import type { Cache, ReturnCache } from '../../index'; export class GuildRelatedResource { - client!: UsingClient; namespace = 'base'; constructor( protected cache: Cache, - client?: UsingClient, - ) { - if (client) { - this.client = client; - } - } - - /** @internal */ - __setClient(client: UsingClient) { - this.client = client; - } + readonly client: UsingClient, + ) {} //@ts-expect-error filter(data: any, id: string, guild_id?: string) { diff --git a/src/client/base.ts b/src/client/base.ts index 5afe4a8..4473c4c 100644 --- a/src/client/base.ts +++ b/src/client/base.ts @@ -1,6 +1,6 @@ import { join } from 'node:path'; import { ApiHandler } from '../api'; -import type { Adapter } from '../cache'; +import type { Adapter, DisabledCache } from '../cache'; import { Cache, MemoryAdapter } from '../cache'; import type { Command, @@ -60,7 +60,7 @@ import type { MessageStructure } from './transformers'; export class BaseClient { rest = new ApiHandler({ token: 'INVALID' }); - cache = new Cache(0, new MemoryAdapter()); + cache = new Cache(0, new MemoryAdapter(), {}, this); applications = new ApplicationShorter(this); users = new UsersShorter(this); @@ -198,7 +198,7 @@ export class BaseClient { this.rest = rest; } if (cache) { - const caches: (keyof Cache['disabledCache'])[] = [ + const caches: (keyof DisabledCache)[] = [ 'bans', 'channels', 'emojis', @@ -214,7 +214,7 @@ export class BaseClient { 'users', 'voiceStates', ]; - let disabledCache: Partial> = this.cache.disabledCache; + let disabledCache: Partial> = {}; if (typeof cache.disabledCache === 'boolean') { for (const i of caches) { @@ -228,7 +228,8 @@ export class BaseClient { disabledCache = cache.disabledCache; } - this.cache = new Cache(this.cache.intents, cache.adapter ?? this.cache.adapter, disabledCache, this); + if (cache.adapter) this.cache.adapter = cache.adapter; + if (cache.disabledCache) this.cache.buildCache(disabledCache, this); } if (middlewares) { this.middlewares = middlewares; @@ -271,8 +272,6 @@ export class BaseClient { if (this.rest.options.token === 'INVALID') this.rest.options.token = token; this.rest.debug = debug; - this.cache.__setClient(this); - if (!this.handleCommand) this.handleCommand = new HandleCommand(this); // The reason of this method is so for adapters that need to connect somewhere, have time to connect. @@ -528,7 +527,7 @@ export interface ServicesOptions { rest?: ApiHandler; cache?: { adapter?: Adapter; - disabledCache?: boolean | Cache['disabledCache'] | ((cacheType: keyof Cache['disabledCache']) => boolean); + disabledCache?: boolean | DisabledCache | ((cacheType: keyof DisabledCache) => boolean); }; langs?: { default?: string; diff --git a/src/client/workerclient.ts b/src/client/workerclient.ts index e85b1a8..901e86c 100644 --- a/src/client/workerclient.ts +++ b/src/client/workerclient.ts @@ -95,7 +95,7 @@ export class WorkerClient extends BaseClient { setServices(rest: ServicesOptions) { super.setServices(rest); - if (rest.cache) { + if (rest.cache?.adapter) { this.__setServicesCache = true; } } @@ -122,13 +122,8 @@ export class WorkerClient extends BaseClient { name: `[Worker #${workerData.workerId}]`, }); - if (this.__setServicesCache) { - this.setServices({ - cache: { - disabledCache: this.cache.disabledCache, - }, - }); - } else { + if (this.__setServicesCache) delete this.__setServicesCache; + else { const adapter = new WorkerAdapter(workerData); if (this.options.postMessage) { adapter.postMessage = this.options.postMessage; @@ -136,13 +131,10 @@ export class WorkerClient extends BaseClient { this.setServices({ cache: { adapter, - disabledCache: this.cache.disabledCache, }, }); } - delete this.__setServicesCache; - if (workerData.debug) { this.debugger = new Logger({ name: `[Worker #${workerData.workerId}]`, diff --git a/src/commands/applications/menu.ts b/src/commands/applications/menu.ts index 2cce941..a17516f 100644 --- a/src/commands/applications/menu.ts +++ b/src/commands/applications/menu.ts @@ -55,16 +55,8 @@ export abstract class ContextMenuCommand { abstract run?(context: MenuCommandContext): any; onAfterRun?(context: MenuCommandContext, error: unknown | undefined): any; - onRunError(context: MenuCommandContext, error: unknown): any { - context.client.logger.fatal(`${this.name}.`, context.author.id, error); - } - onMiddlewaresError(context: MenuCommandContext, error: string): any { - context.client.logger.fatal(`${this.name}.`, context.author.id, error); - } - onBotPermissionsFail(context: MenuCommandContext, permissions: PermissionStrings): any { - context.client.logger.fatal(`${this.name}.`, context.author.id, permissions); - } - onInternalError(client: UsingClient, command: ContextMenuCommand, error?: unknown): any { - client.logger.fatal(command.name, error); - } + onRunError?(context: MenuCommandContext, error: unknown): any; + onMiddlewaresError?(context: MenuCommandContext, error: string): any; + onBotPermissionsFail?(context: MenuCommandContext, permissions: PermissionStrings): any; + onInternalError?(client: UsingClient, command: ContextMenuCommand, error?: unknown): any; } diff --git a/src/commands/handle.ts b/src/commands/handle.ts index dead1d6..7649a34 100644 --- a/src/commands/handle.ts +++ b/src/commands/handle.ts @@ -102,7 +102,7 @@ export class HandleCommand { ) { if (context.guildId && command.botPermissions) { const permissions = this.checkPermissions(interaction.appPermissions, command.botPermissions); - if (permissions) return command.onBotPermissionsFail(context, permissions); + if (permissions) return command.onBotPermissionsFail?.(context, permissions); } const resultGlobal = await this.runGlobalMiddlewares(command, context); @@ -115,12 +115,12 @@ export class HandleCommand { await command.run!(context); await command.onAfterRun?.(context, undefined); } catch (error) { - await command.onRunError(context, error); + await command.onRunError?.(context, error); await command.onAfterRun?.(context, error); } } catch (error) { try { - await command.onInternalError(this.client, command, error); + await command.onInternalError?.(this.client, command, error); } catch { // pass } diff --git a/src/commands/handler.ts b/src/commands/handler.ts index 2cf0072..324042c 100644 --- a/src/commands/handler.ts +++ b/src/commands/handler.ts @@ -467,17 +467,13 @@ export class CommandHandler extends BaseHandler { if (!(commandInstance instanceof ContextMenuCommand)) return false; commandInstance.onAfterRun ??= this.client.options.commands?.defaults?.onAfterRun; - if (this.client.options.commands?.defaults?.onBotPermissionsFail) - commandInstance.onBotPermissionsFail ??= this.client.options.commands?.defaults?.onBotPermissionsFail; + commandInstance.onBotPermissionsFail ??= this.client.options.commands?.defaults?.onBotPermissionsFail; - if (this.client.options.commands?.defaults?.onInternalError) - commandInstance.onInternalError ??= this.client.options.commands.defaults.onInternalError; + commandInstance.onInternalError ??= this.client.options.commands?.defaults?.onInternalError; - if (this.client.options.commands?.defaults?.onMiddlewaresError) - commandInstance.onMiddlewaresError ??= this.client.options.commands.defaults.onMiddlewaresError; + commandInstance.onMiddlewaresError ??= this.client.options.commands?.defaults?.onMiddlewaresError; - if (this.client.options.commands?.defaults?.onRunError) - commandInstance.onRunError ??= this.client.options.commands.defaults.onRunError; + commandInstance.onRunError ??= this.client.options.commands?.defaults?.onRunError; return commandInstance; }