Skip to content

Commit

Permalink
feat: gil custom context
Browse files Browse the repository at this point in the history
  • Loading branch information
zaida04 committed Mar 28, 2024
1 parent 265273c commit 5b11f2e
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 4 deletions.
5 changes: 3 additions & 2 deletions packages/gil/__tests__/bot_sqlite/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
2 changes: 1 addition & 1 deletion packages/gil/lib/GilClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<Record<string, unknown>>;
// adapters
loggingAdapter?: LoggerAdapter;
databaseAdapter: DatabaseAdapter;
Expand Down
9 changes: 9 additions & 0 deletions packages/gil/lib/listeners/CommandMessageListener.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 4 additions & 1 deletion packages/gil/lib/structures/Command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,16 @@ export abstract class Command {
public readonly options: CommandOptions,
) {}

public abstract execute(commandContext: CommandExecuteContext): unknown | Promise<unknown>;
public abstract execute(commandContext: CommandContext<unknown, unknown>): unknown | Promise<unknown>;
}

export interface CommandExecuteContext<Args = Record<string, CommandArgument>> {
message: Message;
args: Args;
}

export type CommandContext<T, Args = Record<string, CommandArgument>> = T & CommandExecuteContext<Args>;

export class CommandManager extends Manager {
public readonly commands = new Collection<string, Command>();

Expand Down

0 comments on commit 5b11f2e

Please sign in to comment.