commandkit 0.1.2 → 0.1.3-dev.20231002193305
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/README.md +9 -7
- package/dist/index.d.mts +74 -7
- package/dist/index.d.ts +74 -7
- package/dist/index.js +495 -281
- package/dist/index.mjs +501 -280
- package/package.json +48 -46
package/dist/index.mjs
CHANGED
|
@@ -1,3 +1,11 @@
|
|
|
1
|
+
var __require = /* @__PURE__ */ ((x) => typeof require !== "undefined" ? require : typeof Proxy !== "undefined" ? new Proxy(x, {
|
|
2
|
+
get: (a, b) => (typeof require !== "undefined" ? require : a)[b]
|
|
3
|
+
}) : x)(function(x) {
|
|
4
|
+
if (typeof require !== "undefined")
|
|
5
|
+
return require.apply(this, arguments);
|
|
6
|
+
throw Error('Dynamic require of "' + x + '" is not supported');
|
|
7
|
+
});
|
|
8
|
+
|
|
1
9
|
// src/utils/get-paths.ts
|
|
2
10
|
import path from "path";
|
|
3
11
|
import fs from "fs";
|
|
@@ -38,7 +46,288 @@ function getFolderPaths(directory, nesting) {
|
|
|
38
46
|
import path2 from "path";
|
|
39
47
|
function toFileURL(filePath) {
|
|
40
48
|
const resolvedPath = path2.resolve(filePath);
|
|
41
|
-
return "file://" + resolvedPath.replace(
|
|
49
|
+
return "file://" + resolvedPath.replace(/\\\\|\\/g, "/");
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
// src/utils/colors.ts
|
|
53
|
+
var resetColor = "\x1B[0m";
|
|
54
|
+
var colors_default = {
|
|
55
|
+
reset: (text) => `${text}${resetColor}`,
|
|
56
|
+
bright: (text) => `\x1B[1m${text}${resetColor}`,
|
|
57
|
+
dim: (text) => `\x1B[2m${text}${resetColor}`,
|
|
58
|
+
underscore: (text) => `\x1B[4m${text}${resetColor}`,
|
|
59
|
+
blink: (text) => `\x1B[5m${text}${resetColor}`,
|
|
60
|
+
reverse: (text) => `\x1B[7m${text}${resetColor}`,
|
|
61
|
+
hidden: (text) => `\x1B[8m${text}${resetColor}`,
|
|
62
|
+
black: (text) => `\x1B[30m${text}${resetColor}`,
|
|
63
|
+
red: (text) => `\x1B[31m${text}${resetColor}`,
|
|
64
|
+
green: (text) => `\x1B[32m${text}${resetColor}`,
|
|
65
|
+
yellow: (text) => `\x1B[33m${text}${resetColor}`,
|
|
66
|
+
blue: (text) => `\x1B[34m${text}${resetColor}`,
|
|
67
|
+
magenta: (text) => `\x1B[35m${text}${resetColor}`,
|
|
68
|
+
cyan: (text) => `\x1B[36m${text}${resetColor}`,
|
|
69
|
+
white: (text) => `\x1B[37m${text}${resetColor}`,
|
|
70
|
+
bgBlack: (text) => `\x1B[40m${text}${resetColor}`,
|
|
71
|
+
bgRed: (text) => `\x1B[41m${text}${resetColor}`,
|
|
72
|
+
bgGreen: (text) => `\x1B[42m${text}${resetColor}`,
|
|
73
|
+
bgYellow: (text) => `\x1B[43m${text}${resetColor}`,
|
|
74
|
+
bgBlue: (text) => `\x1B[44m${text}${resetColor}`,
|
|
75
|
+
bgMagenta: (text) => `\x1B[45m${text}${resetColor}`,
|
|
76
|
+
bgCyan: (text) => `\x1B[46m${text}${resetColor}`,
|
|
77
|
+
bgWhite: (text) => `\x1B[47m${text}${resetColor}`
|
|
78
|
+
};
|
|
79
|
+
|
|
80
|
+
// src/handlers/command-handler/functions/loadCommandsWithRest.ts
|
|
81
|
+
async function loadCommandsWithRest(props) {
|
|
82
|
+
if (props.reloading) {
|
|
83
|
+
if (props.client.isReady()) {
|
|
84
|
+
await handleLoading(
|
|
85
|
+
props.client,
|
|
86
|
+
props.commands,
|
|
87
|
+
props.devGuildIds,
|
|
88
|
+
props.reloading,
|
|
89
|
+
props.type
|
|
90
|
+
);
|
|
91
|
+
} else {
|
|
92
|
+
throw new Error(colors_default.red(`\u274C Cannot reload commands when client is not ready.`));
|
|
93
|
+
}
|
|
94
|
+
} else {
|
|
95
|
+
props.client.once("ready", async (c) => {
|
|
96
|
+
await handleLoading(c, props.commands, props.devGuildIds, props.reloading, props.type);
|
|
97
|
+
});
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
async function handleLoading(client, commands, devGuildIds, reloading, type) {
|
|
101
|
+
const devOnlyCommands = commands.filter((cmd) => cmd.options?.devOnly);
|
|
102
|
+
const globalCommands = commands.filter((cmd) => !cmd.options?.devOnly);
|
|
103
|
+
if (type === "dev") {
|
|
104
|
+
await loadDevCommands(client, devOnlyCommands, devGuildIds, reloading);
|
|
105
|
+
} else if (type === "global") {
|
|
106
|
+
await loadGlobalCommands(client, globalCommands, reloading);
|
|
107
|
+
} else {
|
|
108
|
+
await loadDevCommands(client, devOnlyCommands, devGuildIds, reloading);
|
|
109
|
+
await loadGlobalCommands(client, globalCommands, reloading);
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
async function loadGlobalCommands(client, commands, reloading) {
|
|
113
|
+
const requestBody = commands.map((cmd) => cmd.data);
|
|
114
|
+
await client.application.commands.set(requestBody).catch((error) => {
|
|
115
|
+
console.log(
|
|
116
|
+
colors_default.red(
|
|
117
|
+
`\u274C Error ${reloading ? "reloading" : "loading"} global application commands.
|
|
118
|
+
`
|
|
119
|
+
)
|
|
120
|
+
);
|
|
121
|
+
throw new Error(error);
|
|
122
|
+
});
|
|
123
|
+
console.log(
|
|
124
|
+
colors_default.green(
|
|
125
|
+
`\u2705 ${reloading ? "Reloaded" : "Loaded"} ${requestBody.length} global commands.`
|
|
126
|
+
)
|
|
127
|
+
);
|
|
128
|
+
}
|
|
129
|
+
async function loadDevCommands(client, commands, guildIds, reloading) {
|
|
130
|
+
const requestBody = commands.map((cmd) => cmd.data);
|
|
131
|
+
for (const guildId of guildIds) {
|
|
132
|
+
const targetGuild = client.guilds.cache.get(guildId) || await client.guilds.fetch(guildId);
|
|
133
|
+
if (!targetGuild) {
|
|
134
|
+
console.log(
|
|
135
|
+
`Couldn't ${reloading ? "reloading" : "loading"} commands in guild "${targetGuild}" - guild doesn't exist or client isn't part of the guild.`
|
|
136
|
+
);
|
|
137
|
+
continue;
|
|
138
|
+
}
|
|
139
|
+
await targetGuild.commands.set(requestBody).catch((error) => {
|
|
140
|
+
console.log(
|
|
141
|
+
colors_default.red(
|
|
142
|
+
`\u274C Error ${reloading ? "reloading" : "loading"} developer application commands in guild "${targetGuild?.name || guildId}".
|
|
143
|
+
`
|
|
144
|
+
)
|
|
145
|
+
);
|
|
146
|
+
throw new Error(error);
|
|
147
|
+
});
|
|
148
|
+
console.log(
|
|
149
|
+
colors_default.green(
|
|
150
|
+
`\u2705 ${reloading ? "Reloaded" : "Loaded"} ${requestBody.length} developer commands in guild "${targetGuild.name}".`
|
|
151
|
+
)
|
|
152
|
+
);
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
// src/handlers/command-handler/utils/areSlashCommandsDifferent.ts
|
|
157
|
+
function areSlashCommandsDifferent(appCommand, localCommand) {
|
|
158
|
+
if (!appCommand.options)
|
|
159
|
+
appCommand.options = [];
|
|
160
|
+
if (!localCommand.options)
|
|
161
|
+
localCommand.options = [];
|
|
162
|
+
if (!appCommand.description)
|
|
163
|
+
appCommand.description = "";
|
|
164
|
+
if (!localCommand.description)
|
|
165
|
+
localCommand.description = "";
|
|
166
|
+
if (localCommand.description !== appCommand.description || localCommand.options.length !== appCommand.options.length) {
|
|
167
|
+
return true;
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
// src/handlers/command-handler/functions/registerCommands.ts
|
|
172
|
+
async function registerCommands(props) {
|
|
173
|
+
if (props.reloading) {
|
|
174
|
+
if (props.client.isReady()) {
|
|
175
|
+
await handleRegistration(props.client, props.commands, props.devGuildIds, props.type);
|
|
176
|
+
} else {
|
|
177
|
+
throw new Error(colors_default.red(`\u274C Cannot reload commands when client is not ready.`));
|
|
178
|
+
}
|
|
179
|
+
} else {
|
|
180
|
+
props.client.once("ready", async (c) => {
|
|
181
|
+
await handleRegistration(c, props.commands, props.devGuildIds, props.type);
|
|
182
|
+
});
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
async function handleRegistration(client, commands, devGuildIds, type) {
|
|
186
|
+
const devOnlyCommands = commands.filter((cmd) => cmd.options?.devOnly);
|
|
187
|
+
const globalCommands = commands.filter((cmd) => !cmd.options?.devOnly);
|
|
188
|
+
if (type === "dev") {
|
|
189
|
+
await registerDevCommands(client, devOnlyCommands, devGuildIds);
|
|
190
|
+
} else if (type === "global") {
|
|
191
|
+
await registerGlobalCommands(client, globalCommands);
|
|
192
|
+
} else {
|
|
193
|
+
await registerDevCommands(client, devOnlyCommands, devGuildIds);
|
|
194
|
+
await registerGlobalCommands(client, globalCommands);
|
|
195
|
+
}
|
|
196
|
+
}
|
|
197
|
+
async function registerGlobalCommands(client, commands) {
|
|
198
|
+
const appCommandsManager = client.application.commands;
|
|
199
|
+
await appCommandsManager.fetch();
|
|
200
|
+
for (const command of commands) {
|
|
201
|
+
const targetCommand = appCommandsManager.cache.find(
|
|
202
|
+
(cmd) => cmd.name === command.data.name
|
|
203
|
+
);
|
|
204
|
+
if (command.options?.deleted) {
|
|
205
|
+
if (!targetCommand) {
|
|
206
|
+
console.log(
|
|
207
|
+
colors_default.yellow(
|
|
208
|
+
`\u23E9 Ignoring: Command "${command.data.name}" is globally marked as deleted.`
|
|
209
|
+
)
|
|
210
|
+
);
|
|
211
|
+
} else {
|
|
212
|
+
await targetCommand.delete().catch((error) => {
|
|
213
|
+
console.log(
|
|
214
|
+
colors_default.red(`\u274C Failed to delete command "${command.data.name}" globally.`)
|
|
215
|
+
);
|
|
216
|
+
console.error(error);
|
|
217
|
+
});
|
|
218
|
+
console.log(colors_default.green(`\u{1F6AE} Deleted command "${command.data.name}" globally.`));
|
|
219
|
+
}
|
|
220
|
+
continue;
|
|
221
|
+
}
|
|
222
|
+
if (targetCommand) {
|
|
223
|
+
const commandsAreDifferent = areSlashCommandsDifferent(targetCommand, command.data);
|
|
224
|
+
if (commandsAreDifferent) {
|
|
225
|
+
await targetCommand.edit(command.data).catch((error) => {
|
|
226
|
+
console.log(
|
|
227
|
+
colors_default.red(
|
|
228
|
+
`\u274C Failed to edit command "${command.data.name}" globally.`
|
|
229
|
+
)
|
|
230
|
+
);
|
|
231
|
+
console.error(error);
|
|
232
|
+
});
|
|
233
|
+
console.log(colors_default.green(`\u2705 Edited command "${command.data.name}" globally.`));
|
|
234
|
+
continue;
|
|
235
|
+
}
|
|
236
|
+
}
|
|
237
|
+
if (targetCommand)
|
|
238
|
+
continue;
|
|
239
|
+
await appCommandsManager.create(command.data).catch((error) => {
|
|
240
|
+
console.log(
|
|
241
|
+
colors_default.red(`\u274C Failed to register command "${command.data.name}" globally.`)
|
|
242
|
+
);
|
|
243
|
+
console.error(error);
|
|
244
|
+
});
|
|
245
|
+
console.log(colors_default.green(`\u2705 Registered command "${command.data.name}" globally.`));
|
|
246
|
+
}
|
|
247
|
+
}
|
|
248
|
+
async function registerDevCommands(client, commands, guildIds) {
|
|
249
|
+
const devGuilds = [];
|
|
250
|
+
for (const guildId of guildIds) {
|
|
251
|
+
const guild = client.guilds.cache.get(guildId) || await client.guilds.fetch(guildId);
|
|
252
|
+
if (!guild) {
|
|
253
|
+
console.log(
|
|
254
|
+
colors_default.yellow(
|
|
255
|
+
`\u23E9 Ignoring: Guild ${guildId} does not exist or client isn't in this guild.`
|
|
256
|
+
)
|
|
257
|
+
);
|
|
258
|
+
continue;
|
|
259
|
+
}
|
|
260
|
+
devGuilds.push(guild);
|
|
261
|
+
}
|
|
262
|
+
const guildCommandsManagers = [];
|
|
263
|
+
for (const guild of devGuilds) {
|
|
264
|
+
const guildCommandsManager = guild.commands;
|
|
265
|
+
await guildCommandsManager.fetch();
|
|
266
|
+
guildCommandsManagers.push(guildCommandsManager);
|
|
267
|
+
}
|
|
268
|
+
for (const command of commands) {
|
|
269
|
+
for (const guildCommands of guildCommandsManagers) {
|
|
270
|
+
const targetCommand = guildCommands.cache.find((cmd) => cmd.name === command.data.name);
|
|
271
|
+
if (command.options?.deleted) {
|
|
272
|
+
if (!targetCommand) {
|
|
273
|
+
console.log(
|
|
274
|
+
colors_default.yellow(
|
|
275
|
+
`\u23E9 Ignoring: Command "${command.data.name}" is marked as deleted for ${guildCommands.guild.name}.`
|
|
276
|
+
)
|
|
277
|
+
);
|
|
278
|
+
} else {
|
|
279
|
+
await targetCommand.delete().catch((error) => {
|
|
280
|
+
console.log(
|
|
281
|
+
colors_default.red(
|
|
282
|
+
`\u274C Failed to delete command "${command.data.name}" in ${guildCommands.guild.name}.`
|
|
283
|
+
)
|
|
284
|
+
);
|
|
285
|
+
console.error(error);
|
|
286
|
+
});
|
|
287
|
+
console.log(
|
|
288
|
+
colors_default.green(
|
|
289
|
+
`\u{1F6AE} Deleted command "${command.data.name}" in ${guildCommands.guild.name}.`
|
|
290
|
+
)
|
|
291
|
+
);
|
|
292
|
+
}
|
|
293
|
+
continue;
|
|
294
|
+
}
|
|
295
|
+
if (targetCommand) {
|
|
296
|
+
const commandsAreDifferent = areSlashCommandsDifferent(targetCommand, command.data);
|
|
297
|
+
if (commandsAreDifferent) {
|
|
298
|
+
await targetCommand.edit(command.data).catch((error) => {
|
|
299
|
+
console.log(
|
|
300
|
+
colors_default.red(
|
|
301
|
+
`\u274C Failed to edit command "${command.data.name}" in ${guildCommands.guild.name}.`
|
|
302
|
+
)
|
|
303
|
+
);
|
|
304
|
+
console.error(error);
|
|
305
|
+
});
|
|
306
|
+
console.log(
|
|
307
|
+
colors_default.green(
|
|
308
|
+
`\u2705 Edited command "${command.data.name}" in ${guildCommands.guild.name}.`
|
|
309
|
+
)
|
|
310
|
+
);
|
|
311
|
+
continue;
|
|
312
|
+
}
|
|
313
|
+
}
|
|
314
|
+
if (targetCommand)
|
|
315
|
+
continue;
|
|
316
|
+
await guildCommands.create(command.data).catch((error) => {
|
|
317
|
+
console.log(
|
|
318
|
+
colors_default.red(
|
|
319
|
+
`\u274C Failed to register command "${command.data.name}" in ${guildCommands.guild.name}.`
|
|
320
|
+
)
|
|
321
|
+
);
|
|
322
|
+
console.error(error);
|
|
323
|
+
});
|
|
324
|
+
console.log(
|
|
325
|
+
colors_default.green(
|
|
326
|
+
`\u2705 Registered command "${command.data.name}" in ${guildCommands.guild.name}.`
|
|
327
|
+
)
|
|
328
|
+
);
|
|
329
|
+
}
|
|
330
|
+
}
|
|
42
331
|
}
|
|
43
332
|
|
|
44
333
|
// src/handlers/command-handler/validations/botPermissions.ts
|
|
@@ -82,7 +371,7 @@ function devOnly_default({ interaction, targetCommand, handlerData }) {
|
|
|
82
371
|
const memberRoles = guildMember?.roles.cache;
|
|
83
372
|
let hasDevRole = false;
|
|
84
373
|
memberRoles?.forEach((role) => {
|
|
85
|
-
if (handlerData.devRoleIds
|
|
374
|
+
if (handlerData.devRoleIds.includes(role.id)) {
|
|
86
375
|
hasDevRole = true;
|
|
87
376
|
}
|
|
88
377
|
});
|
|
@@ -138,254 +427,13 @@ function userPermissions_default({ interaction, targetCommand }) {
|
|
|
138
427
|
// src/handlers/command-handler/validations/index.ts
|
|
139
428
|
var validations_default = [botPermissions_default, devOnly_default, guildOnly_default, userPermissions_default];
|
|
140
429
|
|
|
141
|
-
// src/handlers/command-handler/utils/areSlashCommandsDifferent.ts
|
|
142
|
-
function areSlashCommandsDifferent(appCommand, localCommand) {
|
|
143
|
-
if (!appCommand.options)
|
|
144
|
-
appCommand.options = [];
|
|
145
|
-
if (!localCommand.options)
|
|
146
|
-
localCommand.options = [];
|
|
147
|
-
if (!appCommand.description)
|
|
148
|
-
appCommand.description = "";
|
|
149
|
-
if (!localCommand.description)
|
|
150
|
-
localCommand.description = "";
|
|
151
|
-
if (localCommand.description !== appCommand.description || localCommand.options.length !== appCommand.options.length) {
|
|
152
|
-
return true;
|
|
153
|
-
}
|
|
154
|
-
}
|
|
155
|
-
|
|
156
|
-
// src/handlers/command-handler/functions/registerCommands.ts
|
|
157
|
-
import colors from "colors/safe";
|
|
158
|
-
async function registerCommands(commandHandler) {
|
|
159
|
-
const client = commandHandler._data.client;
|
|
160
|
-
const devGuildIds = commandHandler._data.devGuildIds;
|
|
161
|
-
const commands = commandHandler._data.commands;
|
|
162
|
-
client.once("ready", async () => {
|
|
163
|
-
const devGuilds = [];
|
|
164
|
-
for (const devGuildId of devGuildIds) {
|
|
165
|
-
const guild = client.guilds.cache.get(devGuildId);
|
|
166
|
-
if (!guild) {
|
|
167
|
-
console.log(
|
|
168
|
-
colors.yellow(
|
|
169
|
-
`\u23E9 Ignoring: Guild ${devGuildId} does not exist or client isn't in this guild.`
|
|
170
|
-
)
|
|
171
|
-
);
|
|
172
|
-
continue;
|
|
173
|
-
}
|
|
174
|
-
devGuilds.push(guild);
|
|
175
|
-
}
|
|
176
|
-
const appCommands = client.application?.commands;
|
|
177
|
-
await appCommands?.fetch();
|
|
178
|
-
const devGuildCommands = [];
|
|
179
|
-
for (const guild of devGuilds) {
|
|
180
|
-
const guildCommands = guild.commands;
|
|
181
|
-
await guildCommands?.fetch();
|
|
182
|
-
devGuildCommands.push(guildCommands);
|
|
183
|
-
}
|
|
184
|
-
for (const command of commands) {
|
|
185
|
-
let commandData = command.data;
|
|
186
|
-
if (command.options?.deleted) {
|
|
187
|
-
const targetCommand = appCommands?.cache.find(
|
|
188
|
-
(cmd) => cmd.name === commandData.name
|
|
189
|
-
);
|
|
190
|
-
if (!targetCommand) {
|
|
191
|
-
console.log(
|
|
192
|
-
colors.yellow(
|
|
193
|
-
`\u23E9 Ignoring: Command "${commandData.name}" is globally marked as deleted.`
|
|
194
|
-
)
|
|
195
|
-
);
|
|
196
|
-
} else {
|
|
197
|
-
targetCommand.delete().then(() => {
|
|
198
|
-
console.log(
|
|
199
|
-
colors.green(`\u{1F6AE} Deleted command "${commandData.name}" globally.`)
|
|
200
|
-
);
|
|
201
|
-
});
|
|
202
|
-
}
|
|
203
|
-
for (const guildCommands of devGuildCommands) {
|
|
204
|
-
const targetCommand2 = guildCommands.cache.find(
|
|
205
|
-
(cmd) => cmd.name === commandData.name
|
|
206
|
-
);
|
|
207
|
-
if (!targetCommand2) {
|
|
208
|
-
console.log(
|
|
209
|
-
colors.yellow(
|
|
210
|
-
`\u23E9 Ignoring: Command "${commandData.name}" is marked as deleted for ${guildCommands.guild.name}.`
|
|
211
|
-
)
|
|
212
|
-
);
|
|
213
|
-
} else {
|
|
214
|
-
targetCommand2.delete().then(() => {
|
|
215
|
-
console.log(
|
|
216
|
-
colors.green(
|
|
217
|
-
`\u{1F6AE} Deleted command "${commandData.name}" in ${guildCommands.guild.name}.`
|
|
218
|
-
)
|
|
219
|
-
);
|
|
220
|
-
});
|
|
221
|
-
}
|
|
222
|
-
}
|
|
223
|
-
continue;
|
|
224
|
-
}
|
|
225
|
-
let editedCommand = false;
|
|
226
|
-
const appGlobalCommand = appCommands?.cache.find(
|
|
227
|
-
(cmd) => cmd.name === commandData.name
|
|
228
|
-
);
|
|
229
|
-
if (appGlobalCommand) {
|
|
230
|
-
const commandsAreDifferent = areSlashCommandsDifferent(
|
|
231
|
-
appGlobalCommand,
|
|
232
|
-
commandData
|
|
233
|
-
);
|
|
234
|
-
if (commandsAreDifferent) {
|
|
235
|
-
appGlobalCommand.edit(commandData).then(() => {
|
|
236
|
-
console.log(
|
|
237
|
-
colors.green(`\u2705 Edited command "${commandData.name}" globally.`)
|
|
238
|
-
);
|
|
239
|
-
}).catch((error) => {
|
|
240
|
-
console.log(
|
|
241
|
-
colors.red(
|
|
242
|
-
`\u274C Failed to edit command "${commandData.name}" globally.`
|
|
243
|
-
)
|
|
244
|
-
);
|
|
245
|
-
console.error(error);
|
|
246
|
-
});
|
|
247
|
-
editedCommand = true;
|
|
248
|
-
}
|
|
249
|
-
}
|
|
250
|
-
for (const guildCommands of devGuildCommands) {
|
|
251
|
-
const appGuildCommand = guildCommands.cache.find(
|
|
252
|
-
(cmd) => cmd.name === commandData.name
|
|
253
|
-
);
|
|
254
|
-
if (appGuildCommand) {
|
|
255
|
-
const commandsAreDifferent = areSlashCommandsDifferent(
|
|
256
|
-
appGuildCommand,
|
|
257
|
-
commandData
|
|
258
|
-
);
|
|
259
|
-
if (commandsAreDifferent) {
|
|
260
|
-
appGuildCommand.edit(commandData).then(() => {
|
|
261
|
-
console.log(
|
|
262
|
-
colors.green(
|
|
263
|
-
`\u2705 Edited command "${commandData.name}" in ${guildCommands.guild.name}.`
|
|
264
|
-
)
|
|
265
|
-
);
|
|
266
|
-
}).catch((error) => {
|
|
267
|
-
console.log(
|
|
268
|
-
colors.red(
|
|
269
|
-
`\u274C Failed to edit command "${commandData.name}" in ${guildCommands.guild.name}.`
|
|
270
|
-
)
|
|
271
|
-
);
|
|
272
|
-
console.error(error);
|
|
273
|
-
});
|
|
274
|
-
editedCommand = true;
|
|
275
|
-
}
|
|
276
|
-
}
|
|
277
|
-
}
|
|
278
|
-
if (editedCommand)
|
|
279
|
-
continue;
|
|
280
|
-
if (command.options?.devOnly) {
|
|
281
|
-
if (!devGuilds.length) {
|
|
282
|
-
console.log(
|
|
283
|
-
colors.yellow(
|
|
284
|
-
`\u23E9 Ignoring: Cannot register command "${commandData.name}" as no valid "devGuildIds" were provided.`
|
|
285
|
-
)
|
|
286
|
-
);
|
|
287
|
-
continue;
|
|
288
|
-
}
|
|
289
|
-
for (const guild of devGuilds) {
|
|
290
|
-
const cmdExists = guild.commands.cache.some(
|
|
291
|
-
(cmd) => cmd.name === commandData.name
|
|
292
|
-
);
|
|
293
|
-
if (cmdExists)
|
|
294
|
-
continue;
|
|
295
|
-
guild?.commands.create(commandData).then(() => {
|
|
296
|
-
console.log(
|
|
297
|
-
colors.green(
|
|
298
|
-
`\u2705 Registered command "${commandData.name}" in ${guild.name}.`
|
|
299
|
-
)
|
|
300
|
-
);
|
|
301
|
-
}).catch((error) => {
|
|
302
|
-
console.log(
|
|
303
|
-
colors.red(
|
|
304
|
-
`\u274C Failed to register command "${commandData.name}" in ${guild.name}.`
|
|
305
|
-
)
|
|
306
|
-
);
|
|
307
|
-
console.error(error);
|
|
308
|
-
});
|
|
309
|
-
}
|
|
310
|
-
} else {
|
|
311
|
-
const cmdExists = appCommands?.cache.some((cmd) => cmd.name === commandData.name);
|
|
312
|
-
if (cmdExists)
|
|
313
|
-
continue;
|
|
314
|
-
appCommands?.create(commandData).then(() => {
|
|
315
|
-
console.log(
|
|
316
|
-
colors.green(`\u2705 Registered command "${commandData.name}" globally.`)
|
|
317
|
-
);
|
|
318
|
-
}).catch((error) => {
|
|
319
|
-
console.log(
|
|
320
|
-
colors.red(
|
|
321
|
-
`\u274C Failed to register command "${commandData.name}" globally.`
|
|
322
|
-
)
|
|
323
|
-
);
|
|
324
|
-
console.error(error);
|
|
325
|
-
});
|
|
326
|
-
}
|
|
327
|
-
}
|
|
328
|
-
});
|
|
329
|
-
}
|
|
330
|
-
|
|
331
|
-
// src/handlers/command-handler/functions/handleCommands.ts
|
|
332
|
-
function handleCommands(commandHandler) {
|
|
333
|
-
const client = commandHandler._data.client;
|
|
334
|
-
const handler = commandHandler._data.commandKitInstance;
|
|
335
|
-
client.on("interactionCreate", async (interaction) => {
|
|
336
|
-
if (!interaction.isChatInputCommand() && !interaction.isContextMenuCommand())
|
|
337
|
-
return;
|
|
338
|
-
const targetCommand = commandHandler._data.commands.find(
|
|
339
|
-
(cmd) => cmd.data.name === interaction.commandName
|
|
340
|
-
);
|
|
341
|
-
if (!targetCommand)
|
|
342
|
-
return;
|
|
343
|
-
const { data, options, run, ...rest } = targetCommand;
|
|
344
|
-
const commandObj = {
|
|
345
|
-
data: targetCommand.data,
|
|
346
|
-
options: targetCommand.options,
|
|
347
|
-
...rest
|
|
348
|
-
};
|
|
349
|
-
let canRun = true;
|
|
350
|
-
for (const validationFunction of commandHandler._data.customValidations) {
|
|
351
|
-
const stopValidationLoop = await validationFunction({
|
|
352
|
-
interaction,
|
|
353
|
-
client,
|
|
354
|
-
commandObj,
|
|
355
|
-
handler
|
|
356
|
-
});
|
|
357
|
-
if (stopValidationLoop) {
|
|
358
|
-
canRun = false;
|
|
359
|
-
break;
|
|
360
|
-
}
|
|
361
|
-
}
|
|
362
|
-
if (!canRun)
|
|
363
|
-
return;
|
|
364
|
-
if (!commandHandler._data.skipBuiltInValidations) {
|
|
365
|
-
for (const validation of commandHandler._data.builtInValidations) {
|
|
366
|
-
const stopValidationLoop = validation({
|
|
367
|
-
targetCommand,
|
|
368
|
-
interaction,
|
|
369
|
-
handlerData: commandHandler._data
|
|
370
|
-
});
|
|
371
|
-
if (stopValidationLoop) {
|
|
372
|
-
canRun = false;
|
|
373
|
-
break;
|
|
374
|
-
}
|
|
375
|
-
}
|
|
376
|
-
}
|
|
377
|
-
if (!canRun)
|
|
378
|
-
return;
|
|
379
|
-
targetCommand.run({ interaction, client, handler });
|
|
380
|
-
});
|
|
381
|
-
}
|
|
382
|
-
|
|
383
430
|
// src/handlers/command-handler/CommandHandler.ts
|
|
384
|
-
import
|
|
431
|
+
import rdfc from "rfdc";
|
|
432
|
+
var clone = rdfc();
|
|
385
433
|
var CommandHandler = class {
|
|
386
|
-
|
|
434
|
+
#data;
|
|
387
435
|
constructor({ ...options }) {
|
|
388
|
-
this
|
|
436
|
+
this.#data = {
|
|
389
437
|
...options,
|
|
390
438
|
builtInValidations: [],
|
|
391
439
|
commands: []
|
|
@@ -394,54 +442,177 @@ var CommandHandler = class {
|
|
|
394
442
|
async init() {
|
|
395
443
|
await this.#buildCommands();
|
|
396
444
|
this.#buildValidations();
|
|
397
|
-
|
|
445
|
+
const devOnlyCommands = this.#data.commands.filter((cmd) => cmd.options?.devOnly);
|
|
446
|
+
if (devOnlyCommands.length && !this.#data.devGuildIds.length) {
|
|
447
|
+
console.log(
|
|
448
|
+
colors_default.yellow(
|
|
449
|
+
'\u2139\uFE0F Warning: You have commands marked as "devOnly" but "devGuildIds" has not been set.'
|
|
450
|
+
)
|
|
451
|
+
);
|
|
452
|
+
}
|
|
453
|
+
if (devOnlyCommands.length && !this.#data.devUserIds.length && !this.#data.devRoleIds.length) {
|
|
454
|
+
console.log(
|
|
455
|
+
colors_default.yellow(
|
|
456
|
+
'\u2139\uFE0F Warning: You have commands marked as "devOnly" but not "devUserIds" or "devRoleIds" were set.'
|
|
457
|
+
)
|
|
458
|
+
);
|
|
459
|
+
}
|
|
460
|
+
if (this.#data.useRest) {
|
|
461
|
+
await loadCommandsWithRest({
|
|
462
|
+
client: this.#data.client,
|
|
463
|
+
devGuildIds: this.#data.devGuildIds,
|
|
464
|
+
commands: this.#data.commands
|
|
465
|
+
});
|
|
466
|
+
} else {
|
|
467
|
+
await registerCommands({
|
|
468
|
+
client: this.#data.client,
|
|
469
|
+
devGuildIds: this.#data.devGuildIds,
|
|
470
|
+
commands: this.#data.commands
|
|
471
|
+
});
|
|
472
|
+
}
|
|
398
473
|
this.#handleCommands();
|
|
399
474
|
}
|
|
400
475
|
async #buildCommands() {
|
|
401
|
-
const
|
|
402
|
-
|
|
476
|
+
const allowedExtensions = /\.(js|mjs|cjs|ts)$/i;
|
|
477
|
+
const commandFilePaths = getFilePaths(this.#data.commandsPath, true).filter(
|
|
478
|
+
(path3) => allowedExtensions.test(path3)
|
|
403
479
|
);
|
|
404
480
|
for (const commandFilePath of commandFilePaths) {
|
|
405
481
|
const modulePath = toFileURL(commandFilePath);
|
|
406
|
-
let
|
|
482
|
+
let importedObj = await import(`${modulePath}?t=${Date.now()}`);
|
|
483
|
+
let commandObj = clone(importedObj);
|
|
484
|
+
if (typeof module !== "undefined" && typeof __require !== "undefined") {
|
|
485
|
+
delete __require.cache[__require.resolve(commandFilePath)];
|
|
486
|
+
}
|
|
407
487
|
const compactFilePath = commandFilePath.split(process.cwd())[1] || commandFilePath;
|
|
408
488
|
if (commandObj.default)
|
|
409
489
|
commandObj = commandObj.default;
|
|
410
490
|
if (!commandObj.data) {
|
|
411
491
|
console.log(
|
|
412
|
-
|
|
492
|
+
colors_default.yellow(
|
|
413
493
|
`\u23E9 Ignoring: Command ${compactFilePath} does not export "data".`
|
|
414
494
|
)
|
|
415
495
|
);
|
|
416
496
|
continue;
|
|
417
497
|
}
|
|
498
|
+
if (!commandObj.data.name) {
|
|
499
|
+
console.log(
|
|
500
|
+
colors_default.yellow(
|
|
501
|
+
`\u23E9 Ignoring: Command ${compactFilePath} does not export "data.name".`
|
|
502
|
+
)
|
|
503
|
+
);
|
|
504
|
+
continue;
|
|
505
|
+
}
|
|
418
506
|
if (!commandObj.run) {
|
|
419
507
|
console.log(
|
|
420
|
-
|
|
508
|
+
colors_default.yellow(
|
|
509
|
+
`\u23E9 Ignoring: Command ${commandObj.data.name} does not export "run".`
|
|
510
|
+
)
|
|
421
511
|
);
|
|
422
512
|
continue;
|
|
423
513
|
}
|
|
424
|
-
|
|
514
|
+
if (typeof commandObj.run !== "function") {
|
|
515
|
+
console.log(
|
|
516
|
+
colors_default.yellow(
|
|
517
|
+
`\u23E9 Ignoring: Command ${commandObj.data.name} does not export "run" as a function.`
|
|
518
|
+
)
|
|
519
|
+
);
|
|
520
|
+
continue;
|
|
521
|
+
}
|
|
522
|
+
commandObj.filePath = commandFilePath;
|
|
523
|
+
let commandCategory = commandFilePath.split(this.#data.commandsPath)[1]?.replace(/\\\\|\\/g, "/").split("/")[1] || null;
|
|
524
|
+
if (commandCategory && allowedExtensions.test(commandCategory)) {
|
|
525
|
+
commandObj.category = null;
|
|
526
|
+
} else {
|
|
527
|
+
commandObj.category = commandCategory;
|
|
528
|
+
}
|
|
529
|
+
this.#data.commands.push(commandObj);
|
|
425
530
|
}
|
|
426
531
|
}
|
|
427
532
|
#buildValidations() {
|
|
428
533
|
for (const validationFunction of validations_default) {
|
|
429
|
-
this.
|
|
534
|
+
this.#data.builtInValidations.push(validationFunction);
|
|
430
535
|
}
|
|
431
536
|
}
|
|
432
|
-
async #registerCommands() {
|
|
433
|
-
await registerCommands(this);
|
|
434
|
-
}
|
|
435
537
|
#handleCommands() {
|
|
436
|
-
|
|
538
|
+
this.#data.client.on("interactionCreate", async (interaction) => {
|
|
539
|
+
if (!interaction.isChatInputCommand() && !interaction.isContextMenuCommand())
|
|
540
|
+
return;
|
|
541
|
+
const targetCommand = this.#data.commands.find(
|
|
542
|
+
(cmd) => cmd.data.name === interaction.commandName
|
|
543
|
+
);
|
|
544
|
+
if (!targetCommand)
|
|
545
|
+
return;
|
|
546
|
+
const { data, options, run, ...rest } = targetCommand;
|
|
547
|
+
const commandObj = {
|
|
548
|
+
data: targetCommand.data,
|
|
549
|
+
options: targetCommand.options,
|
|
550
|
+
...rest
|
|
551
|
+
};
|
|
552
|
+
let canRun = true;
|
|
553
|
+
for (const validationFunction of this.#data.customValidations) {
|
|
554
|
+
const stopValidationLoop = await validationFunction({
|
|
555
|
+
interaction,
|
|
556
|
+
commandObj,
|
|
557
|
+
client: this.#data.client,
|
|
558
|
+
handler: this.#data.handler
|
|
559
|
+
});
|
|
560
|
+
if (stopValidationLoop) {
|
|
561
|
+
canRun = false;
|
|
562
|
+
break;
|
|
563
|
+
}
|
|
564
|
+
}
|
|
565
|
+
if (!canRun)
|
|
566
|
+
return;
|
|
567
|
+
if (!this.#data.skipBuiltInValidations) {
|
|
568
|
+
for (const validation of this.#data.builtInValidations) {
|
|
569
|
+
const stopValidationLoop = validation({
|
|
570
|
+
targetCommand,
|
|
571
|
+
interaction,
|
|
572
|
+
handlerData: this.#data
|
|
573
|
+
});
|
|
574
|
+
if (stopValidationLoop) {
|
|
575
|
+
canRun = false;
|
|
576
|
+
break;
|
|
577
|
+
}
|
|
578
|
+
}
|
|
579
|
+
}
|
|
580
|
+
if (!canRun)
|
|
581
|
+
return;
|
|
582
|
+
targetCommand.run({
|
|
583
|
+
interaction,
|
|
584
|
+
client: this.#data.client,
|
|
585
|
+
handler: this.#data.handler
|
|
586
|
+
});
|
|
587
|
+
});
|
|
437
588
|
}
|
|
438
589
|
get commands() {
|
|
439
|
-
return this.
|
|
590
|
+
return this.#data.commands;
|
|
591
|
+
}
|
|
592
|
+
async reloadCommands(type) {
|
|
593
|
+
this.#data.commands = [];
|
|
594
|
+
await this.#buildCommands();
|
|
595
|
+
if (this.#data.useRest) {
|
|
596
|
+
await loadCommandsWithRest({
|
|
597
|
+
client: this.#data.client,
|
|
598
|
+
devGuildIds: this.#data.devGuildIds,
|
|
599
|
+
commands: this.#data.commands,
|
|
600
|
+
reloading: true,
|
|
601
|
+
type
|
|
602
|
+
});
|
|
603
|
+
} else {
|
|
604
|
+
await registerCommands({
|
|
605
|
+
client: this.#data.client,
|
|
606
|
+
devGuildIds: this.#data.devGuildIds,
|
|
607
|
+
commands: this.#data.commands,
|
|
608
|
+
reloading: true,
|
|
609
|
+
type
|
|
610
|
+
});
|
|
611
|
+
}
|
|
440
612
|
}
|
|
441
613
|
};
|
|
442
614
|
|
|
443
615
|
// src/handlers/event-handler/EventHandler.ts
|
|
444
|
-
import colors3 from "colors/safe";
|
|
445
616
|
var EventHandler = class {
|
|
446
617
|
#data;
|
|
447
618
|
constructor({ ...options }) {
|
|
@@ -457,7 +628,7 @@ var EventHandler = class {
|
|
|
457
628
|
async #buildEvents() {
|
|
458
629
|
const eventFolderPaths = getFolderPaths(this.#data.eventsPath);
|
|
459
630
|
for (const eventFolderPath of eventFolderPaths) {
|
|
460
|
-
const eventName = eventFolderPath.replace(
|
|
631
|
+
const eventName = eventFolderPath.replace(/\\\\|\\/g, "/").split("/").pop();
|
|
461
632
|
const eventFilePaths = getFilePaths(eventFolderPath, true).filter(
|
|
462
633
|
(path3) => path3.endsWith(".js") || path3.endsWith(".ts")
|
|
463
634
|
);
|
|
@@ -475,7 +646,7 @@ var EventHandler = class {
|
|
|
475
646
|
const compactFilePath = eventFilePath.split(process.cwd())[1] || eventFilePath;
|
|
476
647
|
if (typeof eventFunction !== "function") {
|
|
477
648
|
console.log(
|
|
478
|
-
|
|
649
|
+
colors_default.yellow(
|
|
479
650
|
`\u23E9 Ignoring: Event ${compactFilePath} does not export a function.`
|
|
480
651
|
)
|
|
481
652
|
);
|
|
@@ -505,7 +676,6 @@ var EventHandler = class {
|
|
|
505
676
|
};
|
|
506
677
|
|
|
507
678
|
// src/handlers/validation-handler/ValidationHandler.ts
|
|
508
|
-
import colors4 from "colors/safe";
|
|
509
679
|
var ValidationHandler = class {
|
|
510
680
|
#data;
|
|
511
681
|
constructor({ ...options }) {
|
|
@@ -530,7 +700,7 @@ var ValidationHandler = class {
|
|
|
530
700
|
const compactFilePath = validationFilePath.split(process.cwd())[1] || validationFilePath;
|
|
531
701
|
if (typeof validationFunction !== "function") {
|
|
532
702
|
console.log(
|
|
533
|
-
|
|
703
|
+
colors_default.yellow(
|
|
534
704
|
`\u23E9 Ignoring: Validation ${compactFilePath} does not export a function.`
|
|
535
705
|
)
|
|
536
706
|
);
|
|
@@ -545,22 +715,18 @@ var ValidationHandler = class {
|
|
|
545
715
|
};
|
|
546
716
|
|
|
547
717
|
// src/CommandKit.ts
|
|
548
|
-
import colors5 from "colors/safe";
|
|
549
718
|
var CommandKit = class {
|
|
550
719
|
#data;
|
|
551
|
-
constructor(
|
|
720
|
+
constructor(options) {
|
|
552
721
|
if (!options.client) {
|
|
553
|
-
throw new Error(
|
|
722
|
+
throw new Error(colors_default.red('"client" is required when instantiating CommandKit.'));
|
|
554
723
|
}
|
|
555
724
|
if (options.validationsPath && !options.commandsPath) {
|
|
556
725
|
throw new Error(
|
|
557
|
-
|
|
726
|
+
colors_default.red('"commandsPath" is required when "validationsPath" is set.')
|
|
558
727
|
);
|
|
559
728
|
}
|
|
560
|
-
this.#data =
|
|
561
|
-
...options,
|
|
562
|
-
commands: []
|
|
563
|
-
};
|
|
729
|
+
this.#data = options;
|
|
564
730
|
this.#init();
|
|
565
731
|
}
|
|
566
732
|
async #init() {
|
|
@@ -571,6 +737,7 @@ var CommandKit = class {
|
|
|
571
737
|
commandKitInstance: this
|
|
572
738
|
});
|
|
573
739
|
await eventHandler.init();
|
|
740
|
+
this.#data.eventHandler = eventHandler;
|
|
574
741
|
}
|
|
575
742
|
let validationFunctions = [];
|
|
576
743
|
if (this.#data.validationsPath) {
|
|
@@ -579,6 +746,7 @@ var CommandKit = class {
|
|
|
579
746
|
});
|
|
580
747
|
await validationHandler.init();
|
|
581
748
|
validationHandler.validations.forEach((v) => validationFunctions.push(v));
|
|
749
|
+
this.#data.validationHandler = validationHandler;
|
|
582
750
|
}
|
|
583
751
|
if (this.#data.commandsPath) {
|
|
584
752
|
const commandHandler = new CommandHandler({
|
|
@@ -589,24 +757,71 @@ var CommandKit = class {
|
|
|
589
757
|
devRoleIds: this.#data.devRoleIds || [],
|
|
590
758
|
customValidations: validationFunctions,
|
|
591
759
|
skipBuiltInValidations: this.#data.skipBuiltInValidations || false,
|
|
592
|
-
|
|
760
|
+
handler: this,
|
|
761
|
+
useRest: this.#data.useRest || false
|
|
593
762
|
});
|
|
594
763
|
await commandHandler.init();
|
|
595
|
-
this.#data.
|
|
764
|
+
this.#data.commandHandler = commandHandler;
|
|
596
765
|
}
|
|
597
766
|
}
|
|
598
767
|
/**
|
|
599
|
-
*
|
|
600
|
-
*
|
|
601
|
-
|
|
768
|
+
* Updates application commands with the latest from "commandsPath".
|
|
769
|
+
* @experimental
|
|
770
|
+
*/
|
|
771
|
+
async reloadCommands(type) {
|
|
772
|
+
if (!this.#data.commandHandler)
|
|
773
|
+
return;
|
|
774
|
+
await this.#data.commandHandler.reloadCommands(type);
|
|
775
|
+
}
|
|
776
|
+
/**
|
|
777
|
+
* @returns An array of objects of all the commands that CommandKit is handling.
|
|
602
778
|
*/
|
|
603
779
|
get commands() {
|
|
604
|
-
|
|
780
|
+
if (!this.#data.commandHandler) {
|
|
781
|
+
return [];
|
|
782
|
+
}
|
|
783
|
+
const commands = this.#data.commandHandler.commands.map((cmd) => {
|
|
605
784
|
const { run, ...command } = cmd;
|
|
606
785
|
return command;
|
|
607
786
|
});
|
|
608
787
|
return commands;
|
|
609
788
|
}
|
|
789
|
+
/**
|
|
790
|
+
* @returns The path to the commands folder which was set when instantiating CommandKit.
|
|
791
|
+
*/
|
|
792
|
+
get commandsPath() {
|
|
793
|
+
return this.#data.commandsPath;
|
|
794
|
+
}
|
|
795
|
+
/**
|
|
796
|
+
* @returns The path to the events folder which was set when instantiating CommandKit.
|
|
797
|
+
*/
|
|
798
|
+
get eventsPath() {
|
|
799
|
+
return this.#data.eventsPath;
|
|
800
|
+
}
|
|
801
|
+
/**
|
|
802
|
+
* @returns The path to the validations folder which was set when instantiating CommandKit.
|
|
803
|
+
*/
|
|
804
|
+
get validationsPath() {
|
|
805
|
+
return this.#data.validationsPath;
|
|
806
|
+
}
|
|
807
|
+
/**
|
|
808
|
+
* @returns An array of all the developer user IDs which was set when instantiating CommandKit.
|
|
809
|
+
*/
|
|
810
|
+
get devUserIds() {
|
|
811
|
+
return this.#data.devUserIds || [];
|
|
812
|
+
}
|
|
813
|
+
/**
|
|
814
|
+
* @returns An array of all the developer guild IDs which was set when instantiating CommandKit.
|
|
815
|
+
*/
|
|
816
|
+
get devGuildIds() {
|
|
817
|
+
return this.#data.devGuildIds || [];
|
|
818
|
+
}
|
|
819
|
+
/**
|
|
820
|
+
* @returns An array of all the developer role IDs which was set when instantiating CommandKit.
|
|
821
|
+
*/
|
|
822
|
+
get devRoleIds() {
|
|
823
|
+
return this.#data.devRoleIds || [];
|
|
824
|
+
}
|
|
610
825
|
};
|
|
611
826
|
|
|
612
827
|
// src/types/index.ts
|
|
@@ -616,7 +831,13 @@ var CommandType = /* @__PURE__ */ ((CommandType2) => {
|
|
|
616
831
|
CommandType2[CommandType2["User"] = 2] = "User";
|
|
617
832
|
return CommandType2;
|
|
618
833
|
})(CommandType || {});
|
|
834
|
+
var ReloadType = /* @__PURE__ */ ((ReloadType2) => {
|
|
835
|
+
ReloadType2["Developer"] = "dev";
|
|
836
|
+
ReloadType2["Global"] = "global";
|
|
837
|
+
return ReloadType2;
|
|
838
|
+
})(ReloadType || {});
|
|
619
839
|
export {
|
|
620
840
|
CommandKit,
|
|
621
|
-
CommandType
|
|
841
|
+
CommandType,
|
|
842
|
+
ReloadType
|
|
622
843
|
};
|