Skip to content

Extending Command

The command class is the one that provides all the utilities inside a command and prepares it to receive all the command metadata.

Handling errors

Within Seyfert, there are different types of errors, depending on how critical the problem is.

Run throw error

1
import { Command, type CommandContext } from "seyfert";
2
3
export class HandlingErrors extends Command {
4
async run(context: CommandContext) {
5
throw new Error("Error, ehm, lol player detected");
6
}
7
8
// This will reply with the error message above "Error, ehm, lol player detected"
9
async onRunError(context: CommandContext, error: unknown) {
10
context.client.logger.fatal(error);
11
await context.editOrReply({
12
content: error instanceof Error ? error.message : `Error: ${error}`
13
});
14
}
15
}

Options throw error

1
import {
2
Command,
3
createStringOption,
4
Options,
5
type CommandContext,
6
type OnOptionsReturnObject,
7
type OKFunction
8
} from "seyfert";
9
10
@Options({
11
url: createStringOption({
12
description: 'how to be a gamer',
13
value(data, ok: OKFunction<URL>, fail) {
14
if (isUrl(data.value)) return ok(new URL(data.value));
15
fail('expected a valid url');
16
}
17
})
18
})
19
export class HandlingErrors extends Command {
20
// url: expected a valid url
21
async onOptionsError(
22
context: CommandContext,
23
metadata: OnOptionsReturnObject
24
) {
25
await context.editOrReply({
26
content: Object.entries(metadata)
27
.filter((_) => _[1].failed)
28
.map((error) => `${error[0]}: ${error[1].value}`)
29
.join("\n")
30
});
31
}
32
}

Middleware return stop

When a middleware returns a stop, seyfert issues this error and stops the progress of the command to be handled.

1
export default createMiddleware(({ context, next, stop, pass }) => {
2
if (!Devs.includes(context.author.id)) return stop("User is not a developer");
3
next();
4
});
1
import { Command, Middlewares, type CommandContext } from "seyfert";
2
3
@Middlewares(["OnlyDev"])
4
export class HandlingErrors extends Command {
5
async onMiddlewaresError(context: CommandContext, error: Error) {
6
await context.editOrReply({
7
//User is not a developer
8
content: error.message
9
});
10
}
11
}