Skip to content

Commit

Permalink
feat: rework guards and command execution
Browse files Browse the repository at this point in the history
  • Loading branch information
paradoxuum committed Feb 9, 2024
1 parent d043c44 commit 093175b
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 41 deletions.
5 changes: 1 addition & 4 deletions src/client/command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,6 @@ export class SharedCommand extends ExecutableCommand {
);
}

execute(interaction: CommandInteraction, args: string[]) {
return this.getCommandCallback(interaction, args)();
}

toString() {
return `SharedCommand{path=${this.path}}`;
}
Expand All @@ -49,6 +45,7 @@ export class ServerCommand extends BaseCommand {
const [success, data] = pcall(() =>
Remotes.Execute.InvokeServer(this.path.toString(), args.join(" ")),
);

if (!success) {
interaction.error("An error occurred.");
return;
Expand Down
48 changes: 17 additions & 31 deletions src/shared/core/command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,23 @@ export class ExecutableCommand extends BaseCommand {
}

execute(interaction: CommandInteraction, args: string[]) {
return this.getCommandCallback(interaction, args)();
for (const guard of this.guards) {
if (!guard(interaction)) return;
}

if (interaction.isReplyReceived()) return;

const transformedArgs = this.transformArgs(args);
if (transformedArgs.isErr()) {
interaction.error(transformedArgs.unwrapErr());
return;
}

return this.func(
this.commandClass,
interaction,
...transformedArgs.unwrap(),
);
}

transformArgs(args: string[]): Result<unknown[], string> {
Expand Down Expand Up @@ -182,36 +198,6 @@ export class ExecutableCommand extends BaseCommand {
toString() {
return `ExecutableCommand{path=${this.path}}`;
}

protected getCommandCallback(
interaction: CommandInteraction,
args: string[],
) {
const guardCount = this.guards.size();
let nextIndex = 0;
const runNext = () => {
if (nextIndex < guardCount) {
this.guards[nextIndex++](runNext, interaction);
return;
}

if (interaction.isReplyReceived()) return;

const transformedArgs = this.transformArgs(args);
if (transformedArgs.isErr()) {
interaction.error(transformedArgs.unwrapErr());
return;
}

return this.func(
this.commandClass,
interaction,
...transformedArgs.unwrap(),
);
};

return runNext;
}
}

export class CommandGroup {
Expand Down
6 changes: 4 additions & 2 deletions src/shared/core/dispatcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import { CommandPath } from "./path";
import { BaseRegistry } from "./registry";

const DEFAULT_REPLY_TEXT = "Command executed.";
const ERROR_TEXT = "An error occurred.";

export abstract class BaseDispatcher {
constructor(private readonly registry: BaseRegistry) {}
Expand All @@ -25,7 +24,10 @@ export abstract class BaseDispatcher {
const args = splitStringBySpace(text);

command.execute(interaction, args);
if (!interaction.isReplyReceived()) interaction.reply(DEFAULT_REPLY_TEXT);
if (!interaction.isReplyReceived()) {
interaction.reply(DEFAULT_REPLY_TEXT);
}

return interaction;
}
}
5 changes: 1 addition & 4 deletions src/shared/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,7 @@ import { Result } from "@rbxts/rust-classes";
import { t } from "@rbxts/t";
import { CommandInteraction } from "./core/interaction";

export type CommandGuard = (
runNext: () => void,
interaction: CommandInteraction,
) => void;
export type CommandGuard = (interaction: CommandInteraction) => boolean;

export interface CommanderOptions {
groups?: GroupOptions[];
Expand Down

0 comments on commit 093175b

Please sign in to comment.