pkgs/core/src/commands.ts (view raw)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 |
import type {
ChatInputCommandInteraction,
CommandInteraction,
UserContextMenuCommandInteraction,
MessageContextMenuCommandInteraction,
} from "discord.js";
import type { Option, InferOptions } from "./options";
export class Subcommand {
constructor(
public name: string,
public config: {
description: string;
options?: Record<string, Option<any, any>>;
run: Function;
},
) {}
}
export class SubcommandGroup<Ctx = {}> {
public subcommands: Record<string, Subcommand> = {};
constructor(
public name: string,
public description: string,
) {}
subcommand<O extends Record<string, Option<any, any>>>(
name: string,
config: {
description: string;
options?: O;
run: (
interaction: ChatInputCommandInteraction,
args: InferOptions<O>,
ctx: Ctx,
) => Promise<void> | void;
},
): this {
this.subcommands[name] = new Subcommand(name, config);
return this;
}
}
export class SubcommandGroupBuilder<Ctx = {}> {
public subcommands: Record<string, Subcommand> = {};
public groups: Record<string, SubcommandGroup<Ctx>> = {};
constructor(
public name: string,
public description: string,
public middlewares: Function[] = [],
) {}
subcommand<O extends Record<string, Option<any, any>>>(
name: string,
config: {
description: string;
options?: O;
run: (
interaction: ChatInputCommandInteraction,
args: InferOptions<O>,
ctx: Ctx,
) => Promise<void> | void;
},
): this {
this.subcommands[name] = new Subcommand(name, config);
return this;
}
group(
name: string,
config: { description: string },
setup: (group: SubcommandGroup<Ctx>) => void,
): this {
const group = new SubcommandGroup<Ctx>(name, config.description);
setup(group);
this.groups[name] = group;
return this;
}
}
export class Command {
constructor(
public name: string,
public config: {
description: string;
options?: Record<string, Option<any, any>>;
run: Function;
},
public middlewares: Function[] = [],
) {}
}
export class ContextMenuCommand {
constructor(
public name: string,
public type: "user" | "message",
public run: Function,
public middlewares: Function[] = [],
) {}
}
export class CommandBuilder<Ctx = {}> {
constructor(private middlewares: Function[] = []) {}
use<NewCtx extends Record<string, any>>(
mw: (interaction: CommandInteraction, ctx: Ctx) => Promise<NewCtx | void> | NewCtx | void,
): CommandBuilder<Ctx & NewCtx> {
return new CommandBuilder<Ctx & NewCtx>([...this.middlewares, mw]);
}
command<O extends Record<string, Option<any, any>>>(
name: string,
config: {
description: string;
options?: O;
run: (
interaction: ChatInputCommandInteraction,
args: InferOptions<O>,
ctx: Ctx,
) => Promise<void> | void;
},
): Command;
command(name: string, config: { description: string }): SubcommandGroupBuilder<Ctx>;
command(name: string, config: any): any {
if (config.run) {
return new Command(name, config, this.middlewares);
}
return new SubcommandGroupBuilder<Ctx>(name, config.description, this.middlewares);
}
userContextMenu(
name: string,
run: (interaction: UserContextMenuCommandInteraction, ctx: Ctx) => Promise<void> | void,
): ContextMenuCommand {
return new ContextMenuCommand(name, "user", run, this.middlewares);
}
messageContextMenu(
name: string,
run: (interaction: MessageContextMenuCommandInteraction, ctx: Ctx) => Promise<void> | void,
): ContextMenuCommand {
return new ContextMenuCommand(name, "message", run, this.middlewares);
}
}
|