feat: extraProps

This commit is contained in:
MARCROCK22 2024-06-06 01:57:26 +00:00
parent db6134cf95
commit f3b8e3b67d
10 changed files with 992 additions and 968 deletions

View File

@ -2,7 +2,14 @@ import { join } from 'node:path';
import { ApiHandler, Router } from '../api'; import { ApiHandler, Router } from '../api';
import type { Adapter } from '../cache'; import type { Adapter } from '../cache';
import { Cache, MemoryAdapter } from '../cache'; import { Cache, MemoryAdapter } from '../cache';
import type { Command, CommandContext, OnOptionsReturnObject, RegisteredMiddlewares, UsingClient } from '../commands'; import type {
Command,
CommandContext,
ExtraProps,
OnOptionsReturnObject,
RegisteredMiddlewares,
UsingClient,
} from '../commands';
import { IgnoreCommand, type InferWithPrefix, type MiddlewareContext } from '../commands/applications/shared'; import { IgnoreCommand, type InferWithPrefix, type MiddlewareContext } from '../commands/applications/shared';
import { CommandHandler } from '../commands/handler'; import { CommandHandler } from '../commands/handler';
import { import {
@ -384,6 +391,7 @@ export interface BaseClientOptions {
onMiddlewaresError?: Command['onMiddlewaresError']; onMiddlewaresError?: Command['onMiddlewaresError'];
onOptionsError?: Command['onOptionsError']; onOptionsError?: Command['onOptionsError'];
onAfterRun?: Command['onAfterRun']; onAfterRun?: Command['onAfterRun'];
props?: ExtraProps;
}; };
}; };
components?: { components?: {

View File

@ -24,6 +24,7 @@ import type { OptionResolver } from '../optionresolver';
import type { CommandContext } from './chatcontext'; import type { CommandContext } from './chatcontext';
import type { import type {
DefaultLocale, DefaultLocale,
ExtraProps,
IgnoreCommand, IgnoreCommand,
OKFunction, OKFunction,
OnOptionsReturnObject, OnOptionsReturnObject,
@ -138,6 +139,8 @@ export class BaseCommand {
aliases?: string[]; aliases?: string[];
props: ExtraProps = {};
/** @internal */ /** @internal */
async __runOptions( async __runOptions(
ctx: CommandContext<{}, never>, ctx: CommandContext<{}, never>,

View File

@ -7,7 +7,7 @@ import type {
import { magicImport, type PermissionStrings } from '../../common'; import { magicImport, type PermissionStrings } from '../../common';
import type { RegisteredMiddlewares } from '../decorators'; import type { RegisteredMiddlewares } from '../decorators';
import type { MenuCommandContext } from './menucontext'; import type { MenuCommandContext } from './menucontext';
import type { UsingClient } from './shared'; import type { ExtraProps, UsingClient } from './shared';
export abstract class ContextMenuCommand { export abstract class ContextMenuCommand {
middlewares: (keyof RegisteredMiddlewares)[] = []; middlewares: (keyof RegisteredMiddlewares)[] = [];
@ -28,6 +28,8 @@ export abstract class ContextMenuCommand {
name_localizations?: Partial<Record<LocaleString, string>>; name_localizations?: Partial<Record<LocaleString, string>>;
description_localizations?: Partial<Record<LocaleString, string>>; description_localizations?: Partial<Record<LocaleString, string>>;
props: ExtraProps = {};
toJSON() { toJSON() {
return { return {
name: this.name, name: this.name,

View File

@ -13,6 +13,7 @@ export type InferWithPrefix = InternalOptions extends { withPrefix: infer P } ?
export interface GlobalMetadata {} export interface GlobalMetadata {}
export interface DefaultLocale {} export interface DefaultLocale {}
export interface ExtendContext {} export interface ExtendContext {}
export interface ExtraProps {}
export interface UsingClient extends BaseClient {} export interface UsingClient extends BaseClient {}
export type ParseClient<T extends BaseClient> = T; export type ParseClient<T extends BaseClient> = T;
export interface InternalOptions {} export interface InternalOptions {}

View File

@ -7,7 +7,7 @@ import {
} from 'discord-api-types/v10'; } from 'discord-api-types/v10';
import type { FlatObjectKeys, PermissionStrings } from '../common'; import type { FlatObjectKeys, PermissionStrings } from '../common';
import type { CommandOption, OptionsRecord, SubCommand } from './applications/chat'; import type { CommandOption, OptionsRecord, SubCommand } from './applications/chat';
import type { DefaultLocale, IgnoreCommand, MiddlewareContext } from './applications/shared'; import type { DefaultLocale, ExtraProps, IgnoreCommand, MiddlewareContext } from './applications/shared';
export interface RegisteredMiddlewares {} export interface RegisteredMiddlewares {}
@ -23,6 +23,7 @@ type DeclareOptions =
contexts?: (keyof typeof InteractionContextType)[]; contexts?: (keyof typeof InteractionContextType)[];
ignore?: IgnoreCommand; ignore?: IgnoreCommand;
aliases?: string[]; aliases?: string[];
props?: ExtraProps;
} }
| (Omit< | (Omit<
{ {
@ -34,6 +35,7 @@ type DeclareOptions =
nsfw?: boolean; nsfw?: boolean;
integrationTypes?: (keyof typeof ApplicationIntegrationType)[]; integrationTypes?: (keyof typeof ApplicationIntegrationType)[];
contexts?: (keyof typeof InteractionContextType)[]; contexts?: (keyof typeof InteractionContextType)[];
props?: ExtraProps;
}, },
'type' | 'description' 'type' | 'description'
> & { > & {
@ -157,6 +159,7 @@ export function Declare(declare: DeclareOptions) {
class extends target { class extends target {
name = declare.name; name = declare.name;
nsfw = declare.nsfw; nsfw = declare.nsfw;
props = declare.props;
contexts = contexts =
declare.contexts?.map(i => InteractionContextType[i]) ?? declare.contexts?.map(i => InteractionContextType[i]) ??
Object.values(InteractionContextType).filter(x => typeof x === 'number'); Object.values(InteractionContextType).filter(x => typeof x === 'number');

View File

@ -214,6 +214,8 @@ export class CommandHandler extends BaseHandler {
commandInstance.onRunError ??= client.options?.commands?.defaults?.onRunError; commandInstance.onRunError ??= client.options?.commands?.defaults?.onRunError;
commandInstance.__filePath = command.path; commandInstance.__filePath = command.path;
commandInstance.options ??= [] as NonNullable<Command['options']>; commandInstance.options ??= [] as NonNullable<Command['options']>;
console.log(commandInstance, commandInstance.props);
commandInstance.props ??= client.options.commands?.defaults?.props ?? {};
if (commandInstance.__autoload) { if (commandInstance.__autoload) {
//@AutoLoad //@AutoLoad
const options = await this.getFiles(dirname(command.path)); const options = await this.getFiles(dirname(command.path));
@ -239,35 +241,36 @@ export class CommandHandler extends BaseHandler {
option.onMiddlewaresError = option.onMiddlewaresError =
option.onMiddlewaresError?.bind(option) ?? option.onMiddlewaresError?.bind(option) ??
commandInstance.onMiddlewaresError?.bind(commandInstance) ?? commandInstance.onMiddlewaresError?.bind(commandInstance) ??
this.client.options?.commands?.defaults?.onMiddlewaresError; this.client.options.commands?.defaults?.onMiddlewaresError;
option.onRunError = option.onRunError =
option.onRunError?.bind(option) ?? option.onRunError?.bind(option) ??
commandInstance.onRunError?.bind(commandInstance) ?? commandInstance.onRunError?.bind(commandInstance) ??
this.client.options?.commands?.defaults?.onRunError; this.client.options.commands?.defaults?.onRunError;
option.onOptionsError = option.onOptionsError =
option.onOptionsError?.bind(option) ?? option.onOptionsError?.bind(option) ??
commandInstance.onOptionsError?.bind(commandInstance) ?? commandInstance.onOptionsError?.bind(commandInstance) ??
this.client.options?.commands?.defaults?.onOptionsError; this.client.options.commands?.defaults?.onOptionsError;
option.onInternalError = option.onInternalError =
option.onInternalError?.bind(option) ?? option.onInternalError?.bind(option) ??
commandInstance.onInternalError?.bind(commandInstance) ?? commandInstance.onInternalError?.bind(commandInstance) ??
this.client.options?.commands?.defaults?.onInternalError; this.client.options.commands?.defaults?.onInternalError;
option.onAfterRun = option.onAfterRun =
option.onAfterRun?.bind(option) ?? option.onAfterRun?.bind(option) ??
commandInstance.onAfterRun?.bind(commandInstance) ?? commandInstance.onAfterRun?.bind(commandInstance) ??
this.client.options?.commands?.defaults?.onAfterRun; this.client.options.commands?.defaults?.onAfterRun;
option.onBotPermissionsFail = option.onBotPermissionsFail =
option.onBotPermissionsFail?.bind(option) ?? option.onBotPermissionsFail?.bind(option) ??
commandInstance.onBotPermissionsFail?.bind(commandInstance) ?? commandInstance.onBotPermissionsFail?.bind(commandInstance) ??
this.client.options?.commands?.defaults?.onBotPermissionsFail; this.client.options.commands?.defaults?.onBotPermissionsFail;
option.onPermissionsFail = option.onPermissionsFail =
option.onPermissionsFail?.bind(option) ?? option.onPermissionsFail?.bind(option) ??
commandInstance.onPermissionsFail?.bind(commandInstance) ?? commandInstance.onPermissionsFail?.bind(commandInstance) ??
this.client.options?.commands?.defaults?.onPermissionsFail; this.client.options.commands?.defaults?.onPermissionsFail;
option.botPermissions ??= commandInstance.botPermissions; option.botPermissions ??= commandInstance.botPermissions;
option.defaultMemberPermissions ??= commandInstance.defaultMemberPermissions; option.defaultMemberPermissions ??= commandInstance.defaultMemberPermissions;
option.contexts ??= commandInstance.contexts; option.contexts ??= commandInstance.contexts;
option.integrationTypes ??= commandInstance.integrationTypes; option.integrationTypes ??= commandInstance.integrationTypes;
option.props ??= commandInstance.props;
} }
} }

View File

@ -1,6 +1,6 @@
import { ComponentType } from 'discord-api-types/v10'; import { ComponentType } from 'discord-api-types/v10';
import type { ContextComponentCommandInteractionMap, ComponentContext } from './componentcontext'; import type { ContextComponentCommandInteractionMap, ComponentContext } from './componentcontext';
import type { RegisteredMiddlewares, UsingClient } from '../commands'; import type { ExtraProps, RegisteredMiddlewares, UsingClient } from '../commands';
export const InteractionCommandType = { export const InteractionCommandType = {
COMPONENT: 0, COMPONENT: 0,
@ -17,6 +17,10 @@ export abstract class ComponentCommand {
abstract filter(context: ComponentContext<typeof this.componentType>): Promise<boolean> | boolean; abstract filter(context: ComponentContext<typeof this.componentType>): Promise<boolean> | boolean;
abstract run(context: ComponentContext<typeof this.componentType>): any; abstract run(context: ComponentContext<typeof this.componentType>): any;
middlewares: (keyof RegisteredMiddlewares)[] = [];
props: ExtraProps = {};
get cType(): number { get cType(): number {
return ComponentType[this.componentType]; return ComponentType[this.componentType];
} }
@ -25,6 +29,4 @@ export abstract class ComponentCommand {
onRunError?(context: ComponentContext, error: unknown): any; onRunError?(context: ComponentContext, error: unknown): any;
onMiddlewaresError?(context: ComponentContext, error: string): any; onMiddlewaresError?(context: ComponentContext, error: string): any;
onInternalError?(client: UsingClient, error?: unknown): any; onInternalError?(client: UsingClient, error?: unknown): any;
middlewares: (keyof RegisteredMiddlewares)[] = [];
} }

View File

@ -50,7 +50,7 @@ export class ComponentContext<
super(client); super(client);
} }
command?: ComponentCommand; command!: ComponentCommand;
metadata: CommandMetadata<UnionToTuple<M>> = {} as never; metadata: CommandMetadata<UnionToTuple<M>> = {} as never;
globalMetadata: GlobalMetadata = {}; globalMetadata: GlobalMetadata = {};

View File

@ -1,4 +1,4 @@
import type { RegisteredMiddlewares, UsingClient } from '../commands'; import type { ExtraProps, RegisteredMiddlewares, UsingClient } from '../commands';
import { InteractionCommandType } from './componentcommand'; import { InteractionCommandType } from './componentcommand';
import type { ModalContext } from './modalcontext'; import type { ModalContext } from './modalcontext';
@ -13,6 +13,8 @@ export abstract class ModalCommand {
middlewares: (keyof RegisteredMiddlewares)[] = []; middlewares: (keyof RegisteredMiddlewares)[] = [];
props: ExtraProps = {};
onAfterRun?(context: ModalContext, error: unknown | undefined): any; onAfterRun?(context: ModalContext, error: unknown | undefined): any;
onRunError?(context: ModalContext, error: unknown): any; onRunError?(context: ModalContext, error: unknown): any;
onMiddlewaresError?(context: ModalContext, error: string): any; onMiddlewaresError?(context: ModalContext, error: string): any;

View File

@ -38,7 +38,7 @@ export class ModalContext<M extends keyof RegisteredMiddlewares = never> extends
super(client); super(client);
} }
command?: ModalCommand; command!: ModalCommand;
metadata: CommandMetadata<UnionToTuple<M>> = {} as never; metadata: CommandMetadata<UnionToTuple<M>> = {} as never;
globalMetadata: GlobalMetadata = {}; globalMetadata: GlobalMetadata = {};