commandkit 0.1.10-dev.20240108063947 → 0.1.10-dev.20240121151012
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/index.js +10 -1273
- package/dist/index.mjs +10 -1234
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -1,1273 +1,10 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
var
|
|
9
|
-
|
|
10
|
-
__defProp(target, name, { get: all[name], enumerable: true });
|
|
11
|
-
};
|
|
12
|
-
var __copyProps = (to, from, except, desc) => {
|
|
13
|
-
if (from && typeof from === "object" || typeof from === "function") {
|
|
14
|
-
for (let key of __getOwnPropNames(from))
|
|
15
|
-
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
16
|
-
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
17
|
-
}
|
|
18
|
-
return to;
|
|
19
|
-
};
|
|
20
|
-
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
21
|
-
// If the importer is in node compatibility mode or this is not an ESM
|
|
22
|
-
// file that has been converted to a CommonJS file using a Babel-
|
|
23
|
-
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
24
|
-
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
25
|
-
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
26
|
-
mod
|
|
27
|
-
));
|
|
28
|
-
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
29
|
-
|
|
30
|
-
// src/index.ts
|
|
31
|
-
var src_exports = {};
|
|
32
|
-
__export(src_exports, {
|
|
33
|
-
ButtonKit: () => ButtonKit,
|
|
34
|
-
CommandKit: () => CommandKit,
|
|
35
|
-
createEffect: () => createEffect,
|
|
36
|
-
createSignal: () => createSignal,
|
|
37
|
-
defineConfig: () => defineConfig,
|
|
38
|
-
getConfig: () => getConfig,
|
|
39
|
-
response: () => response,
|
|
40
|
-
useChannel: () => useChannel,
|
|
41
|
-
useClient: () => useClient,
|
|
42
|
-
useCommandData: () => useCommandData,
|
|
43
|
-
useCommandKit: () => useCommandKit,
|
|
44
|
-
useGuild: () => useGuild,
|
|
45
|
-
useInteraction: () => useInteraction,
|
|
46
|
-
useMember: () => useMember,
|
|
47
|
-
useUser: () => useUser
|
|
48
|
-
});
|
|
49
|
-
module.exports = __toCommonJS(src_exports);
|
|
50
|
-
|
|
51
|
-
// src/utils/resolve-file-url.ts
|
|
52
|
-
var import_path = __toESM(require("path"));
|
|
53
|
-
function toFileURL(filePath) {
|
|
54
|
-
const resolvedPath = import_path.default.resolve(filePath);
|
|
55
|
-
return "file://" + resolvedPath.replace(/\\\\|\\/g, "/");
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
// src/utils/get-paths.ts
|
|
59
|
-
var import_path2 = __toESM(require("path"));
|
|
60
|
-
var import_promises = __toESM(require("fs/promises"));
|
|
61
|
-
async function getFilePaths(directory, nesting) {
|
|
62
|
-
let filePaths = [];
|
|
63
|
-
if (!directory)
|
|
64
|
-
return filePaths;
|
|
65
|
-
const files = await import_promises.default.readdir(directory, { withFileTypes: true });
|
|
66
|
-
for (const file of files) {
|
|
67
|
-
const filePath = import_path2.default.join(directory, file.name);
|
|
68
|
-
if (file.isFile()) {
|
|
69
|
-
filePaths.push(filePath);
|
|
70
|
-
}
|
|
71
|
-
if (nesting && file.isDirectory()) {
|
|
72
|
-
filePaths = [...filePaths, ...await getFilePaths(filePath, true)];
|
|
73
|
-
}
|
|
74
|
-
}
|
|
75
|
-
return filePaths;
|
|
76
|
-
}
|
|
77
|
-
async function getFolderPaths(directory, nesting) {
|
|
78
|
-
let folderPaths = [];
|
|
79
|
-
if (!directory)
|
|
80
|
-
return folderPaths;
|
|
81
|
-
const folders = await import_promises.default.readdir(directory, { withFileTypes: true });
|
|
82
|
-
for (const folder of folders) {
|
|
83
|
-
const folderPath = import_path2.default.join(directory, folder.name);
|
|
84
|
-
if (folder.isDirectory()) {
|
|
85
|
-
folderPaths.push(folderPath);
|
|
86
|
-
if (nesting) {
|
|
87
|
-
folderPaths = [...folderPaths, ...await getFolderPaths(folderPath, true)];
|
|
88
|
-
}
|
|
89
|
-
}
|
|
90
|
-
}
|
|
91
|
-
return folderPaths;
|
|
92
|
-
}
|
|
93
|
-
|
|
94
|
-
// src/utils/clone.ts
|
|
95
|
-
var import_rfdc = __toESM(require("rfdc"));
|
|
96
|
-
var clone = (0, import_rfdc.default)();
|
|
97
|
-
|
|
98
|
-
// src/utils/colors.ts
|
|
99
|
-
var resetColor = "\x1B[0m";
|
|
100
|
-
var colors_default = {
|
|
101
|
-
reset: (text) => `${text}${resetColor}`,
|
|
102
|
-
bright: (text) => `\x1B[1m${text}${resetColor}`,
|
|
103
|
-
dim: (text) => `\x1B[2m${text}${resetColor}`,
|
|
104
|
-
underscore: (text) => `\x1B[4m${text}${resetColor}`,
|
|
105
|
-
blink: (text) => `\x1B[5m${text}${resetColor}`,
|
|
106
|
-
reverse: (text) => `\x1B[7m${text}${resetColor}`,
|
|
107
|
-
hidden: (text) => `\x1B[8m${text}${resetColor}`,
|
|
108
|
-
black: (text) => `\x1B[30m${text}${resetColor}`,
|
|
109
|
-
red: (text) => `\x1B[31m${text}${resetColor}`,
|
|
110
|
-
green: (text) => `\x1B[32m${text}${resetColor}`,
|
|
111
|
-
yellow: (text) => `\x1B[33m${text}${resetColor}`,
|
|
112
|
-
blue: (text) => `\x1B[34m${text}${resetColor}`,
|
|
113
|
-
magenta: (text) => `\x1B[35m${text}${resetColor}`,
|
|
114
|
-
cyan: (text) => `\x1B[36m${text}${resetColor}`,
|
|
115
|
-
white: (text) => `\x1B[37m${text}${resetColor}`,
|
|
116
|
-
bgBlack: (text) => `\x1B[40m${text}${resetColor}`,
|
|
117
|
-
bgRed: (text) => `\x1B[41m${text}${resetColor}`,
|
|
118
|
-
bgGreen: (text) => `\x1B[42m${text}${resetColor}`,
|
|
119
|
-
bgYellow: (text) => `\x1B[43m${text}${resetColor}`,
|
|
120
|
-
bgBlue: (text) => `\x1B[44m${text}${resetColor}`,
|
|
121
|
-
bgMagenta: (text) => `\x1B[45m${text}${resetColor}`,
|
|
122
|
-
bgCyan: (text) => `\x1B[46m${text}${resetColor}`,
|
|
123
|
-
bgWhite: (text) => `\x1B[47m${text}${resetColor}`
|
|
124
|
-
};
|
|
125
|
-
|
|
126
|
-
// src/handlers/command-handler/functions/loadCommandsWithRest.ts
|
|
127
|
-
async function loadCommandsWithRest(props) {
|
|
128
|
-
if (props.reloading) {
|
|
129
|
-
if (props.client.isReady()) {
|
|
130
|
-
await handleLoading(
|
|
131
|
-
props.client,
|
|
132
|
-
props.commands,
|
|
133
|
-
props.devGuildIds,
|
|
134
|
-
props.reloading,
|
|
135
|
-
props.type
|
|
136
|
-
);
|
|
137
|
-
} else {
|
|
138
|
-
throw new Error(colors_default.red(`Cannot reload commands when client is not ready.`));
|
|
139
|
-
}
|
|
140
|
-
} else {
|
|
141
|
-
props.client.once("ready", async (c) => {
|
|
142
|
-
await handleLoading(c, props.commands, props.devGuildIds, props.reloading, props.type);
|
|
143
|
-
});
|
|
144
|
-
}
|
|
145
|
-
}
|
|
146
|
-
async function handleLoading(client, commands, devGuildIds, reloading, type) {
|
|
147
|
-
commands = commands.filter((cmd) => !cmd.options?.deleted);
|
|
148
|
-
const devOnlyCommands = commands.filter((cmd) => cmd.options?.devOnly);
|
|
149
|
-
const globalCommands = commands.filter((cmd) => !cmd.options?.devOnly);
|
|
150
|
-
if (type === "dev") {
|
|
151
|
-
await loadDevCommands(client, devOnlyCommands, devGuildIds, reloading);
|
|
152
|
-
} else if (type === "global") {
|
|
153
|
-
await loadGlobalCommands(client, globalCommands, reloading);
|
|
154
|
-
} else {
|
|
155
|
-
await loadDevCommands(client, devOnlyCommands, devGuildIds, reloading);
|
|
156
|
-
await loadGlobalCommands(client, globalCommands, reloading);
|
|
157
|
-
}
|
|
158
|
-
}
|
|
159
|
-
async function loadGlobalCommands(client, commands, reloading) {
|
|
160
|
-
const requestBody = commands.map((cmd) => cmd.data);
|
|
161
|
-
await client.application.commands.set(requestBody).catch((error) => {
|
|
162
|
-
throw new Error(
|
|
163
|
-
colors_default.red(
|
|
164
|
-
`Error ${reloading ? "reloading" : "loading"} global application commands.
|
|
165
|
-
`
|
|
166
|
-
),
|
|
167
|
-
error
|
|
168
|
-
);
|
|
169
|
-
});
|
|
170
|
-
console.log(
|
|
171
|
-
colors_default.green(`${reloading ? "Reloaded" : "Loaded"} ${requestBody.length} global commands.`)
|
|
172
|
-
);
|
|
173
|
-
}
|
|
174
|
-
async function loadDevCommands(client, commands, guildIds, reloading) {
|
|
175
|
-
const requestBody = commands.map((cmd) => cmd.data);
|
|
176
|
-
for (const guildId of guildIds) {
|
|
177
|
-
const targetGuild = client.guilds.cache.get(guildId) || await client.guilds.fetch(guildId);
|
|
178
|
-
if (!targetGuild) {
|
|
179
|
-
process.emitWarning(
|
|
180
|
-
`Cannot ${reloading ? "reload" : "load"} commands in guild "${guildId}" - guild doesn't exist or client isn't part of the guild.`
|
|
181
|
-
);
|
|
182
|
-
continue;
|
|
183
|
-
}
|
|
184
|
-
await targetGuild.commands.set(requestBody).catch((error) => {
|
|
185
|
-
throw new Error(
|
|
186
|
-
colors_default.red(
|
|
187
|
-
`Error ${reloading ? "reloading" : "loading"} developer application commands in guild "${targetGuild?.name || guildId}".
|
|
188
|
-
`
|
|
189
|
-
),
|
|
190
|
-
error
|
|
191
|
-
);
|
|
192
|
-
});
|
|
193
|
-
console.log(
|
|
194
|
-
colors_default.green(
|
|
195
|
-
`${reloading ? "Reloaded" : "Loaded"} ${requestBody.length} developer commands in guild "${targetGuild.name}".`
|
|
196
|
-
)
|
|
197
|
-
);
|
|
198
|
-
}
|
|
199
|
-
}
|
|
200
|
-
|
|
201
|
-
// src/handlers/command-handler/utils/areSlashCommandsDifferent.ts
|
|
202
|
-
function areSlashCommandsDifferent(appCommand, localCommand) {
|
|
203
|
-
if (!appCommand.options)
|
|
204
|
-
appCommand.options = [];
|
|
205
|
-
if (!localCommand.options)
|
|
206
|
-
localCommand.options = [];
|
|
207
|
-
if (!appCommand.description)
|
|
208
|
-
appCommand.description = "";
|
|
209
|
-
if (!localCommand.description)
|
|
210
|
-
localCommand.description = "";
|
|
211
|
-
if (localCommand.description !== appCommand.description || localCommand.options.length !== appCommand.options.length) {
|
|
212
|
-
return true;
|
|
213
|
-
}
|
|
214
|
-
}
|
|
215
|
-
|
|
216
|
-
// src/handlers/command-handler/functions/registerCommands.ts
|
|
217
|
-
async function registerCommands(props) {
|
|
218
|
-
if (props.reloading) {
|
|
219
|
-
if (props.client.isReady()) {
|
|
220
|
-
await handleRegistration(props.client, props.commands, props.devGuildIds, props.type);
|
|
221
|
-
} else {
|
|
222
|
-
throw new Error(colors_default.red(`Cannot reload commands when client is not ready.`));
|
|
223
|
-
}
|
|
224
|
-
} else {
|
|
225
|
-
props.client.once("ready", async (c) => {
|
|
226
|
-
await handleRegistration(c, props.commands, props.devGuildIds, props.type);
|
|
227
|
-
});
|
|
228
|
-
}
|
|
229
|
-
}
|
|
230
|
-
async function handleRegistration(client, commands, devGuildIds, type) {
|
|
231
|
-
const devOnlyCommands = commands.filter((cmd) => cmd.options?.devOnly);
|
|
232
|
-
const globalCommands = commands.filter((cmd) => !cmd.options?.devOnly);
|
|
233
|
-
if (type === "dev") {
|
|
234
|
-
await registerDevCommands(client, devOnlyCommands, devGuildIds);
|
|
235
|
-
} else if (type === "global") {
|
|
236
|
-
await registerGlobalCommands(client, globalCommands);
|
|
237
|
-
} else {
|
|
238
|
-
await registerDevCommands(client, devOnlyCommands, devGuildIds);
|
|
239
|
-
await registerGlobalCommands(client, globalCommands);
|
|
240
|
-
}
|
|
241
|
-
}
|
|
242
|
-
async function registerGlobalCommands(client, commands) {
|
|
243
|
-
const appCommandsManager = client.application.commands;
|
|
244
|
-
await appCommandsManager.fetch();
|
|
245
|
-
for (const command of commands) {
|
|
246
|
-
const targetCommand = appCommandsManager.cache.find(
|
|
247
|
-
(cmd) => cmd.name === command.data.name
|
|
248
|
-
);
|
|
249
|
-
if (command.options?.deleted) {
|
|
250
|
-
if (!targetCommand) {
|
|
251
|
-
process.emitWarning(
|
|
252
|
-
colors_default.yellow(
|
|
253
|
-
`Ignoring: Command "${command.data.name}" is globally marked as deleted.`
|
|
254
|
-
)
|
|
255
|
-
);
|
|
256
|
-
} else {
|
|
257
|
-
await targetCommand.delete().catch((error) => {
|
|
258
|
-
throw new Error(
|
|
259
|
-
colors_default.red(`Failed to delete command "${command.data.name}" globally.
|
|
260
|
-
`),
|
|
261
|
-
error
|
|
262
|
-
);
|
|
263
|
-
});
|
|
264
|
-
console.log(colors_default.green(`Deleted command "${command.data.name}" globally.`));
|
|
265
|
-
}
|
|
266
|
-
continue;
|
|
267
|
-
}
|
|
268
|
-
if (targetCommand) {
|
|
269
|
-
const commandsAreDifferent = areSlashCommandsDifferent(targetCommand, command.data);
|
|
270
|
-
if (commandsAreDifferent) {
|
|
271
|
-
await targetCommand.edit(command.data).catch((error) => {
|
|
272
|
-
throw new Error(
|
|
273
|
-
colors_default.red(`Failed to edit command "${command.data.name}" globally.
|
|
274
|
-
`),
|
|
275
|
-
error
|
|
276
|
-
);
|
|
277
|
-
});
|
|
278
|
-
console.log(colors_default.green(`Edited command "${command.data.name}" globally.`));
|
|
279
|
-
continue;
|
|
280
|
-
}
|
|
281
|
-
}
|
|
282
|
-
if (targetCommand)
|
|
283
|
-
continue;
|
|
284
|
-
await appCommandsManager.create(command.data).catch((error) => {
|
|
285
|
-
throw new Error(
|
|
286
|
-
colors_default.red(`Failed to register command "${command.data.name}" globally.
|
|
287
|
-
`),
|
|
288
|
-
error
|
|
289
|
-
);
|
|
290
|
-
});
|
|
291
|
-
console.log(colors_default.green(`Registered command "${command.data.name}" globally.`));
|
|
292
|
-
}
|
|
293
|
-
}
|
|
294
|
-
async function registerDevCommands(client, commands, guildIds) {
|
|
295
|
-
const devGuilds = [];
|
|
296
|
-
for (const guildId of guildIds) {
|
|
297
|
-
const guild = client.guilds.cache.get(guildId) || await client.guilds.fetch(guildId);
|
|
298
|
-
if (!guild) {
|
|
299
|
-
process.emitWarning(
|
|
300
|
-
colors_default.yellow(
|
|
301
|
-
`Ignoring: Guild ${guildId} doesn't exist or client isn't part of the guild.`
|
|
302
|
-
)
|
|
303
|
-
);
|
|
304
|
-
continue;
|
|
305
|
-
}
|
|
306
|
-
devGuilds.push(guild);
|
|
307
|
-
}
|
|
308
|
-
const guildCommandsManagers = [];
|
|
309
|
-
for (const guild of devGuilds) {
|
|
310
|
-
const guildCommandsManager = guild.commands;
|
|
311
|
-
await guildCommandsManager.fetch();
|
|
312
|
-
guildCommandsManagers.push(guildCommandsManager);
|
|
313
|
-
}
|
|
314
|
-
for (const command of commands) {
|
|
315
|
-
for (const guildCommands of guildCommandsManagers) {
|
|
316
|
-
const targetCommand = guildCommands.cache.find((cmd) => cmd.name === command.data.name);
|
|
317
|
-
if (command.options?.deleted) {
|
|
318
|
-
if (!targetCommand) {
|
|
319
|
-
process.emitWarning(
|
|
320
|
-
colors_default.yellow(
|
|
321
|
-
`Ignoring: Command "${command.data.name}" is marked as deleted in ${guildCommands.guild.name}.`
|
|
322
|
-
)
|
|
323
|
-
);
|
|
324
|
-
} else {
|
|
325
|
-
await targetCommand.delete().catch((error) => {
|
|
326
|
-
throw new Error(
|
|
327
|
-
colors_default.red(
|
|
328
|
-
`Failed to delete command "${command.data.name}" in ${guildCommands.guild.name}.`
|
|
329
|
-
),
|
|
330
|
-
error
|
|
331
|
-
);
|
|
332
|
-
});
|
|
333
|
-
console.log(
|
|
334
|
-
colors_default.green(
|
|
335
|
-
`Deleted command "${command.data.name}" in ${guildCommands.guild.name}.`
|
|
336
|
-
)
|
|
337
|
-
);
|
|
338
|
-
}
|
|
339
|
-
continue;
|
|
340
|
-
}
|
|
341
|
-
if (targetCommand) {
|
|
342
|
-
const commandsAreDifferent = areSlashCommandsDifferent(targetCommand, command.data);
|
|
343
|
-
if (commandsAreDifferent) {
|
|
344
|
-
await targetCommand.edit(command.data).catch((error) => {
|
|
345
|
-
throw new Error(
|
|
346
|
-
colors_default.red(
|
|
347
|
-
`Failed to edit command "${command.data.name}" in ${guildCommands.guild.name}.
|
|
348
|
-
`
|
|
349
|
-
),
|
|
350
|
-
error
|
|
351
|
-
);
|
|
352
|
-
});
|
|
353
|
-
console.log(
|
|
354
|
-
colors_default.green(
|
|
355
|
-
`Edited command "${command.data.name}" in ${guildCommands.guild.name}.`
|
|
356
|
-
)
|
|
357
|
-
);
|
|
358
|
-
continue;
|
|
359
|
-
}
|
|
360
|
-
}
|
|
361
|
-
if (targetCommand)
|
|
362
|
-
continue;
|
|
363
|
-
await guildCommands.create(command.data).catch((error) => {
|
|
364
|
-
throw new Error(
|
|
365
|
-
colors_default.red(
|
|
366
|
-
`Failed to register command "${command.data.name}" in ${guildCommands.guild.name}.
|
|
367
|
-
`
|
|
368
|
-
),
|
|
369
|
-
error
|
|
370
|
-
);
|
|
371
|
-
});
|
|
372
|
-
console.log(
|
|
373
|
-
colors_default.green(
|
|
374
|
-
`Registered command "${command.data.name}" in ${guildCommands.guild.name}.`
|
|
375
|
-
)
|
|
376
|
-
);
|
|
377
|
-
}
|
|
378
|
-
}
|
|
379
|
-
}
|
|
380
|
-
|
|
381
|
-
// src/handlers/command-handler/validations/devOnly.ts
|
|
382
|
-
function devOnly_default({ interaction, targetCommand, handlerData }) {
|
|
383
|
-
if (interaction.isAutocomplete())
|
|
384
|
-
return;
|
|
385
|
-
if (targetCommand.options?.devOnly) {
|
|
386
|
-
if (interaction.inGuild() && !handlerData.devGuildIds.includes(interaction.guildId)) {
|
|
387
|
-
interaction.reply({
|
|
388
|
-
content: "\u274C This command can only be used inside development servers.",
|
|
389
|
-
ephemeral: true
|
|
390
|
-
});
|
|
391
|
-
return true;
|
|
392
|
-
}
|
|
393
|
-
const guildMember = interaction.guild?.members.cache.get(interaction.user.id);
|
|
394
|
-
const memberRoles = guildMember?.roles.cache;
|
|
395
|
-
let hasDevRole = false;
|
|
396
|
-
memberRoles?.forEach((role) => {
|
|
397
|
-
if (handlerData.devRoleIds.includes(role.id)) {
|
|
398
|
-
hasDevRole = true;
|
|
399
|
-
}
|
|
400
|
-
});
|
|
401
|
-
const isDevUser = handlerData.devUserIds.includes(interaction.user.id) || hasDevRole;
|
|
402
|
-
if (!isDevUser) {
|
|
403
|
-
interaction.reply({
|
|
404
|
-
content: "\u274C This command can only be used by developers.",
|
|
405
|
-
ephemeral: true
|
|
406
|
-
});
|
|
407
|
-
return true;
|
|
408
|
-
}
|
|
409
|
-
}
|
|
410
|
-
}
|
|
411
|
-
|
|
412
|
-
// src/handlers/command-handler/validations/permissions.ts
|
|
413
|
-
var import_discord = require("discord.js");
|
|
414
|
-
function permissions_default({ interaction, targetCommand }) {
|
|
415
|
-
if (interaction.isAutocomplete())
|
|
416
|
-
return;
|
|
417
|
-
const userPermissions = interaction.memberPermissions;
|
|
418
|
-
let userPermissionsRequired = targetCommand.options?.userPermissions;
|
|
419
|
-
let missingUserPermissions = [];
|
|
420
|
-
if (typeof userPermissionsRequired === "string") {
|
|
421
|
-
userPermissionsRequired = [userPermissionsRequired];
|
|
422
|
-
}
|
|
423
|
-
const botPermissions = interaction.guild?.members.me?.permissions;
|
|
424
|
-
let botPermissionsRequired = targetCommand.options?.botPermissions;
|
|
425
|
-
let missingBotPermissions = [];
|
|
426
|
-
if (typeof botPermissionsRequired === "string") {
|
|
427
|
-
botPermissionsRequired = [botPermissionsRequired];
|
|
428
|
-
}
|
|
429
|
-
if (!userPermissionsRequired?.length && !botPermissionsRequired?.length) {
|
|
430
|
-
return;
|
|
431
|
-
}
|
|
432
|
-
if (userPermissions && userPermissionsRequired) {
|
|
433
|
-
for (const permission of userPermissionsRequired) {
|
|
434
|
-
const hasPermission = userPermissions.has(permission);
|
|
435
|
-
if (!hasPermission) {
|
|
436
|
-
missingUserPermissions.push(permission);
|
|
437
|
-
}
|
|
438
|
-
}
|
|
439
|
-
}
|
|
440
|
-
if (botPermissions && botPermissionsRequired) {
|
|
441
|
-
for (const permission of botPermissionsRequired) {
|
|
442
|
-
const hasPermission = botPermissions.has(permission);
|
|
443
|
-
if (!hasPermission) {
|
|
444
|
-
missingBotPermissions.push(permission);
|
|
445
|
-
}
|
|
446
|
-
}
|
|
447
|
-
}
|
|
448
|
-
if (!missingUserPermissions.length && !missingBotPermissions.length) {
|
|
449
|
-
return;
|
|
450
|
-
}
|
|
451
|
-
const pattern = /([a-z])([A-Z])|([A-Z]+)([A-Z][a-z])/g;
|
|
452
|
-
missingUserPermissions = missingUserPermissions.map((str) => str.replace(pattern, "$1$3 $2$4"));
|
|
453
|
-
missingBotPermissions = missingBotPermissions.map((str) => str.replace(pattern, "$1$3 $2$4"));
|
|
454
|
-
let embedDescription = "";
|
|
455
|
-
const formatter = new Intl.ListFormat("en", { style: "long", type: "conjunction" });
|
|
456
|
-
const getPermissionWord = (permissions) => permissions.length === 1 ? "permission" : "permissions";
|
|
457
|
-
if (missingUserPermissions.length) {
|
|
458
|
-
const formattedPermissions = missingUserPermissions.map((p) => `\`${p}\``);
|
|
459
|
-
const permissionsString = formatter.format(formattedPermissions);
|
|
460
|
-
embedDescription += `- You must have the ${permissionsString} ${getPermissionWord(
|
|
461
|
-
missingUserPermissions
|
|
462
|
-
)} to be able to run this command.
|
|
463
|
-
`;
|
|
464
|
-
}
|
|
465
|
-
if (missingBotPermissions.length) {
|
|
466
|
-
const formattedPermissions = missingBotPermissions.map((p) => `\`${p}\``);
|
|
467
|
-
const permissionsString = formatter.format(formattedPermissions);
|
|
468
|
-
embedDescription += `- I must have the ${permissionsString} ${getPermissionWord(
|
|
469
|
-
missingBotPermissions
|
|
470
|
-
)} to be able to execute this command.
|
|
471
|
-
`;
|
|
472
|
-
}
|
|
473
|
-
const embed = new import_discord.EmbedBuilder().setTitle(`:x: Missing permissions!`).setDescription(embedDescription).setColor("Red");
|
|
474
|
-
interaction.reply({ embeds: [embed], ephemeral: true });
|
|
475
|
-
return true;
|
|
476
|
-
}
|
|
477
|
-
|
|
478
|
-
// src/handlers/command-handler/validations/index.ts
|
|
479
|
-
var validations_default = [devOnly_default, permissions_default];
|
|
480
|
-
|
|
481
|
-
// src/handlers/command-handler/CommandHandler.ts
|
|
482
|
-
var import_async_hooks = require("async_hooks");
|
|
483
|
-
var CommandHandler = class {
|
|
484
|
-
#data;
|
|
485
|
-
context = null;
|
|
486
|
-
constructor({ ...options }) {
|
|
487
|
-
this.#data = {
|
|
488
|
-
...options,
|
|
489
|
-
builtInValidations: [],
|
|
490
|
-
commands: []
|
|
491
|
-
};
|
|
492
|
-
}
|
|
493
|
-
async init() {
|
|
494
|
-
if (this.#data.enableHooks && !this.context) {
|
|
495
|
-
this.context = new import_async_hooks.AsyncLocalStorage();
|
|
496
|
-
}
|
|
497
|
-
await this.#buildCommands();
|
|
498
|
-
this.#buildBuiltInValidations();
|
|
499
|
-
const devOnlyCommands = this.#data.commands.filter((cmd) => cmd.options?.devOnly);
|
|
500
|
-
if (devOnlyCommands.length && !this.#data.devGuildIds.length) {
|
|
501
|
-
process.emitWarning(
|
|
502
|
-
colors_default.yellow(
|
|
503
|
-
'You have commands marked as "devOnly", but "devGuildIds" have not been set.'
|
|
504
|
-
)
|
|
505
|
-
);
|
|
506
|
-
}
|
|
507
|
-
if (devOnlyCommands.length && !this.#data.devUserIds.length && !this.#data.devRoleIds.length) {
|
|
508
|
-
process.emitWarning(
|
|
509
|
-
colors_default.yellow(
|
|
510
|
-
'You have commands marked as "devOnly", but "devUserIds" or "devRoleIds" have not been set.'
|
|
511
|
-
)
|
|
512
|
-
);
|
|
513
|
-
}
|
|
514
|
-
if (this.#data.bulkRegister) {
|
|
515
|
-
await loadCommandsWithRest({
|
|
516
|
-
client: this.#data.client,
|
|
517
|
-
devGuildIds: this.#data.devGuildIds,
|
|
518
|
-
commands: this.#data.commands
|
|
519
|
-
});
|
|
520
|
-
} else {
|
|
521
|
-
await registerCommands({
|
|
522
|
-
client: this.#data.client,
|
|
523
|
-
devGuildIds: this.#data.devGuildIds,
|
|
524
|
-
commands: this.#data.commands
|
|
525
|
-
});
|
|
526
|
-
}
|
|
527
|
-
this.handleCommands();
|
|
528
|
-
}
|
|
529
|
-
async #buildCommands() {
|
|
530
|
-
const allowedExtensions = /\.(js|mjs|cjs|ts)$/i;
|
|
531
|
-
const paths = await getFilePaths(this.#data.commandsPath, true);
|
|
532
|
-
const commandFilePaths = paths.filter((path3) => allowedExtensions.test(path3));
|
|
533
|
-
for (const commandFilePath of commandFilePaths) {
|
|
534
|
-
const modulePath = toFileURL(commandFilePath);
|
|
535
|
-
const importedObj = await import(`${modulePath}?t=${Date.now()}`);
|
|
536
|
-
let commandObj = clone(importedObj);
|
|
537
|
-
if (typeof module !== "undefined" && typeof require !== "undefined") {
|
|
538
|
-
delete require.cache[require.resolve(commandFilePath)];
|
|
539
|
-
}
|
|
540
|
-
const compactFilePath = commandFilePath.split(process.cwd())[1] || commandFilePath;
|
|
541
|
-
if (commandObj.default)
|
|
542
|
-
commandObj = commandObj.default;
|
|
543
|
-
if (importedObj.default) {
|
|
544
|
-
commandObj.data = importedObj.default.data;
|
|
545
|
-
} else {
|
|
546
|
-
commandObj.data = importedObj.data;
|
|
547
|
-
}
|
|
548
|
-
if (!commandObj.data) {
|
|
549
|
-
process.emitWarning(
|
|
550
|
-
colors_default.yellow(
|
|
551
|
-
`Ignoring: Command file ${compactFilePath} does not export "data".`
|
|
552
|
-
)
|
|
553
|
-
);
|
|
554
|
-
continue;
|
|
555
|
-
}
|
|
556
|
-
if (!commandObj.data.name) {
|
|
557
|
-
process.emitWarning(
|
|
558
|
-
colors_default.yellow(
|
|
559
|
-
`Ignoring: Command file ${compactFilePath} does not export "data.name".`
|
|
560
|
-
)
|
|
561
|
-
);
|
|
562
|
-
continue;
|
|
563
|
-
}
|
|
564
|
-
if (!commandObj.run) {
|
|
565
|
-
process.emitWarning(
|
|
566
|
-
colors_default.yellow(
|
|
567
|
-
`Ignoring: Command file ${commandObj.data.name} does not export "run".`
|
|
568
|
-
)
|
|
569
|
-
);
|
|
570
|
-
continue;
|
|
571
|
-
}
|
|
572
|
-
if (typeof commandObj.run !== "function") {
|
|
573
|
-
process.emitWarning(
|
|
574
|
-
colors_default.yellow(
|
|
575
|
-
`Ignoring: Command file ${commandObj.data.name} does not export "run" as a function.`
|
|
576
|
-
)
|
|
577
|
-
);
|
|
578
|
-
continue;
|
|
579
|
-
}
|
|
580
|
-
commandObj.filePath = commandFilePath;
|
|
581
|
-
let commandCategory = commandFilePath.split(this.#data.commandsPath)[1]?.replace(/\\\\|\\/g, "/").split("/")[1] || null;
|
|
582
|
-
if (commandCategory && allowedExtensions.test(commandCategory)) {
|
|
583
|
-
commandObj.category = null;
|
|
584
|
-
} else {
|
|
585
|
-
commandObj.category = commandCategory;
|
|
586
|
-
}
|
|
587
|
-
this.#data.commands.push(commandObj);
|
|
588
|
-
}
|
|
589
|
-
}
|
|
590
|
-
#buildBuiltInValidations() {
|
|
591
|
-
for (const builtInValidationFunction of validations_default) {
|
|
592
|
-
this.#data.builtInValidations.push(builtInValidationFunction);
|
|
593
|
-
}
|
|
594
|
-
}
|
|
595
|
-
handleCommands() {
|
|
596
|
-
const areHooksEnabled = this.#data.enableHooks;
|
|
597
|
-
this.#data.client.on("interactionCreate", async (interaction) => {
|
|
598
|
-
if (!interaction.isChatInputCommand() && !interaction.isContextMenuCommand() && !interaction.isAutocomplete())
|
|
599
|
-
return;
|
|
600
|
-
const isAutocomplete = interaction.isAutocomplete();
|
|
601
|
-
const targetCommand = this.#data.commands.find(
|
|
602
|
-
(cmd) => cmd.data.name === interaction.commandName
|
|
603
|
-
);
|
|
604
|
-
if (!targetCommand)
|
|
605
|
-
return;
|
|
606
|
-
const { data, options, run, autocomplete, ...rest } = targetCommand;
|
|
607
|
-
if (isAutocomplete && !autocomplete)
|
|
608
|
-
return;
|
|
609
|
-
const executor = async () => {
|
|
610
|
-
const commandObj = {
|
|
611
|
-
data: targetCommand.data,
|
|
612
|
-
options: targetCommand.options,
|
|
613
|
-
...rest
|
|
614
|
-
};
|
|
615
|
-
if (this.#data.validationHandler) {
|
|
616
|
-
let canRun2 = true;
|
|
617
|
-
for (const validationFunction of this.#data.validationHandler.validations) {
|
|
618
|
-
const stopValidationLoop = await validationFunction({
|
|
619
|
-
interaction,
|
|
620
|
-
commandObj,
|
|
621
|
-
client: this.#data.client,
|
|
622
|
-
handler: this.#data.commandkitInstance
|
|
623
|
-
});
|
|
624
|
-
if (stopValidationLoop) {
|
|
625
|
-
canRun2 = false;
|
|
626
|
-
break;
|
|
627
|
-
}
|
|
628
|
-
}
|
|
629
|
-
if (!canRun2)
|
|
630
|
-
return;
|
|
631
|
-
}
|
|
632
|
-
let canRun = true;
|
|
633
|
-
if (!this.#data.skipBuiltInValidations) {
|
|
634
|
-
for (const validation of this.#data.builtInValidations) {
|
|
635
|
-
const stopValidationLoop = validation({
|
|
636
|
-
targetCommand,
|
|
637
|
-
interaction,
|
|
638
|
-
handlerData: this.#data
|
|
639
|
-
});
|
|
640
|
-
if (stopValidationLoop) {
|
|
641
|
-
canRun = false;
|
|
642
|
-
break;
|
|
643
|
-
}
|
|
644
|
-
}
|
|
645
|
-
}
|
|
646
|
-
if (!canRun)
|
|
647
|
-
return;
|
|
648
|
-
const command = targetCommand[isAutocomplete ? "autocomplete" : "run"];
|
|
649
|
-
if (!areHooksEnabled) {
|
|
650
|
-
const context2 = {
|
|
651
|
-
interaction,
|
|
652
|
-
client: this.#data.client,
|
|
653
|
-
handler: this.#data.commandkitInstance
|
|
654
|
-
};
|
|
655
|
-
return await command(context2);
|
|
656
|
-
}
|
|
657
|
-
return command();
|
|
658
|
-
};
|
|
659
|
-
if (this.context)
|
|
660
|
-
return this.context.run(
|
|
661
|
-
{
|
|
662
|
-
command: targetCommand.data,
|
|
663
|
-
interaction
|
|
664
|
-
},
|
|
665
|
-
executor
|
|
666
|
-
);
|
|
667
|
-
return executor();
|
|
668
|
-
});
|
|
669
|
-
}
|
|
670
|
-
get commands() {
|
|
671
|
-
return this.#data.commands;
|
|
672
|
-
}
|
|
673
|
-
async reloadCommands(type) {
|
|
674
|
-
if (!this.#data.commandsPath) {
|
|
675
|
-
throw new Error(
|
|
676
|
-
colors_default.red(
|
|
677
|
-
'Cannot reload commands as "commandsPath" was not provided when instantiating CommandKit.'
|
|
678
|
-
)
|
|
679
|
-
);
|
|
680
|
-
}
|
|
681
|
-
this.#data.commands = [];
|
|
682
|
-
await this.#buildCommands();
|
|
683
|
-
if (this.#data.bulkRegister) {
|
|
684
|
-
await loadCommandsWithRest({
|
|
685
|
-
client: this.#data.client,
|
|
686
|
-
devGuildIds: this.#data.devGuildIds,
|
|
687
|
-
commands: this.#data.commands,
|
|
688
|
-
reloading: true,
|
|
689
|
-
type
|
|
690
|
-
});
|
|
691
|
-
} else {
|
|
692
|
-
await registerCommands({
|
|
693
|
-
client: this.#data.client,
|
|
694
|
-
devGuildIds: this.#data.devGuildIds,
|
|
695
|
-
commands: this.#data.commands,
|
|
696
|
-
reloading: true,
|
|
697
|
-
type
|
|
698
|
-
});
|
|
699
|
-
}
|
|
700
|
-
}
|
|
701
|
-
};
|
|
702
|
-
|
|
703
|
-
// src/handlers/event-handler/EventHandler.ts
|
|
704
|
-
var EventHandler = class {
|
|
705
|
-
#data;
|
|
706
|
-
constructor({ ...options }) {
|
|
707
|
-
this.#data = {
|
|
708
|
-
...options,
|
|
709
|
-
events: []
|
|
710
|
-
};
|
|
711
|
-
}
|
|
712
|
-
async init() {
|
|
713
|
-
await this.#buildEvents();
|
|
714
|
-
this.#registerEvents();
|
|
715
|
-
}
|
|
716
|
-
async #buildEvents() {
|
|
717
|
-
const eventFolderPaths = await getFolderPaths(this.#data.eventsPath);
|
|
718
|
-
for (const eventFolderPath of eventFolderPaths) {
|
|
719
|
-
const eventName = eventFolderPath.replace(/\\\\|\\/g, "/").split("/").pop();
|
|
720
|
-
const allowedExtensions = /\.(js|mjs|cjs|ts)$/i;
|
|
721
|
-
const eventPaths = await getFilePaths(eventFolderPath, true);
|
|
722
|
-
const eventFilePaths = eventPaths.filter((path3) => allowedExtensions.test(path3));
|
|
723
|
-
const eventObj = {
|
|
724
|
-
name: eventName,
|
|
725
|
-
functions: []
|
|
726
|
-
};
|
|
727
|
-
this.#data.events.push(eventObj);
|
|
728
|
-
for (const eventFilePath of eventFilePaths) {
|
|
729
|
-
const modulePath = toFileURL(eventFilePath);
|
|
730
|
-
let importedFunction = (await import(`${modulePath}?t=${Date.now()}`)).default;
|
|
731
|
-
let eventFunction = clone(importedFunction);
|
|
732
|
-
if (typeof module !== "undefined" && typeof require !== "undefined") {
|
|
733
|
-
delete require.cache[require.resolve(eventFilePath)];
|
|
734
|
-
}
|
|
735
|
-
if (eventFunction?.default) {
|
|
736
|
-
eventFunction = eventFunction.default;
|
|
737
|
-
}
|
|
738
|
-
const compactFilePath = eventFilePath.split(process.cwd())[1] || eventFilePath;
|
|
739
|
-
if (typeof eventFunction !== "function") {
|
|
740
|
-
process.emitWarning(
|
|
741
|
-
colors_default.yellow(
|
|
742
|
-
`Ignoring: Event file ${compactFilePath} does not export a function.`
|
|
743
|
-
)
|
|
744
|
-
);
|
|
745
|
-
continue;
|
|
746
|
-
}
|
|
747
|
-
eventObj.functions.push(eventFunction);
|
|
748
|
-
}
|
|
749
|
-
}
|
|
750
|
-
}
|
|
751
|
-
#registerEvents() {
|
|
752
|
-
const client = this.#data.client;
|
|
753
|
-
const handler = this.#data.commandKitInstance;
|
|
754
|
-
for (const eventObj of this.#data.events) {
|
|
755
|
-
client.on(eventObj.name, async (...params) => {
|
|
756
|
-
for (const eventFunction of eventObj.functions) {
|
|
757
|
-
const stopEventLoop = await eventFunction(...params, client, handler);
|
|
758
|
-
if (stopEventLoop) {
|
|
759
|
-
break;
|
|
760
|
-
}
|
|
761
|
-
}
|
|
762
|
-
});
|
|
763
|
-
}
|
|
764
|
-
}
|
|
765
|
-
get events() {
|
|
766
|
-
return this.#data.events;
|
|
767
|
-
}
|
|
768
|
-
async reloadEvents(commandHandler) {
|
|
769
|
-
if (!this.#data.eventsPath) {
|
|
770
|
-
throw new Error(
|
|
771
|
-
colors_default.red(
|
|
772
|
-
'Cannot reload events as "eventsPath" was not provided when instantiating CommandKit.'
|
|
773
|
-
)
|
|
774
|
-
);
|
|
775
|
-
}
|
|
776
|
-
this.#data.events = [];
|
|
777
|
-
await this.#buildEvents();
|
|
778
|
-
this.#data.client.removeAllListeners();
|
|
779
|
-
this.#registerEvents();
|
|
780
|
-
commandHandler?.handleCommands();
|
|
781
|
-
}
|
|
782
|
-
};
|
|
783
|
-
|
|
784
|
-
// src/handlers/validation-handler/ValidationHandler.ts
|
|
785
|
-
var ValidationHandler = class {
|
|
786
|
-
#data;
|
|
787
|
-
constructor({ ...options }) {
|
|
788
|
-
this.#data = {
|
|
789
|
-
...options,
|
|
790
|
-
validations: []
|
|
791
|
-
};
|
|
792
|
-
}
|
|
793
|
-
async init() {
|
|
794
|
-
this.#data.validations = await this.#buildValidations();
|
|
795
|
-
}
|
|
796
|
-
async #buildValidations() {
|
|
797
|
-
const allowedExtensions = /\.(js|mjs|cjs|ts)$/i;
|
|
798
|
-
const validationPaths = await getFilePaths(this.#data.validationsPath, true);
|
|
799
|
-
const validationFilePaths = validationPaths.filter((path3) => allowedExtensions.test(path3));
|
|
800
|
-
const validationFunctions = [];
|
|
801
|
-
for (const validationFilePath of validationFilePaths) {
|
|
802
|
-
const modulePath = toFileURL(validationFilePath);
|
|
803
|
-
let importedFunction = (await import(`${modulePath}?t=${Date.now()}`)).default;
|
|
804
|
-
let validationFunction = clone(importedFunction);
|
|
805
|
-
if (typeof module !== "undefined" && typeof require !== "undefined") {
|
|
806
|
-
delete require.cache[require.resolve(validationFilePath)];
|
|
807
|
-
}
|
|
808
|
-
if (validationFunction?.default) {
|
|
809
|
-
validationFunction = validationFunction.default;
|
|
810
|
-
}
|
|
811
|
-
const compactFilePath = validationFilePath.split(process.cwd())[1] || validationFilePath;
|
|
812
|
-
if (typeof validationFunction !== "function") {
|
|
813
|
-
process.emitWarning(
|
|
814
|
-
colors_default.yellow(
|
|
815
|
-
`Ignoring: Validation file ${compactFilePath} does not export a function.`
|
|
816
|
-
)
|
|
817
|
-
);
|
|
818
|
-
continue;
|
|
819
|
-
}
|
|
820
|
-
validationFunctions.push(validationFunction);
|
|
821
|
-
}
|
|
822
|
-
return validationFunctions;
|
|
823
|
-
}
|
|
824
|
-
get validations() {
|
|
825
|
-
return this.#data.validations;
|
|
826
|
-
}
|
|
827
|
-
async reloadValidations() {
|
|
828
|
-
if (!this.#data.validationsPath) {
|
|
829
|
-
throw new Error(
|
|
830
|
-
colors_default.red(
|
|
831
|
-
'Cannot reload validations as "validationsPath" was not provided when instantiating CommandKit.'
|
|
832
|
-
)
|
|
833
|
-
);
|
|
834
|
-
}
|
|
835
|
-
const newValidations = await this.#buildValidations();
|
|
836
|
-
this.#data.validations = newValidations;
|
|
837
|
-
}
|
|
838
|
-
};
|
|
839
|
-
|
|
840
|
-
// src/CommandKit.ts
|
|
841
|
-
var CommandKit = class _CommandKit {
|
|
842
|
-
#data;
|
|
843
|
-
static _instance = null;
|
|
844
|
-
/**
|
|
845
|
-
* Create a new command and event handler with CommandKit.
|
|
846
|
-
*
|
|
847
|
-
* @param options - The default CommandKit configuration.
|
|
848
|
-
* @see {@link https://commandkit.js.org/docs/commandkit-setup}
|
|
849
|
-
*/
|
|
850
|
-
constructor(options) {
|
|
851
|
-
if (!options.client) {
|
|
852
|
-
throw new Error(colors_default.red('"client" is required when instantiating CommandKit.'));
|
|
853
|
-
}
|
|
854
|
-
if (options.validationsPath && !options.commandsPath) {
|
|
855
|
-
throw new Error(
|
|
856
|
-
colors_default.red('"commandsPath" is required when "validationsPath" is set.')
|
|
857
|
-
);
|
|
858
|
-
}
|
|
859
|
-
this.#data = options;
|
|
860
|
-
_CommandKit._instance = this;
|
|
861
|
-
this.#init();
|
|
862
|
-
}
|
|
863
|
-
/**
|
|
864
|
-
* Get the client attached to this CommandKit instance.
|
|
865
|
-
*/
|
|
866
|
-
get client() {
|
|
867
|
-
return this.#data.client;
|
|
868
|
-
}
|
|
869
|
-
/**
|
|
870
|
-
* Get command handler instance.
|
|
871
|
-
*/
|
|
872
|
-
get commandHandler() {
|
|
873
|
-
return this.#data.commandHandler;
|
|
874
|
-
}
|
|
875
|
-
/**
|
|
876
|
-
* (Private) Initialize CommandKit.
|
|
877
|
-
*/
|
|
878
|
-
async #init() {
|
|
879
|
-
if (this.#data.eventsPath) {
|
|
880
|
-
const eventHandler = new EventHandler({
|
|
881
|
-
client: this.#data.client,
|
|
882
|
-
eventsPath: this.#data.eventsPath,
|
|
883
|
-
commandKitInstance: this
|
|
884
|
-
});
|
|
885
|
-
await eventHandler.init();
|
|
886
|
-
this.#data.eventHandler = eventHandler;
|
|
887
|
-
}
|
|
888
|
-
if (this.#data.validationsPath) {
|
|
889
|
-
const validationHandler = new ValidationHandler({
|
|
890
|
-
validationsPath: this.#data.validationsPath
|
|
891
|
-
});
|
|
892
|
-
await validationHandler.init();
|
|
893
|
-
this.#data.validationHandler = validationHandler;
|
|
894
|
-
}
|
|
895
|
-
if (this.#data.commandsPath) {
|
|
896
|
-
const commandHandler = new CommandHandler({
|
|
897
|
-
client: this.#data.client,
|
|
898
|
-
commandsPath: this.#data.commandsPath,
|
|
899
|
-
devGuildIds: this.#data.devGuildIds || [],
|
|
900
|
-
devUserIds: this.#data.devUserIds || [],
|
|
901
|
-
devRoleIds: this.#data.devRoleIds || [],
|
|
902
|
-
validationHandler: this.#data.validationHandler,
|
|
903
|
-
skipBuiltInValidations: this.#data.skipBuiltInValidations || false,
|
|
904
|
-
commandkitInstance: this,
|
|
905
|
-
bulkRegister: this.#data.bulkRegister || false,
|
|
906
|
-
enableHooks: this.#data.experimental?.hooks ?? false
|
|
907
|
-
});
|
|
908
|
-
await commandHandler.init();
|
|
909
|
-
this.#data.commandHandler = commandHandler;
|
|
910
|
-
}
|
|
911
|
-
}
|
|
912
|
-
/**
|
|
913
|
-
* Updates application commands with the latest from "commandsPath".
|
|
914
|
-
*/
|
|
915
|
-
async reloadCommands(type) {
|
|
916
|
-
if (!this.#data.commandHandler)
|
|
917
|
-
return;
|
|
918
|
-
await this.#data.commandHandler.reloadCommands(type);
|
|
919
|
-
}
|
|
920
|
-
/**
|
|
921
|
-
* Updates application events with the latest from "eventsPath".
|
|
922
|
-
*/
|
|
923
|
-
async reloadEvents() {
|
|
924
|
-
if (!this.#data.eventHandler)
|
|
925
|
-
return;
|
|
926
|
-
await this.#data.eventHandler.reloadEvents(this.#data.commandHandler);
|
|
927
|
-
}
|
|
928
|
-
/**
|
|
929
|
-
* Updates application command validations with the latest from "validationsPath".
|
|
930
|
-
*/
|
|
931
|
-
async reloadValidations() {
|
|
932
|
-
if (!this.#data.validationHandler)
|
|
933
|
-
return;
|
|
934
|
-
await this.#data.validationHandler.reloadValidations();
|
|
935
|
-
}
|
|
936
|
-
/**
|
|
937
|
-
* @returns An array of objects of all the commands that CommandKit is handling.
|
|
938
|
-
*/
|
|
939
|
-
get commands() {
|
|
940
|
-
if (!this.#data.commandHandler) {
|
|
941
|
-
return [];
|
|
942
|
-
}
|
|
943
|
-
const commands = this.#data.commandHandler.commands.map((cmd) => {
|
|
944
|
-
const { run, autocomplete, ...command } = cmd;
|
|
945
|
-
return command;
|
|
946
|
-
});
|
|
947
|
-
return commands;
|
|
948
|
-
}
|
|
949
|
-
/**
|
|
950
|
-
* @returns The path to the commands folder which was set when instantiating CommandKit.
|
|
951
|
-
*/
|
|
952
|
-
get commandsPath() {
|
|
953
|
-
return this.#data.commandsPath;
|
|
954
|
-
}
|
|
955
|
-
/**
|
|
956
|
-
* @returns The path to the events folder which was set when instantiating CommandKit.
|
|
957
|
-
*/
|
|
958
|
-
get eventsPath() {
|
|
959
|
-
return this.#data.eventsPath;
|
|
960
|
-
}
|
|
961
|
-
/**
|
|
962
|
-
* @returns The path to the validations folder which was set when instantiating CommandKit.
|
|
963
|
-
*/
|
|
964
|
-
get validationsPath() {
|
|
965
|
-
return this.#data.validationsPath;
|
|
966
|
-
}
|
|
967
|
-
/**
|
|
968
|
-
* @returns An array of all the developer user IDs which was set when instantiating CommandKit.
|
|
969
|
-
*/
|
|
970
|
-
get devUserIds() {
|
|
971
|
-
return this.#data.devUserIds || [];
|
|
972
|
-
}
|
|
973
|
-
/**
|
|
974
|
-
* @returns An array of all the developer guild IDs which was set when instantiating CommandKit.
|
|
975
|
-
*/
|
|
976
|
-
get devGuildIds() {
|
|
977
|
-
return this.#data.devGuildIds || [];
|
|
978
|
-
}
|
|
979
|
-
/**
|
|
980
|
-
* @returns An array of all the developer role IDs which was set when instantiating CommandKit.
|
|
981
|
-
*/
|
|
982
|
-
get devRoleIds() {
|
|
983
|
-
return this.#data.devRoleIds || [];
|
|
984
|
-
}
|
|
985
|
-
};
|
|
986
|
-
|
|
987
|
-
// src/components/ButtonKit.ts
|
|
988
|
-
var import_discord2 = require("discord.js");
|
|
989
|
-
var ButtonKit = class extends import_discord2.ButtonBuilder {
|
|
990
|
-
#onClickHandler = null;
|
|
991
|
-
#onEndHandler = null;
|
|
992
|
-
#contextData = null;
|
|
993
|
-
#collector = null;
|
|
994
|
-
/**
|
|
995
|
-
* Sets up an inline interaction collector for this button. This collector by default allows as many interactions as possible if it is actively used.
|
|
996
|
-
* If unused, this expires after 24 hours or custom time if specified.
|
|
997
|
-
* @param handler The handler to run when the button is clicked
|
|
998
|
-
* @param data The context data to use for the interaction collector
|
|
999
|
-
* @returns This button
|
|
1000
|
-
* @example
|
|
1001
|
-
* ```ts
|
|
1002
|
-
* const button = new ButtonKit()
|
|
1003
|
-
* .setLabel('Click me')
|
|
1004
|
-
* .setStyle(ButtonStyle.Primary)
|
|
1005
|
-
* .setCustomId('click_me');
|
|
1006
|
-
*
|
|
1007
|
-
* const row = new ActionRowBuilder().addComponents(button);
|
|
1008
|
-
*
|
|
1009
|
-
* const message = await channel.send({ content: 'Click the button', components: [row] });
|
|
1010
|
-
*
|
|
1011
|
-
* button.onClick(async (interaction) => {
|
|
1012
|
-
* await interaction.reply('You clicked me!');
|
|
1013
|
-
* }, { message });
|
|
1014
|
-
*
|
|
1015
|
-
* // Remove onClick handler and destroy the interaction collector
|
|
1016
|
-
* button.onClick(null);
|
|
1017
|
-
* ```
|
|
1018
|
-
*/
|
|
1019
|
-
onClick(handler, data) {
|
|
1020
|
-
if (this.data.style === import_discord2.ButtonStyle.Link) {
|
|
1021
|
-
throw new TypeError('Cannot setup "onClick" handler on link buttons.');
|
|
1022
|
-
}
|
|
1023
|
-
if (!handler) {
|
|
1024
|
-
throw new TypeError('Cannot setup "onClick" without a handler function parameter.');
|
|
1025
|
-
}
|
|
1026
|
-
this.#destroyCollector();
|
|
1027
|
-
this.#onClickHandler = handler;
|
|
1028
|
-
if (data)
|
|
1029
|
-
this.#contextData = data;
|
|
1030
|
-
this.#setupInteractionCollector();
|
|
1031
|
-
return this;
|
|
1032
|
-
}
|
|
1033
|
-
onEnd(handler) {
|
|
1034
|
-
if (!handler) {
|
|
1035
|
-
throw new TypeError('Cannot setup "onEnd" without a handler function parameter.');
|
|
1036
|
-
}
|
|
1037
|
-
this.#onEndHandler = handler;
|
|
1038
|
-
return this;
|
|
1039
|
-
}
|
|
1040
|
-
#setupInteractionCollector() {
|
|
1041
|
-
if (!this.#contextData || !this.#onClickHandler)
|
|
1042
|
-
return;
|
|
1043
|
-
const message = this.#contextData.message;
|
|
1044
|
-
if (!message) {
|
|
1045
|
-
throw new TypeError(
|
|
1046
|
-
'Cannot setup "onClick" handler without a message in the context data.'
|
|
1047
|
-
);
|
|
1048
|
-
}
|
|
1049
|
-
if ("customId" in this.data && !this.data.customId) {
|
|
1050
|
-
throw new TypeError('Cannot setup "onClick" handler on a button without a custom id.');
|
|
1051
|
-
}
|
|
1052
|
-
const data = {
|
|
1053
|
-
time: 864e5,
|
|
1054
|
-
autoReset: true,
|
|
1055
|
-
...this.#contextData
|
|
1056
|
-
};
|
|
1057
|
-
const collector = this.#collector = message.createMessageComponentCollector({
|
|
1058
|
-
filter: (interaction) => interaction.customId === this.data.custom_id && interaction.message.id === message.id,
|
|
1059
|
-
componentType: import_discord2.ComponentType.Button,
|
|
1060
|
-
...data
|
|
1061
|
-
});
|
|
1062
|
-
this.#collector.on("collect", (interaction) => {
|
|
1063
|
-
const handler = this.#onClickHandler;
|
|
1064
|
-
if (!handler)
|
|
1065
|
-
return this.#destroyCollector();
|
|
1066
|
-
if (!this.#collector) {
|
|
1067
|
-
return collector.stop("destroyed");
|
|
1068
|
-
}
|
|
1069
|
-
if (data.autoReset) {
|
|
1070
|
-
this.#collector.resetTimer();
|
|
1071
|
-
}
|
|
1072
|
-
return handler(interaction);
|
|
1073
|
-
});
|
|
1074
|
-
this.#collector.on("end", () => {
|
|
1075
|
-
this.#destroyCollector();
|
|
1076
|
-
this.#onEndHandler?.();
|
|
1077
|
-
});
|
|
1078
|
-
}
|
|
1079
|
-
#destroyCollector() {
|
|
1080
|
-
this.#collector?.stop("end");
|
|
1081
|
-
this.#collector?.removeAllListeners();
|
|
1082
|
-
this.#collector = null;
|
|
1083
|
-
this.#contextData = null;
|
|
1084
|
-
this.#onClickHandler = null;
|
|
1085
|
-
}
|
|
1086
|
-
};
|
|
1087
|
-
|
|
1088
|
-
// src/config.ts
|
|
1089
|
-
var globalConfig = {
|
|
1090
|
-
envExtra: true,
|
|
1091
|
-
outDir: "dist",
|
|
1092
|
-
watch: true,
|
|
1093
|
-
clearRestartLogs: true,
|
|
1094
|
-
minify: false,
|
|
1095
|
-
sourcemap: false,
|
|
1096
|
-
nodeOptions: [],
|
|
1097
|
-
antiCrash: true,
|
|
1098
|
-
requirePolyfill: true
|
|
1099
|
-
};
|
|
1100
|
-
function getConfig() {
|
|
1101
|
-
return globalConfig;
|
|
1102
|
-
}
|
|
1103
|
-
var requiredProps = ["src", "main"];
|
|
1104
|
-
function defineConfig(config) {
|
|
1105
|
-
for (const prop of requiredProps) {
|
|
1106
|
-
if (!config[prop]) {
|
|
1107
|
-
throw new Error(`[CommandKit Config] Missing required config property: ${prop}`);
|
|
1108
|
-
}
|
|
1109
|
-
}
|
|
1110
|
-
globalConfig = {
|
|
1111
|
-
...globalConfig,
|
|
1112
|
-
...config
|
|
1113
|
-
};
|
|
1114
|
-
return globalConfig;
|
|
1115
|
-
}
|
|
1116
|
-
|
|
1117
|
-
// src/utils/signal.ts
|
|
1118
|
-
var context = [];
|
|
1119
|
-
function createSignal(value) {
|
|
1120
|
-
const subscribers = /* @__PURE__ */ new Set();
|
|
1121
|
-
let disposed = false;
|
|
1122
|
-
let val = value instanceof Function ? value() : value;
|
|
1123
|
-
const getter = () => {
|
|
1124
|
-
if (!disposed) {
|
|
1125
|
-
const running = getCurrentObserver();
|
|
1126
|
-
if (running)
|
|
1127
|
-
subscribers.add(running);
|
|
1128
|
-
}
|
|
1129
|
-
return val;
|
|
1130
|
-
};
|
|
1131
|
-
const setter = (newValue) => {
|
|
1132
|
-
if (disposed)
|
|
1133
|
-
return;
|
|
1134
|
-
val = newValue instanceof Function ? newValue(val) : newValue;
|
|
1135
|
-
for (const subscriber of subscribers) {
|
|
1136
|
-
subscriber();
|
|
1137
|
-
}
|
|
1138
|
-
};
|
|
1139
|
-
const dispose = () => {
|
|
1140
|
-
subscribers.clear();
|
|
1141
|
-
disposed = true;
|
|
1142
|
-
};
|
|
1143
|
-
return [getter, setter, dispose];
|
|
1144
|
-
}
|
|
1145
|
-
function createEffect(callback) {
|
|
1146
|
-
const execute = () => {
|
|
1147
|
-
context.push(execute);
|
|
1148
|
-
try {
|
|
1149
|
-
callback();
|
|
1150
|
-
} finally {
|
|
1151
|
-
context.pop();
|
|
1152
|
-
}
|
|
1153
|
-
};
|
|
1154
|
-
execute();
|
|
1155
|
-
}
|
|
1156
|
-
function getCurrentObserver() {
|
|
1157
|
-
return context[context.length - 1];
|
|
1158
|
-
}
|
|
1159
|
-
|
|
1160
|
-
// src/hooks/common.ts
|
|
1161
|
-
function getCommandKit() {
|
|
1162
|
-
return CommandKit._instance;
|
|
1163
|
-
}
|
|
1164
|
-
function getCommandHandler() {
|
|
1165
|
-
const handler = getCommandKit()?.commandHandler;
|
|
1166
|
-
if (!handler) {
|
|
1167
|
-
throw new Error("CommandKit is not initialized.");
|
|
1168
|
-
}
|
|
1169
|
-
return handler;
|
|
1170
|
-
}
|
|
1171
|
-
function getContext() {
|
|
1172
|
-
const info = getCommandHandler().context;
|
|
1173
|
-
if (!info) {
|
|
1174
|
-
throw new Error("Context is not available, did you forget to enable hooks?");
|
|
1175
|
-
}
|
|
1176
|
-
return info;
|
|
1177
|
-
}
|
|
1178
|
-
function prepareHookInvocationError(name) {
|
|
1179
|
-
return new Error(`Cannot invoke hook "${name}" outside of a command.`);
|
|
1180
|
-
}
|
|
1181
|
-
|
|
1182
|
-
// src/hooks/useCommandKit.ts
|
|
1183
|
-
function useCommandKit() {
|
|
1184
|
-
const kit = getCommandKit();
|
|
1185
|
-
if (!kit)
|
|
1186
|
-
throw new Error("CommandKit is not initialized.");
|
|
1187
|
-
return kit;
|
|
1188
|
-
}
|
|
1189
|
-
|
|
1190
|
-
// src/hooks/useClient.ts
|
|
1191
|
-
function useClient() {
|
|
1192
|
-
return useCommandKit().client;
|
|
1193
|
-
}
|
|
1194
|
-
|
|
1195
|
-
// src/hooks/useInteraction.ts
|
|
1196
|
-
function useInteraction() {
|
|
1197
|
-
const data = getContext().getStore();
|
|
1198
|
-
if (!data)
|
|
1199
|
-
throw prepareHookInvocationError("useInteraction");
|
|
1200
|
-
return data.interaction;
|
|
1201
|
-
}
|
|
1202
|
-
|
|
1203
|
-
// src/hooks/response.ts
|
|
1204
|
-
async function response(data) {
|
|
1205
|
-
const interaction = useInteraction();
|
|
1206
|
-
if (interaction.isAutocomplete())
|
|
1207
|
-
return;
|
|
1208
|
-
if (interaction.replied || interaction.deferred) {
|
|
1209
|
-
await interaction.editReply(data);
|
|
1210
|
-
} else if (interaction.isMessageComponent()) {
|
|
1211
|
-
await interaction.update(data);
|
|
1212
|
-
} else {
|
|
1213
|
-
await interaction.reply(data);
|
|
1214
|
-
}
|
|
1215
|
-
}
|
|
1216
|
-
|
|
1217
|
-
// src/hooks/useChannel.ts
|
|
1218
|
-
function useChannel() {
|
|
1219
|
-
const data = getContext().getStore();
|
|
1220
|
-
if (!data)
|
|
1221
|
-
throw prepareHookInvocationError("useChannel");
|
|
1222
|
-
return data.interaction.channel;
|
|
1223
|
-
}
|
|
1224
|
-
|
|
1225
|
-
// src/hooks/useCommandData.ts
|
|
1226
|
-
function useCommandData() {
|
|
1227
|
-
const data = getContext().getStore();
|
|
1228
|
-
if (!data)
|
|
1229
|
-
throw prepareHookInvocationError("useCommandData");
|
|
1230
|
-
return data.command;
|
|
1231
|
-
}
|
|
1232
|
-
|
|
1233
|
-
// src/hooks/useGuild.ts
|
|
1234
|
-
function useGuild() {
|
|
1235
|
-
const data = getContext().getStore();
|
|
1236
|
-
if (!data)
|
|
1237
|
-
throw prepareHookInvocationError("useGuild");
|
|
1238
|
-
return data.interaction.guild;
|
|
1239
|
-
}
|
|
1240
|
-
|
|
1241
|
-
// src/hooks/useMember.ts
|
|
1242
|
-
function useMember() {
|
|
1243
|
-
const data = getContext().getStore();
|
|
1244
|
-
if (!data)
|
|
1245
|
-
throw prepareHookInvocationError("useMember");
|
|
1246
|
-
return data.interaction.member;
|
|
1247
|
-
}
|
|
1248
|
-
|
|
1249
|
-
// src/hooks/useUser.ts
|
|
1250
|
-
function useUser() {
|
|
1251
|
-
const data = getContext().getStore();
|
|
1252
|
-
if (!data)
|
|
1253
|
-
throw prepareHookInvocationError("useUser");
|
|
1254
|
-
return data.interaction.user;
|
|
1255
|
-
}
|
|
1256
|
-
// Annotate the CommonJS export names for ESM import in node:
|
|
1257
|
-
0 && (module.exports = {
|
|
1258
|
-
ButtonKit,
|
|
1259
|
-
CommandKit,
|
|
1260
|
-
createEffect,
|
|
1261
|
-
createSignal,
|
|
1262
|
-
defineConfig,
|
|
1263
|
-
getConfig,
|
|
1264
|
-
response,
|
|
1265
|
-
useChannel,
|
|
1266
|
-
useClient,
|
|
1267
|
-
useCommandData,
|
|
1268
|
-
useCommandKit,
|
|
1269
|
-
useGuild,
|
|
1270
|
-
useInteraction,
|
|
1271
|
-
useMember,
|
|
1272
|
-
useUser
|
|
1273
|
-
});
|
|
1
|
+
"use strict";var __create=Object.create;var __defProp=Object.defineProperty;var __getOwnPropDesc=Object.getOwnPropertyDescriptor;var __getOwnPropNames=Object.getOwnPropertyNames;var __getProtoOf=Object.getPrototypeOf,__hasOwnProp=Object.prototype.hasOwnProperty;var __name=(target,value)=>__defProp(target,"name",{value,configurable:!0});var __export=(target,all)=>{for(var name in all)__defProp(target,name,{get:all[name],enumerable:!0})},__copyProps=(to,from,except,desc)=>{if(from&&typeof from=="object"||typeof from=="function")for(let key of __getOwnPropNames(from))!__hasOwnProp.call(to,key)&&key!==except&&__defProp(to,key,{get:()=>from[key],enumerable:!(desc=__getOwnPropDesc(from,key))||desc.enumerable});return to};var __toESM=(mod,isNodeMode,target)=>(target=mod!=null?__create(__getProtoOf(mod)):{},__copyProps(isNodeMode||!mod||!mod.__esModule?__defProp(target,"default",{value:mod,enumerable:!0}):target,mod)),__toCommonJS=mod=>__copyProps(__defProp({},"__esModule",{value:!0}),mod);var src_exports={};__export(src_exports,{ButtonKit:()=>ButtonKit,CommandKit:()=>CommandKit,createEffect:()=>createEffect,createSignal:()=>createSignal,defineConfig:()=>defineConfig,getConfig:()=>getConfig,response:()=>response,useChannel:()=>useChannel,useClient:()=>useClient,useCommandData:()=>useCommandData,useCommandKit:()=>useCommandKit,useGuild:()=>useGuild,useInteraction:()=>useInteraction,useMember:()=>useMember,useUser:()=>useUser});module.exports=__toCommonJS(src_exports);var import_path=__toESM(require("path"));function toFileURL(filePath){return"file://"+import_path.default.resolve(filePath).replace(/\\\\|\\/g,"/")}__name(toFileURL,"toFileURL");var import_path2=__toESM(require("path")),import_promises=__toESM(require("fs/promises"));async function getFilePaths(directory,nesting){let filePaths=[];if(!directory)return filePaths;let files=await import_promises.default.readdir(directory,{withFileTypes:!0});for(let file of files){let filePath=import_path2.default.join(directory,file.name);file.isFile()&&filePaths.push(filePath),nesting&&file.isDirectory()&&(filePaths=[...filePaths,...await getFilePaths(filePath,!0)])}return filePaths}__name(getFilePaths,"getFilePaths");async function getFolderPaths(directory,nesting){let folderPaths=[];if(!directory)return folderPaths;let folders=await import_promises.default.readdir(directory,{withFileTypes:!0});for(let folder of folders){let folderPath=import_path2.default.join(directory,folder.name);folder.isDirectory()&&(folderPaths.push(folderPath),nesting&&(folderPaths=[...folderPaths,...await getFolderPaths(folderPath,!0)]))}return folderPaths}__name(getFolderPaths,"getFolderPaths");var import_rfdc=__toESM(require("rfdc")),clone=(0,import_rfdc.default)();var resetColor="\x1B[0m",colors_default={reset:text=>`${text}${resetColor}`,bright:text=>`\x1B[1m${text}${resetColor}`,dim:text=>`\x1B[2m${text}${resetColor}`,underscore:text=>`\x1B[4m${text}${resetColor}`,blink:text=>`\x1B[5m${text}${resetColor}`,reverse:text=>`\x1B[7m${text}${resetColor}`,hidden:text=>`\x1B[8m${text}${resetColor}`,black:text=>`\x1B[30m${text}${resetColor}`,red:text=>`\x1B[31m${text}${resetColor}`,green:text=>`\x1B[32m${text}${resetColor}`,yellow:text=>`\x1B[33m${text}${resetColor}`,blue:text=>`\x1B[34m${text}${resetColor}`,magenta:text=>`\x1B[35m${text}${resetColor}`,cyan:text=>`\x1B[36m${text}${resetColor}`,white:text=>`\x1B[37m${text}${resetColor}`,bgBlack:text=>`\x1B[40m${text}${resetColor}`,bgRed:text=>`\x1B[41m${text}${resetColor}`,bgGreen:text=>`\x1B[42m${text}${resetColor}`,bgYellow:text=>`\x1B[43m${text}${resetColor}`,bgBlue:text=>`\x1B[44m${text}${resetColor}`,bgMagenta:text=>`\x1B[45m${text}${resetColor}`,bgCyan:text=>`\x1B[46m${text}${resetColor}`,bgWhite:text=>`\x1B[47m${text}${resetColor}`};async function loadCommandsWithRest(props){if(props.reloading)if(props.client.isReady())await handleLoading(props.client,props.commands,props.devGuildIds,props.reloading,props.type);else throw new Error(colors_default.red("Cannot reload commands when client is not ready."));else props.client.once("ready",async c=>{await handleLoading(c,props.commands,props.devGuildIds,props.reloading,props.type)})}__name(loadCommandsWithRest,"loadCommandsWithRest");async function handleLoading(client,commands,devGuildIds,reloading,type){commands=commands.filter(cmd=>!cmd.options?.deleted);let devOnlyCommands=commands.filter(cmd=>cmd.options?.devOnly),globalCommands=commands.filter(cmd=>!cmd.options?.devOnly);type==="dev"?await loadDevCommands(client,devOnlyCommands,devGuildIds,reloading):type==="global"?await loadGlobalCommands(client,globalCommands,reloading):(await loadDevCommands(client,devOnlyCommands,devGuildIds,reloading),await loadGlobalCommands(client,globalCommands,reloading))}__name(handleLoading,"handleLoading");async function loadGlobalCommands(client,commands,reloading){let requestBody=commands.map(cmd=>cmd.data);await client.application.commands.set(requestBody).catch(error=>{throw new Error(colors_default.red(`Error ${reloading?"reloading":"loading"} global application commands.
|
|
2
|
+
`),error)}),console.log(colors_default.green(`${reloading?"Reloaded":"Loaded"} ${requestBody.length} global commands.`))}__name(loadGlobalCommands,"loadGlobalCommands");async function loadDevCommands(client,commands,guildIds,reloading){let requestBody=commands.map(cmd=>cmd.data);for(let guildId of guildIds){let targetGuild=client.guilds.cache.get(guildId)||await client.guilds.fetch(guildId);if(!targetGuild){process.emitWarning(`Cannot ${reloading?"reload":"load"} commands in guild "${guildId}" - guild doesn't exist or client isn't part of the guild.`);continue}await targetGuild.commands.set(requestBody).catch(error=>{throw new Error(colors_default.red(`Error ${reloading?"reloading":"loading"} developer application commands in guild "${targetGuild?.name||guildId}".
|
|
3
|
+
`),error)}),console.log(colors_default.green(`${reloading?"Reloaded":"Loaded"} ${requestBody.length} developer commands in guild "${targetGuild.name}".`))}}__name(loadDevCommands,"loadDevCommands");function areSlashCommandsDifferent(appCommand,localCommand){if(appCommand.options||(appCommand.options=[]),localCommand.options||(localCommand.options=[]),appCommand.description||(appCommand.description=""),localCommand.description||(localCommand.description=""),localCommand.description!==appCommand.description||localCommand.options.length!==appCommand.options.length)return!0}__name(areSlashCommandsDifferent,"areSlashCommandsDifferent");async function registerCommands(props){if(props.reloading)if(props.client.isReady())await handleRegistration(props.client,props.commands,props.devGuildIds,props.type);else throw new Error(colors_default.red("Cannot reload commands when client is not ready."));else props.client.once("ready",async c=>{await handleRegistration(c,props.commands,props.devGuildIds,props.type)})}__name(registerCommands,"registerCommands");async function handleRegistration(client,commands,devGuildIds,type){let devOnlyCommands=commands.filter(cmd=>cmd.options?.devOnly),globalCommands=commands.filter(cmd=>!cmd.options?.devOnly);type==="dev"?await registerDevCommands(client,devOnlyCommands,devGuildIds):type==="global"?await registerGlobalCommands(client,globalCommands):(await registerDevCommands(client,devOnlyCommands,devGuildIds),await registerGlobalCommands(client,globalCommands))}__name(handleRegistration,"handleRegistration");async function registerGlobalCommands(client,commands){let appCommandsManager=client.application.commands;await appCommandsManager.fetch();for(let command of commands){let targetCommand=appCommandsManager.cache.find(cmd=>cmd.name===command.data.name);if(command.options?.deleted){targetCommand?(await targetCommand.delete().catch(error=>{throw new Error(colors_default.red(`Failed to delete command "${command.data.name}" globally.
|
|
4
|
+
`),error)}),console.log(colors_default.green(`Deleted command "${command.data.name}" globally.`))):process.emitWarning(colors_default.yellow(`Ignoring: Command "${command.data.name}" is globally marked as deleted.`));continue}if(targetCommand&&areSlashCommandsDifferent(targetCommand,command.data)){await targetCommand.edit(command.data).catch(error=>{throw new Error(colors_default.red(`Failed to edit command "${command.data.name}" globally.
|
|
5
|
+
`),error)}),console.log(colors_default.green(`Edited command "${command.data.name}" globally.`));continue}targetCommand||(await appCommandsManager.create(command.data).catch(error=>{throw new Error(colors_default.red(`Failed to register command "${command.data.name}" globally.
|
|
6
|
+
`),error)}),console.log(colors_default.green(`Registered command "${command.data.name}" globally.`)))}}__name(registerGlobalCommands,"registerGlobalCommands");async function registerDevCommands(client,commands,guildIds){let devGuilds=[];for(let guildId of guildIds){let guild=client.guilds.cache.get(guildId)||await client.guilds.fetch(guildId);if(!guild){process.emitWarning(colors_default.yellow(`Ignoring: Guild ${guildId} doesn't exist or client isn't part of the guild.`));continue}devGuilds.push(guild)}let guildCommandsManagers=[];for(let guild of devGuilds){let guildCommandsManager=guild.commands;await guildCommandsManager.fetch(),guildCommandsManagers.push(guildCommandsManager)}for(let command of commands)for(let guildCommands of guildCommandsManagers){let targetCommand=guildCommands.cache.find(cmd=>cmd.name===command.data.name);if(command.options?.deleted){targetCommand?(await targetCommand.delete().catch(error=>{throw new Error(colors_default.red(`Failed to delete command "${command.data.name}" in ${guildCommands.guild.name}.`),error)}),console.log(colors_default.green(`Deleted command "${command.data.name}" in ${guildCommands.guild.name}.`))):process.emitWarning(colors_default.yellow(`Ignoring: Command "${command.data.name}" is marked as deleted in ${guildCommands.guild.name}.`));continue}if(targetCommand&&areSlashCommandsDifferent(targetCommand,command.data)){await targetCommand.edit(command.data).catch(error=>{throw new Error(colors_default.red(`Failed to edit command "${command.data.name}" in ${guildCommands.guild.name}.
|
|
7
|
+
`),error)}),console.log(colors_default.green(`Edited command "${command.data.name}" in ${guildCommands.guild.name}.`));continue}targetCommand||(await guildCommands.create(command.data).catch(error=>{throw new Error(colors_default.red(`Failed to register command "${command.data.name}" in ${guildCommands.guild.name}.
|
|
8
|
+
`),error)}),console.log(colors_default.green(`Registered command "${command.data.name}" in ${guildCommands.guild.name}.`)))}}__name(registerDevCommands,"registerDevCommands");function devOnly_default({interaction,targetCommand,handlerData}){if(!interaction.isAutocomplete()&&targetCommand.options?.devOnly){if(interaction.inGuild()&&!handlerData.devGuildIds.includes(interaction.guildId))return interaction.reply({content:"\u274C This command can only be used inside development servers.",ephemeral:!0}),!0;let memberRoles=interaction.guild?.members.cache.get(interaction.user.id)?.roles.cache,hasDevRole=!1;if(memberRoles?.forEach(role=>{handlerData.devRoleIds.includes(role.id)&&(hasDevRole=!0)}),!(handlerData.devUserIds.includes(interaction.user.id)||hasDevRole))return interaction.reply({content:"\u274C This command can only be used by developers.",ephemeral:!0}),!0}}__name(devOnly_default,"default");var import_discord=require("discord.js");function permissions_default({interaction,targetCommand}){if(interaction.isAutocomplete())return;let userPermissions=interaction.memberPermissions,userPermissionsRequired=targetCommand.options?.userPermissions,missingUserPermissions=[];typeof userPermissionsRequired=="string"&&(userPermissionsRequired=[userPermissionsRequired]);let botPermissions=interaction.guild?.members.me?.permissions,botPermissionsRequired=targetCommand.options?.botPermissions,missingBotPermissions=[];if(typeof botPermissionsRequired=="string"&&(botPermissionsRequired=[botPermissionsRequired]),!userPermissionsRequired?.length&&!botPermissionsRequired?.length)return;if(userPermissions&&userPermissionsRequired)for(let permission of userPermissionsRequired)userPermissions.has(permission)||missingUserPermissions.push(permission);if(botPermissions&&botPermissionsRequired)for(let permission of botPermissionsRequired)botPermissions.has(permission)||missingBotPermissions.push(permission);if(!missingUserPermissions.length&&!missingBotPermissions.length)return;let pattern=/([a-z])([A-Z])|([A-Z]+)([A-Z][a-z])/g;missingUserPermissions=missingUserPermissions.map(str=>str.replace(pattern,"$1$3 $2$4")),missingBotPermissions=missingBotPermissions.map(str=>str.replace(pattern,"$1$3 $2$4"));let embedDescription="",formatter=new Intl.ListFormat("en",{style:"long",type:"conjunction"}),getPermissionWord=__name(permissions=>permissions.length===1?"permission":"permissions","getPermissionWord");if(missingUserPermissions.length){let formattedPermissions=missingUserPermissions.map(p=>`\`${p}\``),permissionsString=formatter.format(formattedPermissions);embedDescription+=`- You must have the ${permissionsString} ${getPermissionWord(missingUserPermissions)} to be able to run this command.
|
|
9
|
+
`}if(missingBotPermissions.length){let formattedPermissions=missingBotPermissions.map(p=>`\`${p}\``),permissionsString=formatter.format(formattedPermissions);embedDescription+=`- I must have the ${permissionsString} ${getPermissionWord(missingBotPermissions)} to be able to execute this command.
|
|
10
|
+
`}let embed=new import_discord.EmbedBuilder().setTitle(":x: Missing permissions!").setDescription(embedDescription).setColor("Red");return interaction.reply({embeds:[embed],ephemeral:!0}),!0}__name(permissions_default,"default");var validations_default=[devOnly_default,permissions_default];var import_async_hooks=require("async_hooks");var CommandHandler=class{static{__name(this,"CommandHandler")}#data;context=null;constructor({...options}){this.#data={...options,builtInValidations:[],commands:[]}}async init(){this.#data.enableHooks&&!this.context&&(this.context=new import_async_hooks.AsyncLocalStorage),await this.#buildCommands(),this.#buildBuiltInValidations();let devOnlyCommands=this.#data.commands.filter(cmd=>cmd.options?.devOnly);devOnlyCommands.length&&!this.#data.devGuildIds.length&&process.emitWarning(colors_default.yellow('You have commands marked as "devOnly", but "devGuildIds" have not been set.')),devOnlyCommands.length&&!this.#data.devUserIds.length&&!this.#data.devRoleIds.length&&process.emitWarning(colors_default.yellow('You have commands marked as "devOnly", but "devUserIds" or "devRoleIds" have not been set.')),this.#data.bulkRegister?await loadCommandsWithRest({client:this.#data.client,devGuildIds:this.#data.devGuildIds,commands:this.#data.commands}):await registerCommands({client:this.#data.client,devGuildIds:this.#data.devGuildIds,commands:this.#data.commands}),this.handleCommands()}async#buildCommands(){let allowedExtensions=/\.(js|mjs|cjs|ts)$/i,commandFilePaths=(await getFilePaths(this.#data.commandsPath,!0)).filter(path3=>allowedExtensions.test(path3));for(let commandFilePath of commandFilePaths){let importedObj=await import(`${toFileURL(commandFilePath)}?t=${Date.now()}`),commandObj=clone(importedObj);typeof module<"u"&&typeof require<"u"&&delete require.cache[require.resolve(commandFilePath)];let compactFilePath=commandFilePath.split(process.cwd())[1]||commandFilePath;if(commandObj.default&&(commandObj=commandObj.default),importedObj.default?commandObj.data=importedObj.default.data:commandObj.data=importedObj.data,!commandObj.data){process.emitWarning(colors_default.yellow(`Ignoring: Command file ${compactFilePath} does not export "data".`));continue}if(!commandObj.data.name){process.emitWarning(colors_default.yellow(`Ignoring: Command file ${compactFilePath} does not export "data.name".`));continue}if(!commandObj.run){process.emitWarning(colors_default.yellow(`Ignoring: Command file ${commandObj.data.name} does not export "run".`));continue}if(typeof commandObj.run!="function"){process.emitWarning(colors_default.yellow(`Ignoring: Command file ${commandObj.data.name} does not export "run" as a function.`));continue}commandObj.filePath=commandFilePath;let commandCategory=commandFilePath.split(this.#data.commandsPath)[1]?.replace(/\\\\|\\/g,"/").split("/")[1]||null;commandCategory&&allowedExtensions.test(commandCategory)?commandObj.category=null:commandObj.category=commandCategory,this.#data.commands.push(commandObj)}}#buildBuiltInValidations(){for(let builtInValidationFunction of validations_default)this.#data.builtInValidations.push(builtInValidationFunction)}handleCommands(){let areHooksEnabled=this.#data.enableHooks;this.#data.client.on("interactionCreate",async interaction=>{if(!interaction.isChatInputCommand()&&!interaction.isContextMenuCommand()&&!interaction.isAutocomplete())return;let isAutocomplete=interaction.isAutocomplete(),targetCommand=this.#data.commands.find(cmd=>cmd.data.name===interaction.commandName);if(!targetCommand)return;let{data,options,run,autocomplete,...rest}=targetCommand;if(isAutocomplete&&!autocomplete)return;let executor=__name(async()=>{let commandObj={data:targetCommand.data,options:targetCommand.options,...rest};if(this.#data.validationHandler){let canRun2=!0;for(let validationFunction of this.#data.validationHandler.validations)if(await validationFunction({interaction,commandObj,client:this.#data.client,handler:this.#data.commandkitInstance})){canRun2=!1;break}if(!canRun2)return}let canRun=!0;if(!this.#data.skipBuiltInValidations){for(let validation of this.#data.builtInValidations)if(validation({targetCommand,interaction,handlerData:this.#data})){canRun=!1;break}}if(!canRun)return;let command=targetCommand[isAutocomplete?"autocomplete":"run"];if(!areHooksEnabled){let context2={interaction,client:this.#data.client,handler:this.#data.commandkitInstance};return await command(context2)}return command()},"executor");return this.context?this.context.run({command:targetCommand.data,interaction},executor):executor()})}get commands(){return this.#data.commands}async reloadCommands(type){if(!this.#data.commandsPath)throw new Error(colors_default.red('Cannot reload commands as "commandsPath" was not provided when instantiating CommandKit.'));this.#data.commands=[],await this.#buildCommands(),this.#data.bulkRegister?await loadCommandsWithRest({client:this.#data.client,devGuildIds:this.#data.devGuildIds,commands:this.#data.commands,reloading:!0,type}):await registerCommands({client:this.#data.client,devGuildIds:this.#data.devGuildIds,commands:this.#data.commands,reloading:!0,type})}};var EventHandler=class{static{__name(this,"EventHandler")}#data;constructor({...options}){this.#data={...options,events:[]}}async init(){await this.#buildEvents(),this.#registerEvents()}async#buildEvents(){let eventFolderPaths=await getFolderPaths(this.#data.eventsPath);for(let eventFolderPath of eventFolderPaths){let eventName=eventFolderPath.replace(/\\\\|\\/g,"/").split("/").pop(),allowedExtensions=/\.(js|mjs|cjs|ts)$/i,eventFilePaths=(await getFilePaths(eventFolderPath,!0)).filter(path3=>allowedExtensions.test(path3)),eventObj={name:eventName,functions:[]};this.#data.events.push(eventObj);for(let eventFilePath of eventFilePaths){let importedFunction=(await import(`${toFileURL(eventFilePath)}?t=${Date.now()}`)).default,eventFunction=clone(importedFunction);typeof module<"u"&&typeof require<"u"&&delete require.cache[require.resolve(eventFilePath)],eventFunction?.default&&(eventFunction=eventFunction.default);let compactFilePath=eventFilePath.split(process.cwd())[1]||eventFilePath;if(typeof eventFunction!="function"){process.emitWarning(colors_default.yellow(`Ignoring: Event file ${compactFilePath} does not export a function.`));continue}eventObj.functions.push(eventFunction)}}}#registerEvents(){let client=this.#data.client,handler=this.#data.commandKitInstance;for(let eventObj of this.#data.events)client.on(eventObj.name,async(...params)=>{for(let eventFunction of eventObj.functions)if(await eventFunction(...params,client,handler))break})}get events(){return this.#data.events}async reloadEvents(commandHandler){if(!this.#data.eventsPath)throw new Error(colors_default.red('Cannot reload events as "eventsPath" was not provided when instantiating CommandKit.'));this.#data.events=[],await this.#buildEvents(),this.#data.client.removeAllListeners(),this.#registerEvents(),commandHandler?.handleCommands()}};var ValidationHandler=class{static{__name(this,"ValidationHandler")}#data;constructor({...options}){this.#data={...options,validations:[]}}async init(){this.#data.validations=await this.#buildValidations()}async#buildValidations(){let allowedExtensions=/\.(js|mjs|cjs|ts)$/i,validationFilePaths=(await getFilePaths(this.#data.validationsPath,!0)).filter(path3=>allowedExtensions.test(path3)),validationFunctions=[];for(let validationFilePath of validationFilePaths){let importedFunction=(await import(`${toFileURL(validationFilePath)}?t=${Date.now()}`)).default,validationFunction=clone(importedFunction);typeof module<"u"&&typeof require<"u"&&delete require.cache[require.resolve(validationFilePath)],validationFunction?.default&&(validationFunction=validationFunction.default);let compactFilePath=validationFilePath.split(process.cwd())[1]||validationFilePath;if(typeof validationFunction!="function"){process.emitWarning(colors_default.yellow(`Ignoring: Validation file ${compactFilePath} does not export a function.`));continue}validationFunctions.push(validationFunction)}return validationFunctions}get validations(){return this.#data.validations}async reloadValidations(){if(!this.#data.validationsPath)throw new Error(colors_default.red('Cannot reload validations as "validationsPath" was not provided when instantiating CommandKit.'));let newValidations=await this.#buildValidations();this.#data.validations=newValidations}};var CommandKit=class _CommandKit{static{__name(this,"CommandKit")}#data;static _instance=null;constructor(options){if(!options.client)throw new Error(colors_default.red('"client" is required when instantiating CommandKit.'));if(options.validationsPath&&!options.commandsPath)throw new Error(colors_default.red('"commandsPath" is required when "validationsPath" is set.'));this.#data=options,_CommandKit._instance=this,this.#init()}get client(){return this.#data.client}get commandHandler(){return this.#data.commandHandler}async#init(){if(this.#data.eventsPath){let eventHandler=new EventHandler({client:this.#data.client,eventsPath:this.#data.eventsPath,commandKitInstance:this});await eventHandler.init(),this.#data.eventHandler=eventHandler}if(this.#data.validationsPath){let validationHandler=new ValidationHandler({validationsPath:this.#data.validationsPath});await validationHandler.init(),this.#data.validationHandler=validationHandler}if(this.#data.commandsPath){let commandHandler=new CommandHandler({client:this.#data.client,commandsPath:this.#data.commandsPath,devGuildIds:this.#data.devGuildIds||[],devUserIds:this.#data.devUserIds||[],devRoleIds:this.#data.devRoleIds||[],validationHandler:this.#data.validationHandler,skipBuiltInValidations:this.#data.skipBuiltInValidations||!1,commandkitInstance:this,bulkRegister:this.#data.bulkRegister||!1,enableHooks:this.#data.experimental?.hooks??!1});await commandHandler.init(),this.#data.commandHandler=commandHandler}}async reloadCommands(type){this.#data.commandHandler&&await this.#data.commandHandler.reloadCommands(type)}async reloadEvents(){this.#data.eventHandler&&await this.#data.eventHandler.reloadEvents(this.#data.commandHandler)}async reloadValidations(){this.#data.validationHandler&&await this.#data.validationHandler.reloadValidations()}get commands(){return this.#data.commandHandler?this.#data.commandHandler.commands.map(cmd=>{let{run,autocomplete,...command}=cmd;return command}):[]}get commandsPath(){return this.#data.commandsPath}get eventsPath(){return this.#data.eventsPath}get validationsPath(){return this.#data.validationsPath}get devUserIds(){return this.#data.devUserIds||[]}get devGuildIds(){return this.#data.devGuildIds||[]}get devRoleIds(){return this.#data.devRoleIds||[]}};var import_discord2=require("discord.js");var ButtonKit=class extends import_discord2.ButtonBuilder{static{__name(this,"ButtonKit")}#onClickHandler=null;#onEndHandler=null;#contextData=null;#collector=null;onClick(handler,data){if(this.data.style===import_discord2.ButtonStyle.Link)throw new TypeError('Cannot setup "onClick" handler on link buttons.');if(!handler)throw new TypeError('Cannot setup "onClick" without a handler function parameter.');return this.#destroyCollector(),this.#onClickHandler=handler,data&&(this.#contextData=data),this.#setupInteractionCollector(),this}onEnd(handler){if(!handler)throw new TypeError('Cannot setup "onEnd" without a handler function parameter.');return this.#onEndHandler=handler,this}#setupInteractionCollector(){if(!this.#contextData||!this.#onClickHandler)return;let message=this.#contextData.message;if(!message)throw new TypeError('Cannot setup "onClick" handler without a message in the context data.');if("customId"in this.data&&!this.data.customId)throw new TypeError('Cannot setup "onClick" handler on a button without a custom id.');let data={time:864e5,autoReset:!0,...this.#contextData},collector=this.#collector=message.createMessageComponentCollector({filter:interaction=>interaction.customId===this.data.custom_id&&interaction.message.id===message.id,componentType:import_discord2.ComponentType.Button,...data});this.#collector.on("collect",interaction=>{let handler=this.#onClickHandler;return handler?this.#collector?(data.autoReset&&this.#collector.resetTimer(),handler(interaction)):collector.stop("destroyed"):this.#destroyCollector()}),this.#collector.on("end",()=>{this.#destroyCollector(),this.#onEndHandler?.()})}#destroyCollector(){this.#collector?.stop("end"),this.#collector?.removeAllListeners(),this.#collector=null,this.#contextData=null,this.#onClickHandler=null}};var globalConfig={envExtra:!0,outDir:"dist",watch:!0,clearRestartLogs:!0,minify:!1,sourcemap:!1,nodeOptions:[],antiCrash:!0,requirePolyfill:!0};function getConfig(){return globalConfig}__name(getConfig,"getConfig");var requiredProps=["src","main"];function defineConfig(config){for(let prop of requiredProps)if(!config[prop])throw new Error(`[CommandKit Config] Missing required config property: ${prop}`);return globalConfig={...globalConfig,...config},globalConfig}__name(defineConfig,"defineConfig");var context=[];function createSignal(value){let subscribers=new Set,disposed=!1,val=value instanceof Function?value():value;return[__name(()=>{if(!disposed){let running=getCurrentObserver();running&&subscribers.add(running)}return val},"getter"),__name(newValue=>{if(!disposed){val=newValue instanceof Function?newValue(val):newValue;for(let subscriber of subscribers)subscriber()}},"setter"),__name(()=>{subscribers.clear(),disposed=!0},"dispose")]}__name(createSignal,"createSignal");function createEffect(callback){let execute=__name(()=>{context.push(execute);try{callback()}finally{context.pop()}},"execute");execute()}__name(createEffect,"createEffect");function getCurrentObserver(){return context[context.length-1]}__name(getCurrentObserver,"getCurrentObserver");function getCommandKit(){return CommandKit._instance}__name(getCommandKit,"getCommandKit");function getCommandHandler(){let handler=getCommandKit()?.commandHandler;if(!handler)throw new Error("CommandKit is not initialized.");return handler}__name(getCommandHandler,"getCommandHandler");function getContext(){let info=getCommandHandler().context;if(!info)throw new Error("Context is not available, did you forget to enable hooks?");return info}__name(getContext,"getContext");function prepareHookInvocationError(name){return new Error(`Cannot invoke hook "${name}" outside of a command.`)}__name(prepareHookInvocationError,"prepareHookInvocationError");function useCommandKit(){let kit=getCommandKit();if(!kit)throw new Error("CommandKit is not initialized.");return kit}__name(useCommandKit,"useCommandKit");function useClient(){return useCommandKit().client}__name(useClient,"useClient");function useInteraction(){let data=getContext().getStore();if(!data)throw prepareHookInvocationError("useInteraction");return data.interaction}__name(useInteraction,"useInteraction");async function response(data){let interaction=useInteraction();interaction.isAutocomplete()||(interaction.replied||interaction.deferred?await interaction.editReply(data):interaction.isMessageComponent()?await interaction.update(data):await interaction.reply(data))}__name(response,"response");function useChannel(){let data=getContext().getStore();if(!data)throw prepareHookInvocationError("useChannel");return data.interaction.channel}__name(useChannel,"useChannel");function useCommandData(){let data=getContext().getStore();if(!data)throw prepareHookInvocationError("useCommandData");return data.command}__name(useCommandData,"useCommandData");function useGuild(){let data=getContext().getStore();if(!data)throw prepareHookInvocationError("useGuild");return data.interaction.guild}__name(useGuild,"useGuild");function useMember(){let data=getContext().getStore();if(!data)throw prepareHookInvocationError("useMember");return data.interaction.member}__name(useMember,"useMember");function useUser(){let data=getContext().getStore();if(!data)throw prepareHookInvocationError("useUser");return data.interaction.user}__name(useUser,"useUser");0&&(module.exports={ButtonKit,CommandKit,createEffect,createSignal,defineConfig,getConfig,response,useChannel,useClient,useCommandData,useCommandKit,useGuild,useInteraction,useMember,useUser});
|