Skip to content

Commit f743648

Browse files
committed
feat: error catching in events and commands
1 parent 0791be4 commit f743648

File tree

4 files changed

+42
-13
lines changed

4 files changed

+42
-13
lines changed

.changeset/silly-goats-kiss.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'jellycommands': minor
3+
---
4+
5+
error catching in events and commands

packages/jellycommands/src/JellyCommands/commands/CommandManager.ts

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
import type { RESTPutAPIGuildApplicationCommandsPermissionsJSONBody } from 'discord-api-types/v9';
22
import { CommandCache, CommandIdResolver } from './CommandPersistance';
33
import type { APIApplicationCommand } from 'discord-api-types/v9';
4+
import { Command } from './types/commands/Command.js';
45
import type { JellyCommands } from '../JellyCommands';
56
import { getAuthDetails } from '../../util/token.js';
67
import { createRequest } from '../../util/request';
78
import { BaseCommand } from './base/BaseCommand';
89
import type { Interaction } from 'discord.js';
910
import { Routes } from 'discord-api-types/v9';
10-
import { Command } from './types/commands/Command.js';
1111

1212
export type CommandIDMap = Map<string, BaseCommand>;
1313
export type CommandMap = Map<string, BaseCommand>;
@@ -59,10 +59,18 @@ export class CommandManager {
5959
);
6060

6161
// Run the command
62-
command.run({
63-
client: this.client,
64-
interaction,
65-
});
62+
try {
63+
await command.run({
64+
client: this.client,
65+
interaction,
66+
});
67+
} catch (e) {
68+
console.error(
69+
`There was an error running command ${command.options.name}`,
70+
`interaction: ${interaction.id}, channel: ${interaction.channel}, guild: ${interaction.guild}`,
71+
e,
72+
);
73+
}
6674
}
6775

6876
static async readCommands(

packages/jellycommands/src/JellyCommands/events/Event.ts

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,23 @@ import { schema, EventOptions } from './options';
22
import { removeKeys } from 'ghoststools';
33

44
import type { JellyCommands } from '../JellyCommands';
5-
import type { Client, ClientEvents } from 'discord.js';
5+
import type { ClientEvents } from 'discord.js';
6+
7+
type Awaitable<T> = Promise<T> | T;
8+
9+
export type EventCallback<EventName extends keyof ClientEvents> = (
10+
instance: { client: JellyCommands },
11+
...args: ClientEvents[EventName]
12+
) => Awaitable<void>;
613

714
export class Event<T extends keyof ClientEvents> {
815
public readonly name: keyof ClientEvents;
9-
public readonly run: Function;
16+
public readonly run: EventCallback<T>;
1017
public readonly options: Required<EventOptions<T>>;
1118

1219
constructor(
1320
name: keyof ClientEvents,
14-
run: Function,
21+
run: Event<T>['run'],
1522
options: EventOptions<T>,
1623
) {
1724
this.name = name;
@@ -37,10 +44,7 @@ export class Event<T extends keyof ClientEvents> {
3744

3845
export const event = <K extends keyof ClientEvents>(
3946
options: EventOptions<K> & {
40-
run: (
41-
instance: { client: JellyCommands },
42-
...args: ClientEvents[K]
43-
) => void | any;
47+
run: EventCallback<K>;
4448
},
4549
) => {
4650
return new Event<K>(

packages/jellycommands/src/JellyCommands/events/EventManager.ts

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import type { JellyCommands } from '../JellyCommands';
2+
import type { ClientEvents } from 'discord.js';
23
import type { Event } from './Event';
34

45
export class EventManager {
@@ -9,7 +10,18 @@ export class EventManager {
910
for (const event of events) {
1011
if (event.options.disabled) continue;
1112

12-
const cb = (...ctx: any[]) => event.run({ client }, ...ctx);
13+
const cb = async <EventName extends keyof ClientEvents>(
14+
...ctx: ClientEvents[EventName]
15+
) => {
16+
try {
17+
await event.run({ client }, ...ctx);
18+
} catch (e) {
19+
console.error(
20+
`There was an error running event ${event.name}`,
21+
e,
22+
);
23+
}
24+
};
1325

1426
if (event.options.once) client.once(event.name, cb);
1527
else client.on(event.name, cb);

0 commit comments

Comments
 (0)