@constatic/base 0.0.4 → 0.0.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/creators/commands/command.d.ts +3 -2
- package/dist/creators/commands/manager.d.ts +2 -0
- package/dist/creators/commands/manager.js +31 -19
- package/dist/creators/responders/responder.d.ts +1 -4
- package/dist/env.d.ts +1 -1
- package/dist/utils/types.d.ts +4 -0
- package/dist/version.js +1 -1
- package/package.json +3 -3
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { ApplicationCommandOptionType, ApplicationCommandType, AutocompleteInteraction, ChatInputCommandInteraction, InteractionContextType, MessageContextMenuCommandInteraction, UserContextMenuCommandInteraction, type ApplicationCommandOptionAllowedChannelTypes, type ApplicationCommandOptionChoiceData, type BaseApplicationCommandData, type CacheType, type LocalizationMap } from "discord.js";
|
|
2
|
-
|
|
2
|
+
import type { NotEmptyArray, UniqueArray } from "../../utils/types.js";
|
|
3
|
+
export type CommandType = ApplicationCommandType.ChatInput | ApplicationCommandType.Message | ApplicationCommandType.User;
|
|
3
4
|
type AutocompleData<Type> = Promise<readonly ApplicationCommandOptionChoiceData<Type extends (number | string) ? Type : (number | string)>[] | undefined | void>;
|
|
4
5
|
export type AutocompleteRun<Type, Contexts> = (this: void, interaction: AutocompleteInteraction<CacheMode<Contexts>>) => AutocompleData<Type>;
|
|
5
6
|
interface AutocompleteOptionData<Type, Contexts> {
|
|
@@ -66,7 +67,7 @@ type BaseAppCommandData = Omit<BaseApplicationCommandData, "contexts"> & Pick<Ba
|
|
|
66
67
|
export interface CommandData<T, Contexts, R> extends BaseAppCommandData {
|
|
67
68
|
name: string;
|
|
68
69
|
description?: string;
|
|
69
|
-
contexts?: Contexts
|
|
70
|
+
contexts?: NotEmptyArray<UniqueArray<Contexts>>;
|
|
70
71
|
type?: T;
|
|
71
72
|
global?: boolean;
|
|
72
73
|
run?(this: CommandRunThis, interaction: RunInteraction<T, Contexts>): Promise<R>;
|
|
@@ -9,7 +9,9 @@ export declare class CommandManager extends BaseManager {
|
|
|
9
9
|
private get config();
|
|
10
10
|
private readonly collection;
|
|
11
11
|
readonly runners: Map<string, Runner[]>;
|
|
12
|
+
readonly autocompleteRunners: Map<string, Runner>;
|
|
12
13
|
set<T, C extends readonly InteractionContextType[], R>(command: Command<T, C, R>): void;
|
|
14
|
+
getAutocompleteHandler(...path: (string | null)[]): Runner;
|
|
13
15
|
getTitle(type?: CommandType): string[];
|
|
14
16
|
build(): BuildedCommandData[];
|
|
15
17
|
private buildOptions;
|
|
@@ -10,12 +10,13 @@ class CommandManager extends BaseManager {
|
|
|
10
10
|
}
|
|
11
11
|
collection = new Map;
|
|
12
12
|
runners = new Map;
|
|
13
|
+
autocompleteRunners = new Map;
|
|
13
14
|
set(command) {
|
|
14
15
|
this.collection.set(command.data.name, command);
|
|
15
16
|
const path = `/${command.data.type}/${command.data.name}`;
|
|
16
17
|
this.runners.set(path, [command.data.run]);
|
|
17
18
|
if (command.data.autocomplete) {
|
|
18
|
-
this.
|
|
19
|
+
this.autocompleteRunners.set(path, command.data.autocomplete);
|
|
19
20
|
}
|
|
20
21
|
const [icon, label] = this.getTitle(command.data.type);
|
|
21
22
|
this.logs.push([
|
|
@@ -25,6 +26,12 @@ class CommandManager extends BaseManager {
|
|
|
25
26
|
styleText("green", "✓")
|
|
26
27
|
].join(" "));
|
|
27
28
|
}
|
|
29
|
+
getAutocompleteHandler(...path) {
|
|
30
|
+
const commandName = path[0];
|
|
31
|
+
const type = ApplicationCommandType.ChatInput;
|
|
32
|
+
const resolved = `/${type}/${path.filter((p) => p).join("/")}`;
|
|
33
|
+
return this.autocompleteRunners.get(resolved) ?? this.autocompleteRunners.get(`/${type}/${commandName}`);
|
|
34
|
+
}
|
|
28
35
|
getTitle(type) {
|
|
29
36
|
return {
|
|
30
37
|
[ApplicationCommandType.ChatInput]: ["{/}", "Slash command"],
|
|
@@ -60,7 +67,7 @@ class CommandManager extends BaseManager {
|
|
|
60
67
|
for (const option of options) {
|
|
61
68
|
const description = option.description ?? option.name;
|
|
62
69
|
if ("autocomplete" in option && option.autocomplete && typeof option.autocomplete === "function") {
|
|
63
|
-
this.
|
|
70
|
+
this.autocompleteRunners.set(`${path}/${option.name}`, option.autocomplete);
|
|
64
71
|
}
|
|
65
72
|
switch (option.type) {
|
|
66
73
|
case ApplicationCommandOptionType.SubcommandGroup: {
|
|
@@ -137,10 +144,9 @@ class CommandManager extends BaseManager {
|
|
|
137
144
|
}
|
|
138
145
|
async onAutocomplete(interaction) {
|
|
139
146
|
const options = interaction.options;
|
|
140
|
-
const
|
|
141
|
-
if (!
|
|
147
|
+
const run = this.getAutocompleteHandler(interaction.commandName, options.getSubcommandGroup(false), options.getSubcommand(false), options.getFocused(true).name);
|
|
148
|
+
if (!run)
|
|
142
149
|
return;
|
|
143
|
-
const [run] = handler;
|
|
144
150
|
const choices = await run(interaction);
|
|
145
151
|
if (choices && Array.isArray(choices)) {
|
|
146
152
|
await interaction.respond(choices.slice(0, 25));
|
|
@@ -190,26 +196,32 @@ class CommandManager extends BaseManager {
|
|
|
190
196
|
const promises = [];
|
|
191
197
|
const manager = client.application.commands;
|
|
192
198
|
const logs = [];
|
|
193
|
-
const setGlobal = (commands2) => {
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
`${client.user.username} application`
|
|
197
|
-
])));
|
|
199
|
+
const setGlobal = async (commands2) => {
|
|
200
|
+
const data = await manager.set(commands2);
|
|
201
|
+
logs.push([data.size, `${client.user.username} application`]);
|
|
198
202
|
};
|
|
199
|
-
if (guildList.size
|
|
200
|
-
const
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
203
|
+
if (guildList.size) {
|
|
204
|
+
const guildScopedCommands = [];
|
|
205
|
+
const globalCommands = [];
|
|
206
|
+
for (const command of commands) {
|
|
207
|
+
if (command.global) {
|
|
208
|
+
globalCommands.push(command);
|
|
209
|
+
} else {
|
|
210
|
+
guildScopedCommands.push(command);
|
|
211
|
+
}
|
|
212
|
+
}
|
|
213
|
+
for (const id of guildList) {
|
|
214
|
+
promises.push(manager.set(guildScopedCommands, id).then((registerdCommands) => logs.push([
|
|
215
|
+
registerdCommands.size,
|
|
204
216
|
`${id} guild`
|
|
205
217
|
])));
|
|
206
218
|
}
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
setGlobal(globalCommands);
|
|
219
|
+
if (globalCommands.length) {
|
|
220
|
+
promises.push(setGlobal(globalCommands));
|
|
210
221
|
}
|
|
211
222
|
} else {
|
|
212
|
-
setGlobal(commands);
|
|
223
|
+
promises.push(setGlobal(commands));
|
|
224
|
+
client.guilds.cache.forEach((guild) => guild.commands.set([]).catch(() => null));
|
|
213
225
|
}
|
|
214
226
|
await Promise.all(promises);
|
|
215
227
|
console.log(logs.map(([size, location]) => format(styleText("green", "└ %s command%s registered in %s"), size, size === 1 ? "" : "s", styleText("greenBright", location))).join(`
|
|
@@ -1,9 +1,6 @@
|
|
|
1
1
|
import type { ButtonInteraction, CacheType, ChannelSelectMenuInteraction, MentionableSelectMenuInteraction, ModalMessageModalSubmitInteraction, ModalSubmitInteraction, RoleSelectMenuInteraction, StringSelectMenuInteraction, UserSelectMenuInteraction } from "discord.js";
|
|
2
2
|
import { type InferRouteParams } from "rou3";
|
|
3
|
-
import type { Prettify } from "../../utils/types.js";
|
|
4
|
-
type NotEmptyArray<T> = T extends never[] ? never : T;
|
|
5
|
-
type UniqueArray<T> = T extends readonly [infer X, ...infer Rest] ? InArray<Rest, X> extends true ? ["Encountered value with duplicates:", X] : readonly [X, ...UniqueArray<Rest>] : T;
|
|
6
|
-
type InArray<T, X> = T extends readonly [X, ...infer _Rest] ? true : T extends readonly [X] ? true : T extends readonly [infer _, ...infer Rest] ? InArray<Rest, X> : false;
|
|
3
|
+
import type { NotEmptyArray, Prettify, UniqueArray } from "../../utils/types.js";
|
|
7
4
|
export declare enum ResponderType {
|
|
8
5
|
Button = "button",
|
|
9
6
|
StringSelect = "select.string",
|
package/dist/env.d.ts
CHANGED
package/dist/utils/types.d.ts
CHANGED
|
@@ -1,3 +1,7 @@
|
|
|
1
1
|
export type Prettify<T> = {
|
|
2
2
|
[K in keyof T]: T[K];
|
|
3
3
|
} & {};
|
|
4
|
+
export type NotEmptyArray<T> = T extends never[] ? never : T;
|
|
5
|
+
export type UniqueArray<T> = T extends readonly [infer X, ...infer Rest] ? InArray<Rest, X> extends true ? ["Encountered value with duplicates:", X] : readonly [X, ...UniqueArray<Rest>] : T;
|
|
6
|
+
type InArray<T, X> = T extends readonly [X, ...infer _Rest] ? true : T extends readonly [X] ? true : T extends readonly [infer _, ...infer Rest] ? InArray<Rest, X> : false;
|
|
7
|
+
export {};
|
package/dist/version.js
CHANGED
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@constatic/base",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.6",
|
|
4
4
|
"description": "Library with structures and functions for creating modern Discord applications.",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"type": "module",
|
|
@@ -33,7 +33,7 @@
|
|
|
33
33
|
],
|
|
34
34
|
"repository": {
|
|
35
35
|
"type": "git",
|
|
36
|
-
"url": "https://github.com/rinckodev/constatic",
|
|
36
|
+
"url": "git+https://github.com/rinckodev/constatic.git",
|
|
37
37
|
"directory": "packages/base"
|
|
38
38
|
},
|
|
39
39
|
"author": {
|
|
@@ -42,6 +42,7 @@
|
|
|
42
42
|
},
|
|
43
43
|
"devDependencies": {
|
|
44
44
|
"@constatic/config": "*",
|
|
45
|
+
"@standard-schema/spec": "^1.1.0",
|
|
45
46
|
"typescript": "^5.8.2"
|
|
46
47
|
},
|
|
47
48
|
"exports": {
|
|
@@ -66,7 +67,6 @@
|
|
|
66
67
|
"typegen": "tsc",
|
|
67
68
|
"build": "bun run ./build.ts",
|
|
68
69
|
"prepublish": "bun run build",
|
|
69
|
-
"publish": "npm publish",
|
|
70
70
|
"dev": "bun run ./playground/index.ts"
|
|
71
71
|
},
|
|
72
72
|
"peerDependencies": {
|