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 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 |
import type {
ChatInputCommandInteraction,
CommandInteraction,
UserContextMenuCommandInteraction,
MessageContextMenuCommandInteraction,
} from "discord.js";
import type { Option, InferOptions } from "./options";
export const INTEGRATION_TYPES = {
GUILD_INSTALL: 0,
USER_INSTALL: 1,
} as const;
export const INTERACTION_CONTEXTS = {
GUILD: 0,
BOT_DM: 1,
PRIVATE_CHANNEL: 2,
} as const;
export const INTEGRATION_TYPE_PRESETS = {
ALL: Object.values(INTEGRATION_TYPES),
GUILD_ONLY: [INTEGRATION_TYPES.GUILD_INSTALL],
USER_ONLY: [INTEGRATION_TYPES.USER_INSTALL],
};
export const INTEGRATION_CONTEXT_PRESETS = {
EVERYWHERE: Object.values(INTERACTION_CONTEXTS),
GUILDS_ONLY: [INTERACTION_CONTEXTS.GUILD],
DMS_ONLY: [INTERACTION_CONTEXTS.BOT_DM, INTERACTION_CONTEXTS.PRIVATE_CHANNEL],
};
export type IntegrationType = (typeof INTEGRATION_TYPES)[keyof typeof INTEGRATION_TYPES];
export type InteractionContextType =
(typeof INTERACTION_CONTEXTS)[keyof typeof INTERACTION_CONTEXTS];
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[] = [],
public config?: {
integrationTypes?: IntegrationType[];
contexts?: InteractionContextType[];
},
) {}
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>>;
integrationTypes?: IntegrationType[];
contexts?: InteractionContextType[];
run: Function;
},
public middlewares: Function[] = [],
) {}
}
export class ContextMenuCommand {
constructor(
public name: string,
public type: "user" | "message",
public run: Function,
public middlewares: Function[] = [],
public config?: {
integrationTypes?: IntegrationType[];
contexts?: InteractionContextType[];
},
) {}
}
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;
integrationTypes?: IntegrationType[];
contexts?: InteractionContextType[];
run: (
interaction: ChatInputCommandInteraction,
args: InferOptions<O>,
ctx: Ctx,
) => Promise<void> | void;
},
): Command;
command(
name: string,
config: {
description: string;
integrationTypes?: IntegrationType[];
contexts?: InteractionContextType[];
},
): 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, {
integrationTypes: config.integrationTypes,
contexts: config.contexts,
});
}
userContextMenu(
name: string,
runOrConfig:
| ((interaction: UserContextMenuCommandInteraction, ctx: Ctx) => Promise<void> | void)
| {
integrationTypes?: IntegrationType[];
contexts?: InteractionContextType[];
run: (interaction: UserContextMenuCommandInteraction, ctx: Ctx) => Promise<void> | void;
},
): ContextMenuCommand {
if (typeof runOrConfig === "function")
return new ContextMenuCommand(name, "user", runOrConfig, this.middlewares);
return new ContextMenuCommand(name, "user", runOrConfig.run, this.middlewares, {
integrationTypes: runOrConfig.integrationTypes,
contexts: runOrConfig.contexts,
});
}
messageContextMenu(
name: string,
runOrConfig:
| ((interaction: MessageContextMenuCommandInteraction, ctx: Ctx) => Promise<void> | void)
| {
integrationTypes?: IntegrationType[];
contexts?: InteractionContextType[];
run: (
interaction: MessageContextMenuCommandInteraction,
ctx: Ctx,
) => Promise<void> | void;
},
): ContextMenuCommand {
if (typeof runOrConfig === "function")
return new ContextMenuCommand(name, "message", runOrConfig, this.middlewares);
return new ContextMenuCommand(name, "message", runOrConfig.run, this.middlewares, {
integrationTypes: runOrConfig.integrationTypes,
contexts: runOrConfig.contexts,
});
}
}
|