Skip to content

Commit

Permalink
feat: help example command
Browse files Browse the repository at this point in the history
  • Loading branch information
zaida04 committed Mar 27, 2024
1 parent a8ebaa2 commit 1e05adb
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 4 deletions.
34 changes: 34 additions & 0 deletions packages/gil/__tests__/bot_mongo/commands/Help.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import { Command, CommandExecuteContext, GilClient } from "../../../lib";

export default class Help extends Command {
public constructor(client: GilClient) {
super(client, {
name: "help",
description: "Shows this help message.",
aliases: ["h"],
args: [
{
name: "command",
optional: true,
type: "string",
},
],
});
}

public async execute(ctx: CommandExecuteContext<{ command: string }>) {
const { command } = ctx.args;

if (command) {
const getCommand = this.gil.commands.getCommand(command);
if (!getCommand) {
return ctx.message.reply("Command not found.");
}

return ctx.message.reply(`Help for ${getCommand.options.name}`);
}

const allCommands = this.gil.commands.commands.map((command) => command.options.name);
return ctx.message.reply(`All commands: ${allCommands.join(", ")}`);
}
}
10 changes: 9 additions & 1 deletion packages/gil/lib/arguments/ArgumentParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,20 +43,28 @@ export async function convertArguments(params: {

for (let i = 0; i < commandArgs.length; i++) {
const currentArg = commandArgs[i];
const currentInput = params.args[i];

if (currentArg.optional && params.args.length <= i) {
castedArguments[currentArg.name] = null;
continue;
}

if (currentInput === undefined) {
if (currentArg.optional) {
castedArguments[currentArg.name] = null;
continue;
}
}

const validator = validators[currentArg.type].validate;
const validation_run = await validator({
mentionCounters,
rawArgs: params.args,
message: params.message,
argument: castedArguments[currentArg.name],
argIndex: i,
input: params.args[i],
input: currentInput,
});

if (validation_run.error) {
Expand Down
5 changes: 2 additions & 3 deletions packages/gil/lib/structures/Command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,9 @@ export abstract class Command {

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

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

export class CommandManager extends Manager {
Expand Down

0 comments on commit 1e05adb

Please sign in to comment.