From 5b11f2ed365c1bbe8f887d4008a4c2be9d824d5e Mon Sep 17 00:00:00 2001 From: "Zaid Arshad (Nico)" Date: Thu, 28 Mar 2024 15:14:00 -0400 Subject: [PATCH] feat: gil custom context --- packages/gil/__tests__/bot_sqlite/index.ts | 5 +++-- packages/gil/lib/GilClient.ts | 2 +- packages/gil/lib/listeners/CommandMessageListener.ts | 9 +++++++++ packages/gil/lib/structures/Command.ts | 5 ++++- 4 files changed, 17 insertions(+), 4 deletions(-) diff --git a/packages/gil/__tests__/bot_sqlite/index.ts b/packages/gil/__tests__/bot_sqlite/index.ts index 7911871f..a0a46eb7 100644 --- a/packages/gil/__tests__/bot_sqlite/index.ts +++ b/packages/gil/__tests__/bot_sqlite/index.ts @@ -4,14 +4,15 @@ import { GilClient } from "../../lib/GilClient"; import { BetterSQLite3Adapter } from "../../lib/adapters/db/BetterSQLite3Adapter"; import sqlite from "better-sqlite3"; -import { ConsoleAdapter } from "../../lib"; +import pino from "pino"; +import { PinoAdapter } from "../../lib"; const database = sqlite("test.db"); const YokiBot = new GilClient({ token: process.env.TOKEN!, commandDirectory: join(__dirname, "..", "shared", "commands"), listenerDirectory: join(__dirname, "listeners"), - loggingAdapter: new ConsoleAdapter(), + loggingAdapter: new PinoAdapter(pino({ base: null })), databaseAdapter: new BetterSQLite3Adapter({ sqliteInstance: database, serverTable: "servers", diff --git a/packages/gil/lib/GilClient.ts b/packages/gil/lib/GilClient.ts index a9e8b25e..46b9976f 100644 --- a/packages/gil/lib/GilClient.ts +++ b/packages/gil/lib/GilClient.ts @@ -12,7 +12,7 @@ import { TaskManager } from "./structures/Task"; interface GilClientOptions { token: string; clientOptions?: ClientOptions; - customContext?: unknown; + customCommandContext?: (data: { serverId: string; authorId: string; messageId: string }) => Promise>; // adapters loggingAdapter?: LoggerAdapter; databaseAdapter: DatabaseAdapter; diff --git a/packages/gil/lib/listeners/CommandMessageListener.ts b/packages/gil/lib/listeners/CommandMessageListener.ts index 144c08bc..953843fa 100644 --- a/packages/gil/lib/listeners/CommandMessageListener.ts +++ b/packages/gil/lib/listeners/CommandMessageListener.ts @@ -73,10 +73,19 @@ 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, + }) + : {}; + try { await command.execute({ message: params.message, args: attemptConvertArguments.arguments, + ...context, }); } catch (e) { // todo: user friendly error "something went wrong" message diff --git a/packages/gil/lib/structures/Command.ts b/packages/gil/lib/structures/Command.ts index 49a8a63f..38bbe4db 100644 --- a/packages/gil/lib/structures/Command.ts +++ b/packages/gil/lib/structures/Command.ts @@ -43,13 +43,16 @@ export abstract class Command { public readonly options: CommandOptions, ) {} - public abstract execute(commandContext: CommandExecuteContext): unknown | Promise; + public abstract execute(commandContext: CommandContext): unknown | Promise; } + export interface CommandExecuteContext> { message: Message; args: Args; } +export type CommandContext> = T & CommandExecuteContext; + export class CommandManager extends Manager { public readonly commands = new Collection();