Skip to content

Commit

Permalink
feat: add some context and error handler utils
Browse files Browse the repository at this point in the history
  • Loading branch information
zaida04 committed Mar 28, 2024
1 parent 5b11f2e commit f0988da
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 6 deletions.
8 changes: 7 additions & 1 deletion packages/gil/lib/GilClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,17 @@ import { GilEvents } from "./events";
import { CommandManager } from "./structures/Command";
import { ListenerManager } from "./structures/ListenerManager";
import { TaskManager } from "./structures/Task";
import { CommandCustomContextFn, CommandErrorHandler } from "./types";

interface GilClientOptions {
token: string;
clientOptions?: ClientOptions;
customCommandContext?: (data: { serverId: string; authorId: string; messageId: string }) => Promise<Record<string, unknown>>;
contexts?: {
command: CommandCustomContextFn;
};
errorHandler?: {
command: CommandErrorHandler;
};
// adapters
loggingAdapter?: LoggerAdapter;
databaseAdapter: DatabaseAdapter;
Expand Down
9 changes: 9 additions & 0 deletions packages/gil/lib/events.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Member, Message } from "guilded.js";
import { StoredServer } from "./adapters/db/DatabaseAdapter";
import { Command } from "./structures/Command";

export interface NonCommandMessageParams {
message: Message;
Expand All @@ -14,7 +15,15 @@ export interface CommandMessageParams {
prefix: string;
}

export interface CommandRanParams {
message: Message;
member: Member;
server: StoredServer;
command: Command;
}

export type GilEvents = {
nonCommandMessage(params: NonCommandMessageParams): unknown;
commandMessage(params: CommandMessageParams): unknown;
commandRan(params: CommandRanParams): unknown;
};
1 change: 1 addition & 0 deletions packages/gil/lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,4 @@ export * from "./adapters/db/PostgresAdapter";
export * from "./adapters/db/BetterSQLite3Adapter";
// export * from "./adapters/logging/WinstonAdapter";
export * from "./adapters/logging/PinoAdapter";
export * from "./types";
22 changes: 17 additions & 5 deletions packages/gil/lib/listeners/CommandMessageListener.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,14 +73,20 @@ export default class CommandMessageListener extends Listener {
return;
}

const context = this.gil.options.customCommandContext
? await this.gil.options.customCommandContext({
serverId: params.server.server_id,
authorId: params.member.id,
messageId: params.message.id,
const context = this.gil.options?.contexts?.command
? await this.gil.options.contexts.command({
server: params.server,
message: params.message,
})
: {};

this.gil.emitter.emit("commandRan", {
message: params.message,
member: params.member,
server: params.server,
command,
});

try {
await command.execute({
message: params.message,
Expand All @@ -92,6 +98,12 @@ export default class CommandMessageListener extends Listener {

this.gil.logger.error(e as Error);
this.gil.logger.warn(`Error executing command ${name}`, params.message.id);
this.gil.options.errorHandler?.command?.(e as Error, {
message: params.message,
member: params.member,
server: params.server,
command,
});
}
}

Expand Down
6 changes: 6 additions & 0 deletions packages/gil/lib/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import type { Member, Message } from "guilded.js";
import type { StoredServer } from "./adapters/db/DatabaseAdapter";
import type { Command } from "./structures/Command";

export type CommandCustomContextFn = (data: { server: StoredServer; message: Message }) => Promise<Record<string, unknown>>;
export type CommandErrorHandler = (error: Error, additionalDetails: { message: Message; member: Member; server: StoredServer; command: Command }) => void;

0 comments on commit f0988da

Please sign in to comment.