fix: default callbacks & cache update (#294)

This commit is contained in:
MARCROCK22 2024-11-14 13:45:14 -04:00 committed by GitHub
parent e4233e6a40
commit 7f4044469b
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
9 changed files with 58 additions and 147 deletions

100
src/cache/index.ts vendored
View File

@ -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<void>)
: this.onPacketDefault.bind(this);
}
flush(): ReturnCache<void> {
@ -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);

View File

@ -4,22 +4,12 @@ import type { GatewayIntentBits } from '../../../types';
import type { Cache, ReturnCache } from '../../index';
export class BaseResource<T = any, S = any> {
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) {

View File

@ -4,22 +4,12 @@ import type { GatewayIntentBits } from '../../../types';
import type { Cache, ReturnCache } from '../../index';
export class GuildBasedResource<T = any, S = any> {
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) {

View File

@ -4,22 +4,12 @@ import type { GatewayIntentBits } from '../../../types';
import type { Cache, ReturnCache } from '../../index';
export class GuildRelatedResource<T = any, S = any> {
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) {

View File

@ -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<Record<keyof Cache['disabledCache'], boolean>> = this.cache.disabledCache;
let disabledCache: Partial<Record<keyof DisabledCache, boolean>> = {};
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;

View File

@ -95,7 +95,7 @@ export class WorkerClient<Ready extends boolean = boolean> 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<Ready extends boolean = boolean> 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<Ready extends boolean = boolean> extends BaseClient {
this.setServices({
cache: {
adapter,
disabledCache: this.cache.disabledCache,
},
});
}
delete this.__setServicesCache;
if (workerData.debug) {
this.debugger = new Logger({
name: `[Worker #${workerData.workerId}]`,

View File

@ -55,16 +55,8 @@ export abstract class ContextMenuCommand {
abstract run?(context: MenuCommandContext<any>): any;
onAfterRun?(context: MenuCommandContext<any>, error: unknown | undefined): any;
onRunError(context: MenuCommandContext<any, never>, error: unknown): any {
context.client.logger.fatal(`${this.name}.<onRunError>`, context.author.id, error);
}
onMiddlewaresError(context: MenuCommandContext<any, never>, error: string): any {
context.client.logger.fatal(`${this.name}.<onMiddlewaresError>`, context.author.id, error);
}
onBotPermissionsFail(context: MenuCommandContext<any, never>, permissions: PermissionStrings): any {
context.client.logger.fatal(`${this.name}.<onBotPermissionsFail>`, context.author.id, permissions);
}
onInternalError(client: UsingClient, command: ContextMenuCommand, error?: unknown): any {
client.logger.fatal(command.name, error);
}
onRunError?(context: MenuCommandContext<any, never>, error: unknown): any;
onMiddlewaresError?(context: MenuCommandContext<any, never>, error: string): any;
onBotPermissionsFail?(context: MenuCommandContext<any, never>, permissions: PermissionStrings): any;
onInternalError?(client: UsingClient, command: ContextMenuCommand, error?: unknown): any;
}

View File

@ -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
}

View File

@ -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;
}