@kevlid/discordmenus 0.0.2
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/LICENSE +15 -0
- package/README.md +60 -0
- package/index.d.ts +255 -0
- package/package.json +27 -0
- package/src/builder.js +50 -0
- package/src/handle/component.js +379 -0
- package/src/handle/modal.js +231 -0
- package/src/index.js +19 -0
- package/src/menuBuilder.js +157 -0
- package/src/menuInstance.js +334 -0
- package/src/menuManager.js +83 -0
- package/src/options/boolean.js +38 -0
- package/src/options/category.js +27 -0
- package/src/options/list.js +105 -0
- package/src/options/number.js +50 -0
- package/src/options/object.js +74 -0
- package/src/options/selectMenu.js +132 -0
- package/src/options/string.js +50 -0
- package/src/render/list.js +347 -0
- package/src/render/modal.js +76 -0
- package/src/render/object.js +417 -0
- package/src/render/page.js +142 -0
- package/src/render/selectMenu.js +1 -0
- package/src/render/types.js +1 -0
- package/src/render/utils.js +60 -0
- package/src/utils/adapters.js +98 -0
- package/src/utils/componentsValidate.js +23 -0
- package/src/utils/constants.js +11 -0
- package/src/utils/customIds.js +67 -0
- package/src/utils/formatKey.js +1 -0
- package/src/utils/helpers.js +100 -0
- package/src/utils/parseModal.js +1 -0
- package/src/utils/storage.js +131 -0
- package/src/utils/types.js +34 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
ISC License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026
|
|
4
|
+
|
|
5
|
+
Permission to use, copy, modify, and/or distribute this software for any
|
|
6
|
+
purpose with or without fee is hereby granted, provided that the above
|
|
7
|
+
copyright notice and this permission notice appear in all copies.
|
|
8
|
+
|
|
9
|
+
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
|
|
10
|
+
REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
|
|
11
|
+
AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
|
|
12
|
+
INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
|
|
13
|
+
LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
|
|
14
|
+
OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
|
|
15
|
+
PERFORMANCE OF THIS SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
# @kevlid/discordmenus
|
|
2
|
+
|
|
3
|
+
```js
|
|
4
|
+
const fs = require("fs");
|
|
5
|
+
const path = require("path");
|
|
6
|
+
const { Client, GatewayIntentBits, REST, Routes, SlashCommandBuilder } = require("discord.js");
|
|
7
|
+
const { MenuManager, MenuBuilder, CustomIdPrefix, fromDiscordJS } = require("@kevlid/discordmenus");
|
|
8
|
+
|
|
9
|
+
const DATA = path.join(__dirname, "menu-data.json");
|
|
10
|
+
const store = new Map(
|
|
11
|
+
Object.entries(fs.existsSync(DATA) ? JSON.parse(fs.readFileSync(DATA, "utf8")) : { prefix: "!" }),
|
|
12
|
+
);
|
|
13
|
+
|
|
14
|
+
function persist() {
|
|
15
|
+
fs.writeFileSync(DATA, JSON.stringify(Object.fromEntries(store), null, 2));
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
const builder = new MenuBuilder()
|
|
19
|
+
.setKey("settings")
|
|
20
|
+
.setTitle("Settings")
|
|
21
|
+
.createCategory(c => c.setKey("main"))
|
|
22
|
+
.addString(o => o.setKey("prefix").setTitle("Prefix").setMinLength(1).setMaxLength(5).setPreview(true));
|
|
23
|
+
|
|
24
|
+
const manager = new MenuManager();
|
|
25
|
+
|
|
26
|
+
const client = new Client({ intents: [GatewayIntentBits.Guilds] });
|
|
27
|
+
|
|
28
|
+
client.once("ready", async () => {
|
|
29
|
+
await manager.registerMenu(builder, {
|
|
30
|
+
get: (key) => store.get(key),
|
|
31
|
+
save: (key, value) => {
|
|
32
|
+
store.set(key, value);
|
|
33
|
+
persist();
|
|
34
|
+
},
|
|
35
|
+
});
|
|
36
|
+
await new REST().setToken(process.env.BOT_TOKEN).put(
|
|
37
|
+
Routes.applicationGuildCommands(client.user.id, process.env.GUILD_ID),
|
|
38
|
+
{ body: [new SlashCommandBuilder().setName("menu").setDescription("Open").toJSON()] },
|
|
39
|
+
);
|
|
40
|
+
});
|
|
41
|
+
|
|
42
|
+
client.on("interactionCreate", async (interaction) => {
|
|
43
|
+
if (interaction.isChatInputCommand() && interaction.commandName === "menu") {
|
|
44
|
+
await interaction.deferReply();
|
|
45
|
+
await interaction.editReply(
|
|
46
|
+
await manager.renderMenu("settings", { userId: interaction.user.id }),
|
|
47
|
+
);
|
|
48
|
+
return;
|
|
49
|
+
}
|
|
50
|
+
if (interaction.customId?.startsWith(CustomIdPrefix)) {
|
|
51
|
+
const body = await manager.handleInteraction(fromDiscordJS(interaction));
|
|
52
|
+
await interaction.client.rest.post(
|
|
53
|
+
Routes.interactionCallback(interaction.id, interaction.token),
|
|
54
|
+
{ body },
|
|
55
|
+
);
|
|
56
|
+
}
|
|
57
|
+
});
|
|
58
|
+
|
|
59
|
+
client.login(process.env.BOT_TOKEN);
|
|
60
|
+
```
|
package/index.d.ts
ADDED
|
@@ -0,0 +1,255 @@
|
|
|
1
|
+
export type GetFunction = (
|
|
2
|
+
key: string,
|
|
3
|
+
options?: Record<string, unknown>,
|
|
4
|
+
) => unknown | Promise<unknown>;
|
|
5
|
+
|
|
6
|
+
export type SaveFunction = (
|
|
7
|
+
key: string,
|
|
8
|
+
value: unknown,
|
|
9
|
+
options?: Record<string, unknown>,
|
|
10
|
+
) => unknown | Promise<unknown>;
|
|
11
|
+
|
|
12
|
+
export type PermissionResolvable = string | number | bigint;
|
|
13
|
+
|
|
14
|
+
export interface MenuManagerOptions {
|
|
15
|
+
get?: GetFunction;
|
|
16
|
+
save?: SaveFunction;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
export interface RenderMenuOptions {
|
|
20
|
+
category?: number;
|
|
21
|
+
index?: number;
|
|
22
|
+
userId?: string;
|
|
23
|
+
guildId?: string;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
export interface DiscordInteraction {
|
|
27
|
+
type: number;
|
|
28
|
+
id?: string;
|
|
29
|
+
token?: string;
|
|
30
|
+
applicationId?: string;
|
|
31
|
+
guildId?: string;
|
|
32
|
+
guildID?: string;
|
|
33
|
+
guild_id?: string;
|
|
34
|
+
user?: { id: string; [key: string]: unknown };
|
|
35
|
+
member?: {
|
|
36
|
+
user?: { id: string; [key: string]: unknown };
|
|
37
|
+
permissions?: string | bigint | number | { allow?: string | number | bigint } | null;
|
|
38
|
+
permission?: string | bigint | number | { allow?: string | number | bigint } | null;
|
|
39
|
+
[key: string]: unknown;
|
|
40
|
+
};
|
|
41
|
+
memberPermissions?: string | bigint | null;
|
|
42
|
+
data?: {
|
|
43
|
+
custom_id?: string;
|
|
44
|
+
customID?: string;
|
|
45
|
+
values?: string[];
|
|
46
|
+
components?: unknown[];
|
|
47
|
+
[key: string]: unknown;
|
|
48
|
+
};
|
|
49
|
+
customId?: string;
|
|
50
|
+
custom_id?: string;
|
|
51
|
+
values?: string[];
|
|
52
|
+
components?: unknown[];
|
|
53
|
+
[key: string]: unknown;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
export interface DiscordComponent {
|
|
57
|
+
type: number;
|
|
58
|
+
[key: string]: unknown;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
export interface RenderedMenu {
|
|
62
|
+
flags: number;
|
|
63
|
+
components: DiscordComponent[];
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
export interface InteractionResponse {
|
|
67
|
+
type: number;
|
|
68
|
+
data?: {
|
|
69
|
+
flags?: number;
|
|
70
|
+
content?: string;
|
|
71
|
+
title?: string;
|
|
72
|
+
custom_id?: string;
|
|
73
|
+
components?: DiscordComponent[];
|
|
74
|
+
[key: string]: unknown;
|
|
75
|
+
};
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
export declare class Builder {
|
|
79
|
+
readonly type: string;
|
|
80
|
+
readonly key: string | null;
|
|
81
|
+
readonly title: string | null;
|
|
82
|
+
readonly description: string | null;
|
|
83
|
+
readonly required: boolean;
|
|
84
|
+
setKey(key: string): this;
|
|
85
|
+
setTitle(title: string): this;
|
|
86
|
+
setDescription(description: string): this;
|
|
87
|
+
setRequired(required: boolean): this;
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
export declare class BooleanBuilder extends Builder {
|
|
91
|
+
setTrueLabel(label: string): this;
|
|
92
|
+
setFalseLabel(label: string): this;
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
export declare class StringBuilder extends Builder {
|
|
96
|
+
setPreview(enabled: boolean): this;
|
|
97
|
+
setMaxLength(length: number): this;
|
|
98
|
+
setMinLength(length: number): this;
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
export declare class NumberBuilder extends Builder {
|
|
102
|
+
setPreview(enabled: boolean): this;
|
|
103
|
+
setMaxValue(value: number): this;
|
|
104
|
+
setMinValue(value: number): this;
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
export declare class CategoryBuilder extends Builder {}
|
|
108
|
+
|
|
109
|
+
export declare class StringListBuilder extends Builder {
|
|
110
|
+
setMinLength(length: number): this;
|
|
111
|
+
setMaxLength(length: number): this;
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
export declare class NumberListBuilder extends Builder {
|
|
115
|
+
setMinValue(value: number): this;
|
|
116
|
+
setMaxValue(value: number): this;
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
export declare class ObjectListBuilder extends Builder {
|
|
120
|
+
addString(callback: (builder: StringListBuilder) => void): this;
|
|
121
|
+
addNumber(callback: (builder: NumberListBuilder) => void): this;
|
|
122
|
+
addBoolean(callback: (builder: BooleanBuilder) => void): this;
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
export declare class ObjectBuilder extends Builder {
|
|
126
|
+
setPreview(enabled: boolean): this;
|
|
127
|
+
addString(callback: (builder: StringBuilder) => void): this;
|
|
128
|
+
addNumber(callback: (builder: NumberBuilder) => void): this;
|
|
129
|
+
addBoolean(callback: (builder: BooleanBuilder) => void): this;
|
|
130
|
+
addStringList(callback: (builder: StringListBuilder) => void): this;
|
|
131
|
+
addNumberList(callback: (builder: NumberListBuilder) => void): this;
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
export declare class StringSelectChoiceBuilder {
|
|
135
|
+
setDescription(desc: string): this;
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
export declare class StringSelectBuilder extends Builder {
|
|
139
|
+
setPlaceholder(text: string): this;
|
|
140
|
+
setMinValues(n: number): this;
|
|
141
|
+
setMaxValues(n: number): this;
|
|
142
|
+
addChoice(
|
|
143
|
+
label: string,
|
|
144
|
+
value: string,
|
|
145
|
+
callback?: (choice: StringSelectChoiceBuilder) => void,
|
|
146
|
+
): this;
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
export declare class UserSelectBuilder extends Builder {
|
|
150
|
+
setPlaceholder(text: string): this;
|
|
151
|
+
setMinValues(n: number): this;
|
|
152
|
+
setMaxValues(n: number): this;
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
export declare class RoleSelectBuilder extends Builder {
|
|
156
|
+
setPlaceholder(text: string): this;
|
|
157
|
+
setMinValues(n: number): this;
|
|
158
|
+
setMaxValues(n: number): this;
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
export declare class MentionableSelectBuilder extends Builder {
|
|
162
|
+
setPlaceholder(text: string): this;
|
|
163
|
+
setMinValues(n: number): this;
|
|
164
|
+
setMaxValues(n: number): this;
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
export declare class ChannelSelectBuilder extends Builder {
|
|
168
|
+
setPlaceholder(text: string): this;
|
|
169
|
+
setMinValues(n: number): this;
|
|
170
|
+
setMaxValues(n: number): this;
|
|
171
|
+
addChannelType(channelType: number): this;
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
export declare class MenuBuilder extends Builder {
|
|
175
|
+
setGetMethod(method: GetFunction): this;
|
|
176
|
+
setSaveMethod(method: SaveFunction): this;
|
|
177
|
+
setPermissions(permissions: (PermissionResolvable | PermissionResolvable[])[]): this;
|
|
178
|
+
createCategory(callback: (builder: CategoryBuilder) => void): this;
|
|
179
|
+
addString(callback: (builder: StringBuilder) => void): this;
|
|
180
|
+
addNumber(callback: (builder: NumberBuilder) => void): this;
|
|
181
|
+
addBoolean(callback: (builder: BooleanBuilder) => void): this;
|
|
182
|
+
addStringList(callback: (builder: StringListBuilder) => void): this;
|
|
183
|
+
addNumberList(callback: (builder: NumberListBuilder) => void): this;
|
|
184
|
+
addObject(callback: (builder: ObjectBuilder) => void): this;
|
|
185
|
+
addObjectList(callback: (builder: ObjectListBuilder) => void): this;
|
|
186
|
+
addStringSelect(callback: (builder: StringSelectBuilder) => void): this;
|
|
187
|
+
addUserSelect(callback: (builder: UserSelectBuilder) => void): this;
|
|
188
|
+
addRoleSelect(callback: (builder: RoleSelectBuilder) => void): this;
|
|
189
|
+
addMentionableSelect(callback: (builder: MentionableSelectBuilder) => void): this;
|
|
190
|
+
addChannelSelect(callback: (builder: ChannelSelectBuilder) => void): this;
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
export declare class MenuManager {
|
|
194
|
+
registerMenu(menu: MenuBuilder, options?: MenuManagerOptions): Promise<void>;
|
|
195
|
+
renderMenu(key: string, options?: RenderMenuOptions): Promise<RenderedMenu>;
|
|
196
|
+
menuFromCustomId(customId: string): { menu: unknown; menu_key: string };
|
|
197
|
+
handleComponent(interaction: DiscordInteraction): Promise<InteractionResponse>;
|
|
198
|
+
handleModal(interaction: DiscordInteraction): Promise<InteractionResponse>;
|
|
199
|
+
handleInteraction(interaction: DiscordInteraction): Promise<InteractionResponse>;
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
export interface StorageContext {
|
|
203
|
+
guildId?: string | null;
|
|
204
|
+
userId?: string | null;
|
|
205
|
+
[key: string]: unknown;
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
export interface MenuStorageOptions {
|
|
209
|
+
cache?: boolean;
|
|
210
|
+
cacheTTL?: number;
|
|
211
|
+
cacheMaxSize?: number;
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
export interface StorageHandler {
|
|
215
|
+
get: GetFunction;
|
|
216
|
+
save: SaveFunction;
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
export declare class MenuStorage {
|
|
220
|
+
constructor(options?: MenuStorageOptions);
|
|
221
|
+
get(key: string, ctx?: StorageContext): Promise<unknown>;
|
|
222
|
+
save(key: string, value: unknown, ctx?: StorageContext): Promise<void>;
|
|
223
|
+
cacheClear(guildId?: string): void;
|
|
224
|
+
handler(): StorageHandler;
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
export declare class MemoryMenuStorage extends MenuStorage {
|
|
228
|
+
data: Map<string, unknown>;
|
|
229
|
+
get(key: string, ctx?: StorageContext): Promise<unknown>;
|
|
230
|
+
save(key: string, value: unknown, ctx?: StorageContext): Promise<void>;
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
export declare const CustomIdPrefix: string;
|
|
234
|
+
export declare const EphemeralMessageFlag: number;
|
|
235
|
+
|
|
236
|
+
export declare const OptionTypes: {
|
|
237
|
+
readonly Menu: string;
|
|
238
|
+
readonly Category: string;
|
|
239
|
+
readonly String: string;
|
|
240
|
+
readonly Number: string;
|
|
241
|
+
readonly Boolean: string;
|
|
242
|
+
readonly List: string;
|
|
243
|
+
readonly Object: string;
|
|
244
|
+
readonly StringSelect: string;
|
|
245
|
+
readonly UserSelect: string;
|
|
246
|
+
readonly RoleSelect: string;
|
|
247
|
+
readonly MentionableSelect: string;
|
|
248
|
+
readonly ChannelSelect: string;
|
|
249
|
+
};
|
|
250
|
+
|
|
251
|
+
export declare function normalizeInteraction(
|
|
252
|
+
interaction: DiscordInteraction,
|
|
253
|
+
): DiscordInteraction;
|
|
254
|
+
export declare function fromEris(interaction: DiscordInteraction): DiscordInteraction;
|
|
255
|
+
export declare function fromDiscordJS(interaction: DiscordInteraction): DiscordInteraction;
|
package/package.json
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@kevlid/discordmenus",
|
|
3
|
+
"version": "0.0.2",
|
|
4
|
+
"description": "Components v2 settings menus for Discord bots",
|
|
5
|
+
"main": "src/index.js",
|
|
6
|
+
"types": "index.d.ts",
|
|
7
|
+
"author": "kevlid",
|
|
8
|
+
"license": "ISC",
|
|
9
|
+
"keywords": [
|
|
10
|
+
"discord",
|
|
11
|
+
"discord.js",
|
|
12
|
+
"menu",
|
|
13
|
+
"typescript",
|
|
14
|
+
"builder"
|
|
15
|
+
],
|
|
16
|
+
"files": [
|
|
17
|
+
"src",
|
|
18
|
+
"index.d.ts",
|
|
19
|
+
"README.md",
|
|
20
|
+
"LICENSE"
|
|
21
|
+
],
|
|
22
|
+
"dependencies": {
|
|
23
|
+
"@kevlid/customids": "^1.0.0",
|
|
24
|
+
"discord.js": "^14.25.1",
|
|
25
|
+
"eris": "^0.18.0"
|
|
26
|
+
}
|
|
27
|
+
}
|
package/src/builder.js
ADDED
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
const { formatKey } = require("./utils/formatKey");
|
|
2
|
+
|
|
3
|
+
class Builder {
|
|
4
|
+
constructor(type, cost) {
|
|
5
|
+
if (typeof type !== "string") throw new TypeError("Type must be a string");
|
|
6
|
+
this.type = type;
|
|
7
|
+
this.key = null;
|
|
8
|
+
this.title = null;
|
|
9
|
+
this.description = null;
|
|
10
|
+
this.required = false;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
setKey(key) {
|
|
14
|
+
this.key = formatKey(String(key));
|
|
15
|
+
return this;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
setTitle(title) {
|
|
19
|
+
this.title = String(title);
|
|
20
|
+
return this;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
setDescription(description) {
|
|
24
|
+
this.description = String(description);
|
|
25
|
+
return this;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
setRequired(required) {
|
|
29
|
+
this.required = Boolean(required);
|
|
30
|
+
return this;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
build() {
|
|
34
|
+
if (!this.key) throw new Error(this.type + " must have a key");
|
|
35
|
+
if (!this.title) {
|
|
36
|
+
this.title = this.key;
|
|
37
|
+
}
|
|
38
|
+
return {
|
|
39
|
+
type: this.type,
|
|
40
|
+
key: this.key,
|
|
41
|
+
title: this.title,
|
|
42
|
+
description: this.description,
|
|
43
|
+
required: this.required,
|
|
44
|
+
};
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
module.exports = {
|
|
49
|
+
Builder
|
|
50
|
+
}
|