Skip to content

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.

import {
class Command
Command
, type
class CommandContext<T extends OptionsRecord = {}, M extends keyof RegisteredMiddlewares = never>
interface CommandContext<T extends OptionsRecord = {}, M extends keyof RegisteredMiddlewares = never>
CommandContext
} from "seyfert";
export class
class HandlingErrors
HandlingErrors
extends
class Command
Command
{
async
HandlingErrors.run(context: CommandContext): Promise<void>
run
(
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
) {
throw new
var Error: ErrorConstructor
new (message?: string) => Error
Error
("Error, ehm, lol player detected");
}
Log: This responds with the previous error message: "Error, ehm, lol player detected"
async
HandlingErrors.onRunError(context: CommandContext, error: unknown): Promise<void>
onRunError
(
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: unknown
error
: unknown) {
context: CommandContext<{}, never>
context
.
CommandContext<{}, never>.client: UsingClient
client
.
BaseClient.logger: Logger
logger
.
Logger.fatal(...args: any[]): void

Logs a fatal error message.

@paramargs The arguments to log.

fatal
(
error: unknown
error
);
await
context: CommandContext<{}, never>
context
.
CommandContext<{}, never>.editOrReply<false>(body: InteractionCreateBodyRequest | InteractionMessageUpdateBodyRequest, withResponse?: false | undefined): Promise<...>
editOrReply
({
content: string
content
:
error: unknown
error
instanceof
var Error: ErrorConstructor
Error
?
error: Error
error
.
Error.message: string
message
: `Error: ${
error: unknown
error
}`
});
}
}

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

@sincev10.0.0

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

@sincev10.0.0

URL
(
data: {
context: CommandContext;
value: string;
}
data
.
value: string
value
));
Message: This will trigger the onOptionsError method
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
) {
Log: url: expected a valid URL
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

@paramo Object that contains the properties and methods. This can be an object that you created or an existing Document Object Model (DOM) 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.

@parampredicate A function that accepts up to three arguments. The filter method calls the predicate function one time for each element in the array.

@paramthisArg An object to which the this keyword can refer in the predicate function. If thisArg is omitted, undefined is used as the this value.

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.

@paramcallbackfn A function that accepts up to three arguments. The map method calls the callbackfn function one time for each element in the array.

@paramthisArg An object to which the this keyword can refer in the callbackfn function. If thisArg is omitted, undefined is used as the this value.

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.

@paramseparator A string used to separate one element of the array from the next in the resulting string. If omitted, the array elements are separated with a comma.

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.

@paramsearchElement The element to search for.

@paramfromIndex The position in this array at which to begin searching for searchElement.

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
({
Log: User is not a developer
content: string
content
:
error: string
error
});
}
}