Extending Command
With Seyfert, you can handle errors in an organized way and treat them differently depending on the type of error.
Error when executing a command
This is the most common error and occurs when an error is thrown in the run
method.
Error when validating options
This error is thrown when an option fails in the value
method.
const const options: { url: { readonly type: ApplicationCommandOptionType.String; readonly required?: boolean | undefined; readonly choices?: SeyfertChoice<...>[] | undefined; ... 8 more ...; readonly max_length?: number; };}
options = { url: { readonly type: ApplicationCommandOptionType.String; readonly required?: boolean | undefined; readonly choices?: SeyfertChoice<...>[] | undefined; ... 8 more ...; readonly max_length?: number;}
url: createStringOption<boolean, SeyfertChoice<string>[], URL>(data: SeyfertStringOption<SeyfertChoice<string>[], boolean, URL>): { readonly type: ApplicationCommandOptionType.String; ... 10 more ...; readonly max_length?: number;}
createStringOption({ SeyfertBaseChoiceableOption<T extends keyof ReturnOptionsTypes, C = T extends ChoiceableTypes ? SeyfertChoice<ChoiceableValues[T]>[] : never, R = boolean, VC = never>.description: string
description: 'how to be a gamer', SeyfertBaseChoiceableOption<ApplicationCommandOptionType.String, SeyfertChoice<string>[], boolean, URL>.value?: ValueCallback<ApplicationCommandOptionType.String, SeyfertChoice<string>[], URL> | undefined
value(data: { context: CommandContext; value: string;}
data, ok: OKFunction<URL>
ok: type OKFunction<T> = (value: T) => void
OKFunction<interface URL
URL
class is a global reference for import { URL } from 'url'
https://nodejs.org/api/url.html#the-whatwg-url-api
URL>, fail: StopFunction
fail) { if (const isURL: (url: string) => boolean
isURL(data: { context: CommandContext; value: string;}
data.value: string
value)) return ok: (value: URL) => void
ok(new var URL: new (input: string | { toString: () => string;}, base?: string | URL) => URL
URL
class is a global reference for import { URL } from 'url'
https://nodejs.org/api/url.html#the-whatwg-url-api
URL(data: { context: CommandContext; value: string;}
data.value: string
value));
Message: fail: (error: string) => void
fail('expected a valid URL'); }, })};
@function Options(options: (new () => SubCommand)[] | OptionsRecord): <T extends { new (...args: any[]): object;}>(target: T) => { new (...args: any[]): { options: SubCommand[] | CommandOption[]; };} & T
Options(const options: { url: { readonly type: ApplicationCommandOptionType.String; readonly required?: boolean | undefined; readonly choices?: SeyfertChoice<...>[] | undefined; ... 8 more ...; readonly max_length?: number; };}
options)export class class HandlingErrors
HandlingErrors extends class Command
Command { async HandlingErrors.onOptionsError(context: CommandContext, metadata: OnOptionsReturnObject): Promise<void>
onOptionsError( context: CommandContext<{}, never>
context: class CommandContext<T extends OptionsRecord = {}, M extends keyof RegisteredMiddlewares = never>interface CommandContext<T extends OptionsRecord = {}, M extends keyof RegisteredMiddlewares = never>
CommandContext, metadata: OnOptionsReturnObject
metadata: type OnOptionsReturnObject = { [x: string]: { failed: false; value: unknown; } | { failed: true; value: string; parseError: MessageCommandOptionErrors | undefined; };}
OnOptionsReturnObject ) { await context: CommandContext<{}, never>
context.CommandContext<{}, never>.editOrReply<false>(body: InteractionCreateBodyRequest | InteractionMessageUpdateBodyRequest, withResponse?: false | undefined): Promise<...>
editOrReply({ content: string
content: var Object: ObjectConstructor
Provides functionality common to all JavaScript objects.
Object.ObjectConstructor.entries<{ failed: false; value: unknown;} | { failed: true; value: string; parseError: MessageCommandOptionErrors | undefined;}>(o: { ...;} | ArrayLike<...>): [...][] (+1 overload)
Returns an array of key/values of the enumerable own properties of an object
entries(metadata: OnOptionsReturnObject
metadata) .Array<[string, { failed: false; value: unknown; } | { failed: true; value: string; parseError: MessageCommandOptionErrors | undefined; }]>.filter(predicate: (value: [string, { failed: false; value: unknown;} | { failed: true; value: string; parseError: MessageCommandOptionErrors | undefined;}], index: number, array: [...][]) => unknown, thisArg?: any): [...][] (+1 overload)
Returns the elements of an array that meet the condition specified in a callback function.
filter((_: [string, { failed: false; value: unknown;} | { failed: true; value: string; parseError: MessageCommandOptionErrors | undefined;}]
_) => _: [string, { failed: false; value: unknown;} | { failed: true; value: string; parseError: MessageCommandOptionErrors | undefined;}]
_[1].failed: boolean
failed) .Array<[string, { failed: false; value: unknown; } | { failed: true; value: string; parseError: MessageCommandOptionErrors | undefined; }]>.map<string>(callbackfn: (value: [string, { failed: false; value: unknown;} | { failed: true; value: string; parseError: MessageCommandOptionErrors | undefined;}], index: number, array: [...][]) => string, thisArg?: any): string[]
Calls a defined callback function on each element of an array, and returns an array that contains the results.
map((error: [string, { failed: false; value: unknown;} | { failed: true; value: string; parseError: MessageCommandOptionErrors | undefined;}]
error) => `${error: [string, { failed: false; value: unknown;} | { failed: true; value: string; parseError: MessageCommandOptionErrors | undefined;}]
error[0]}: ${error: [string, { failed: false; value: unknown;} | { failed: true; value: string; parseError: MessageCommandOptionErrors | undefined;}]
error[1].value: unknown
value}`) .Array<string>.join(separator?: string): string
Adds all the elements of an array into a string, separated by the specified separator string.
join("\n") }); }}
Stop a middleware with an error
When a middleware returns a stop, Seyfert generates this error and stops the progress of the command being handled.
import { function createMiddleware<T = any, C extends AnyContext = AnyContext>(data: MiddlewareContext<T, C>): MiddlewareContext<T, C>
createMiddleware } from "seyfert";
export default createMiddleware<void, AnyContext>(data: MiddlewareContext<void, AnyContext>): MiddlewareContext<void, AnyContext>
createMiddleware<void>(({ context: AnyContext
context, next: () => void
next, stop: StopFunction
stop, pass: PassFunction
pass }) => { if (!const Devs: string[]
Devs.Array<string>.includes(searchElement: string, fromIndex?: number): boolean
Determines whether an array includes a certain element, returning true or false as appropriate.
includes(context: AnyContext
context.author: User
Gets the author of the interaction.
author.DiscordBase<APIUser>.id: string
id)) { return stop: (error: string) => void
stop("User is not a developer"); } next: () => void
next();});
import { class Command
Command, function Middlewares(cbs: readonly (keyof RegisteredMiddlewares)[]): <T extends { new (...args: any[]): object;}>(target: T) => { new (...args: any[]): { middlewares: readonly never[]; };} & T
Middlewares, type class CommandContext<T extends OptionsRecord = {}, M extends keyof RegisteredMiddlewares = never>interface CommandContext<T extends OptionsRecord = {}, M extends keyof RegisteredMiddlewares = never>
CommandContext } from "seyfert";
@function Middlewares(cbs: readonly (keyof RegisteredMiddlewares)[]): <T extends { new (...args: any[]): object;}>(target: T) => { new (...args: any[]): { middlewares: readonly never[]; };} & T
Middlewares(["OnlyDev"])export class class HandlingErrors
HandlingErrors extends class Command
Command { async HandlingErrors.onMiddlewaresError(context: CommandContext, error: string): Promise<void>
onMiddlewaresError(context: CommandContext<{}, never>
context: class CommandContext<T extends OptionsRecord = {}, M extends keyof RegisteredMiddlewares = never>interface CommandContext<T extends OptionsRecord = {}, M extends keyof RegisteredMiddlewares = never>
CommandContext, error: string
error: string) { await context: CommandContext<{}, never>
context.CommandContext<{}, never>.editOrReply<false>(body: InteractionCreateBodyRequest | InteractionMessageUpdateBodyRequest, withResponse?: false | undefined): Promise<...>
editOrReply({ content: string
content: error: string
error }); }}