commandkit 0.0.9 → 0.0.10
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.d.mts +51 -0
- package/dist/index.d.ts +51 -1
- package/dist/index.js +552 -15
- package/dist/index.mjs +517 -0
- package/package.json +21 -4
- package/.github/workflows/publish.yaml +0 -26
- package/CHANGELOG.md +0 -52
- package/dist/CommandKit.d.ts +0 -36
- package/dist/CommandKit.js +0 -57
- package/dist/handlers/command-handler/CommandHandler.d.ts +0 -12
- package/dist/handlers/command-handler/CommandHandler.js +0 -62
- package/dist/handlers/command-handler/functions/handleCommands.d.ts +0 -2
- package/dist/handlers/command-handler/functions/handleCommands.js +0 -50
- package/dist/handlers/command-handler/functions/registerCommands.d.ts +0 -2
- package/dist/handlers/command-handler/functions/registerCommands.js +0 -135
- package/dist/handlers/command-handler/utils/areSlashCommandsDifferent.d.ts +0 -1
- package/dist/handlers/command-handler/utils/areSlashCommandsDifferent.js +0 -17
- package/dist/handlers/command-handler/validations/botPermissions.d.ts +0 -1
- package/dist/handlers/command-handler/validations/botPermissions.js +0 -17
- package/dist/handlers/command-handler/validations/devOnly.d.ts +0 -1
- package/dist/handlers/command-handler/validations/devOnly.js +0 -29
- package/dist/handlers/command-handler/validations/guildOnly.d.ts +0 -1
- package/dist/handlers/command-handler/validations/guildOnly.js +0 -11
- package/dist/handlers/command-handler/validations/userPermissions.d.ts +0 -1
- package/dist/handlers/command-handler/validations/userPermissions.js +0 -17
- package/dist/handlers/event-handler/EventHandler.d.ts +0 -11
- package/dist/handlers/event-handler/EventHandler.js +0 -52
- package/dist/handlers/index.d.ts +0 -3
- package/dist/handlers/index.js +0 -19
- package/dist/handlers/validation-handler/ValidationHandler.d.ts +0 -7
- package/dist/handlers/validation-handler/ValidationHandler.js +0 -29
- package/dist/utils/get-paths.d.ts +0 -3
- package/dist/utils/get-paths.js +0 -42
- package/tsconfig.json +0 -14
- package/typings.d.ts +0 -63
package/dist/index.d.mts
ADDED
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
import * as discord_js from 'discord.js';
|
|
2
|
+
import { Client } from 'discord.js';
|
|
3
|
+
import * as _discordjs_builders from '@discordjs/builders';
|
|
4
|
+
|
|
5
|
+
interface CommandKitOptions {
|
|
6
|
+
client: Client;
|
|
7
|
+
commandsPath?: string;
|
|
8
|
+
eventsPath?: string;
|
|
9
|
+
validationsPath?: string;
|
|
10
|
+
devGuildIds?: string[];
|
|
11
|
+
devUserIds?: string[];
|
|
12
|
+
devRoleIds?: string[];
|
|
13
|
+
skipBuiltInValidations?: boolean;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
declare class CommandKit {
|
|
17
|
+
#private;
|
|
18
|
+
constructor({ ...options }: CommandKitOptions);
|
|
19
|
+
get commands(): ({
|
|
20
|
+
data: _discordjs_builders.SlashCommandBuilder | {
|
|
21
|
+
name: string;
|
|
22
|
+
name_localizations?: any;
|
|
23
|
+
description: string;
|
|
24
|
+
dm_permission?: boolean | undefined;
|
|
25
|
+
options?: discord_js.APIApplicationCommandOption[] | undefined;
|
|
26
|
+
};
|
|
27
|
+
options?: {
|
|
28
|
+
guildOnly?: boolean | undefined;
|
|
29
|
+
devOnly?: boolean | undefined;
|
|
30
|
+
deleted?: boolean | undefined;
|
|
31
|
+
userPermissions?: discord_js.PermissionResolvable[] | undefined;
|
|
32
|
+
botPermissions?: discord_js.PermissionResolvable[] | undefined;
|
|
33
|
+
} | undefined;
|
|
34
|
+
} | {
|
|
35
|
+
data: _discordjs_builders.ContextMenuCommandBuilder | {
|
|
36
|
+
name: string;
|
|
37
|
+
name_localizations?: any;
|
|
38
|
+
type: _discordjs_builders.ContextMenuCommandType;
|
|
39
|
+
dm_permission?: boolean | undefined;
|
|
40
|
+
};
|
|
41
|
+
options?: {
|
|
42
|
+
guildOnly?: boolean | undefined;
|
|
43
|
+
devOnly?: boolean | undefined;
|
|
44
|
+
deleted?: boolean | undefined;
|
|
45
|
+
userPermissions?: discord_js.PermissionResolvable[] | undefined;
|
|
46
|
+
botPermissions?: discord_js.PermissionResolvable[] | undefined;
|
|
47
|
+
} | undefined;
|
|
48
|
+
})[];
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
export { CommandKit };
|
package/dist/index.d.ts
CHANGED
|
@@ -1 +1,51 @@
|
|
|
1
|
-
|
|
1
|
+
import * as discord_js from 'discord.js';
|
|
2
|
+
import { Client } from 'discord.js';
|
|
3
|
+
import * as _discordjs_builders from '@discordjs/builders';
|
|
4
|
+
|
|
5
|
+
interface CommandKitOptions {
|
|
6
|
+
client: Client;
|
|
7
|
+
commandsPath?: string;
|
|
8
|
+
eventsPath?: string;
|
|
9
|
+
validationsPath?: string;
|
|
10
|
+
devGuildIds?: string[];
|
|
11
|
+
devUserIds?: string[];
|
|
12
|
+
devRoleIds?: string[];
|
|
13
|
+
skipBuiltInValidations?: boolean;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
declare class CommandKit {
|
|
17
|
+
#private;
|
|
18
|
+
constructor({ ...options }: CommandKitOptions);
|
|
19
|
+
get commands(): ({
|
|
20
|
+
data: _discordjs_builders.SlashCommandBuilder | {
|
|
21
|
+
name: string;
|
|
22
|
+
name_localizations?: any;
|
|
23
|
+
description: string;
|
|
24
|
+
dm_permission?: boolean | undefined;
|
|
25
|
+
options?: discord_js.APIApplicationCommandOption[] | undefined;
|
|
26
|
+
};
|
|
27
|
+
options?: {
|
|
28
|
+
guildOnly?: boolean | undefined;
|
|
29
|
+
devOnly?: boolean | undefined;
|
|
30
|
+
deleted?: boolean | undefined;
|
|
31
|
+
userPermissions?: discord_js.PermissionResolvable[] | undefined;
|
|
32
|
+
botPermissions?: discord_js.PermissionResolvable[] | undefined;
|
|
33
|
+
} | undefined;
|
|
34
|
+
} | {
|
|
35
|
+
data: _discordjs_builders.ContextMenuCommandBuilder | {
|
|
36
|
+
name: string;
|
|
37
|
+
name_localizations?: any;
|
|
38
|
+
type: _discordjs_builders.ContextMenuCommandType;
|
|
39
|
+
dm_permission?: boolean | undefined;
|
|
40
|
+
};
|
|
41
|
+
options?: {
|
|
42
|
+
guildOnly?: boolean | undefined;
|
|
43
|
+
devOnly?: boolean | undefined;
|
|
44
|
+
deleted?: boolean | undefined;
|
|
45
|
+
userPermissions?: discord_js.PermissionResolvable[] | undefined;
|
|
46
|
+
botPermissions?: discord_js.PermissionResolvable[] | undefined;
|
|
47
|
+
} | undefined;
|
|
48
|
+
})[];
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
export { CommandKit };
|
package/dist/index.js
CHANGED
|
@@ -1,17 +1,554 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
var
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
o[k2] = m[k];
|
|
12
|
-
}));
|
|
13
|
-
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
-
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
2
|
+
var __create = Object.create;
|
|
3
|
+
var __defProp = Object.defineProperty;
|
|
4
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
7
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
8
|
+
var __export = (target, all) => {
|
|
9
|
+
for (var name in all)
|
|
10
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
15
11
|
};
|
|
16
|
-
|
|
17
|
-
|
|
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
|
+
CommandKit: () => CommandKit
|
|
34
|
+
});
|
|
35
|
+
module.exports = __toCommonJS(src_exports);
|
|
36
|
+
|
|
37
|
+
// src/utils/get-paths.ts
|
|
38
|
+
var import_path = __toESM(require("path"));
|
|
39
|
+
var import_fs = __toESM(require("fs"));
|
|
40
|
+
function getFilePaths(directory, nesting) {
|
|
41
|
+
let filePaths = [];
|
|
42
|
+
if (!directory)
|
|
43
|
+
return filePaths;
|
|
44
|
+
const files = import_fs.default.readdirSync(directory, { withFileTypes: true });
|
|
45
|
+
for (const file of files) {
|
|
46
|
+
const filePath = import_path.default.join(directory, file.name);
|
|
47
|
+
if (file.isFile()) {
|
|
48
|
+
filePaths.push(filePath);
|
|
49
|
+
}
|
|
50
|
+
if (nesting && file.isDirectory()) {
|
|
51
|
+
filePaths = [...filePaths, ...getFilePaths(filePath, true)];
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
return filePaths;
|
|
55
|
+
}
|
|
56
|
+
function getFolderPaths(directory, nesting) {
|
|
57
|
+
let folderPaths = [];
|
|
58
|
+
if (!directory)
|
|
59
|
+
return folderPaths;
|
|
60
|
+
const folders = import_fs.default.readdirSync(directory, { withFileTypes: true });
|
|
61
|
+
for (const folder of folders) {
|
|
62
|
+
const folderPath = import_path.default.join(directory, folder.name);
|
|
63
|
+
if (folder.isDirectory()) {
|
|
64
|
+
folderPaths.push(folderPath);
|
|
65
|
+
if (nesting) {
|
|
66
|
+
folderPaths = [...folderPaths, ...getFolderPaths(folderPath, true)];
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
return folderPaths;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
// src/utils/resolve-file-url.ts
|
|
74
|
+
var import_path2 = __toESM(require("path"));
|
|
75
|
+
function toFileURL(filePath) {
|
|
76
|
+
const resolvedPath = import_path2.default.resolve(filePath);
|
|
77
|
+
return "file://" + resolvedPath.replace(/\\/g, "/");
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
// src/handlers/command-handler/validations/botPermissions.ts
|
|
81
|
+
function botPermissions_default({
|
|
82
|
+
interaction,
|
|
83
|
+
targetCommand
|
|
84
|
+
}) {
|
|
85
|
+
const botMember = interaction.guild?.members.me;
|
|
86
|
+
if (targetCommand.options?.botPermissions && botMember) {
|
|
87
|
+
for (const permission of targetCommand.options.botPermissions) {
|
|
88
|
+
const hasPermission = botMember.permissions.has(permission);
|
|
89
|
+
if (!hasPermission) {
|
|
90
|
+
interaction.reply({
|
|
91
|
+
content: `\u274C I do not have enough permission to execute this command. Required permission: \`${permission}\``,
|
|
92
|
+
ephemeral: true
|
|
93
|
+
});
|
|
94
|
+
return true;
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
// src/handlers/command-handler/validations/devOnly.ts
|
|
101
|
+
function devOnly_default({
|
|
102
|
+
interaction,
|
|
103
|
+
targetCommand,
|
|
104
|
+
handlerData
|
|
105
|
+
}) {
|
|
106
|
+
if (targetCommand.options?.devOnly) {
|
|
107
|
+
if (interaction.inGuild() && !handlerData.devGuildIds.includes(interaction.guildId)) {
|
|
108
|
+
interaction.reply({
|
|
109
|
+
content: "\u274C This command can only be used inside development servers.",
|
|
110
|
+
ephemeral: true
|
|
111
|
+
});
|
|
112
|
+
return true;
|
|
113
|
+
}
|
|
114
|
+
const guildMember = interaction.guild?.members.cache.get(
|
|
115
|
+
interaction.user.id
|
|
116
|
+
);
|
|
117
|
+
const memberRoles = guildMember?.roles.cache;
|
|
118
|
+
let hasDevRole = false;
|
|
119
|
+
memberRoles?.forEach((role) => {
|
|
120
|
+
if (handlerData.devRoleIds?.includes(role.id)) {
|
|
121
|
+
hasDevRole = true;
|
|
122
|
+
}
|
|
123
|
+
});
|
|
124
|
+
const isDevUser = handlerData.devUserIds.includes(interaction.user.id) || hasDevRole;
|
|
125
|
+
if (!isDevUser) {
|
|
126
|
+
interaction.reply({
|
|
127
|
+
content: "\u274C This command can only be used by developers.",
|
|
128
|
+
ephemeral: true
|
|
129
|
+
});
|
|
130
|
+
return true;
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
// src/handlers/command-handler/validations/guildOnly.ts
|
|
136
|
+
function guildOnly_default({
|
|
137
|
+
interaction,
|
|
138
|
+
targetCommand
|
|
139
|
+
}) {
|
|
140
|
+
if (targetCommand.options?.guildOnly && !interaction.inGuild()) {
|
|
141
|
+
interaction.reply({
|
|
142
|
+
content: "\u274C This command can only be used inside a server.",
|
|
143
|
+
ephemeral: true
|
|
144
|
+
});
|
|
145
|
+
return true;
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
// src/handlers/command-handler/validations/userPermissions.ts
|
|
150
|
+
function userPermissions_default({
|
|
151
|
+
interaction,
|
|
152
|
+
targetCommand
|
|
153
|
+
}) {
|
|
154
|
+
const memberPermissions = interaction.memberPermissions;
|
|
155
|
+
if (targetCommand.options?.userPermissions && memberPermissions) {
|
|
156
|
+
for (const permission of targetCommand.options.userPermissions) {
|
|
157
|
+
const hasPermission = memberPermissions.has(permission);
|
|
158
|
+
if (!hasPermission) {
|
|
159
|
+
interaction.reply({
|
|
160
|
+
content: `\u274C You do not have enough permission to run this command. Required permission: \`${permission}\``,
|
|
161
|
+
ephemeral: true
|
|
162
|
+
});
|
|
163
|
+
return true;
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
// src/handlers/command-handler/validations/index.ts
|
|
170
|
+
var validations_default = [botPermissions_default, devOnly_default, guildOnly_default, userPermissions_default];
|
|
171
|
+
|
|
172
|
+
// src/handlers/command-handler/utils/areSlashCommandsDifferent.ts
|
|
173
|
+
function areSlashCommandsDifferent(appCommand, localCommand) {
|
|
174
|
+
if (!appCommand.options)
|
|
175
|
+
appCommand.options = [];
|
|
176
|
+
if (!localCommand.options)
|
|
177
|
+
localCommand.options = [];
|
|
178
|
+
if (!appCommand.description)
|
|
179
|
+
appCommand.description = "";
|
|
180
|
+
if (!localCommand.description)
|
|
181
|
+
localCommand.description = "";
|
|
182
|
+
if (localCommand.description !== appCommand.description || localCommand.options.length !== appCommand.options.length) {
|
|
183
|
+
return true;
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
// src/handlers/command-handler/functions/registerCommands.ts
|
|
188
|
+
var import_colors = require("colors");
|
|
189
|
+
async function registerCommands(commandHandler) {
|
|
190
|
+
const client = commandHandler._data.client;
|
|
191
|
+
const devGuildIds = commandHandler._data.devGuildIds;
|
|
192
|
+
const commands = commandHandler._data.commands;
|
|
193
|
+
client.once("ready", async () => {
|
|
194
|
+
const devGuilds = [];
|
|
195
|
+
for (const devGuildId of devGuildIds) {
|
|
196
|
+
const guild = client.guilds.cache.get(devGuildId);
|
|
197
|
+
if (!guild) {
|
|
198
|
+
console.log(`\u23E9 Ignoring: Guild ${devGuildId} does not exist or client isn't in this guild.`.yellow);
|
|
199
|
+
continue;
|
|
200
|
+
}
|
|
201
|
+
devGuilds.push(guild);
|
|
202
|
+
}
|
|
203
|
+
const appCommands = client.application?.commands;
|
|
204
|
+
await appCommands?.fetch();
|
|
205
|
+
const devGuildCommands = [];
|
|
206
|
+
for (const guild of devGuilds) {
|
|
207
|
+
const guildCommands = guild.commands;
|
|
208
|
+
await guildCommands?.fetch();
|
|
209
|
+
devGuildCommands.push(guildCommands);
|
|
210
|
+
}
|
|
211
|
+
for (const command of commands) {
|
|
212
|
+
if (command.options?.deleted) {
|
|
213
|
+
const targetCommand = appCommands?.cache.find((cmd) => cmd.name === command.data.name);
|
|
214
|
+
if (!targetCommand) {
|
|
215
|
+
console.log(`\u23E9 Ignoring: Command "${command.data.name}" is globally marked as deleted.`.yellow);
|
|
216
|
+
} else {
|
|
217
|
+
targetCommand.delete().then(() => {
|
|
218
|
+
console.log(`\u{1F6AE} Deleted command "${command.data.name}" globally.`.green);
|
|
219
|
+
});
|
|
220
|
+
}
|
|
221
|
+
for (const guildCommands of devGuildCommands) {
|
|
222
|
+
const targetCommand2 = guildCommands.cache.find((cmd) => cmd.name === command.data.name);
|
|
223
|
+
if (!targetCommand2) {
|
|
224
|
+
console.log(
|
|
225
|
+
`\u23E9 Ignoring: Command "${command.data.name}" is marked as deleted for ${guildCommands.guild.name}.`.yellow
|
|
226
|
+
);
|
|
227
|
+
} else {
|
|
228
|
+
targetCommand2.delete().then(() => {
|
|
229
|
+
console.log(
|
|
230
|
+
`\u{1F6AE} Deleted command "${command.data.name}" in ${guildCommands.guild.name}.`.green
|
|
231
|
+
);
|
|
232
|
+
});
|
|
233
|
+
}
|
|
234
|
+
}
|
|
235
|
+
continue;
|
|
236
|
+
}
|
|
237
|
+
let commandData = command.data;
|
|
238
|
+
let editedCommand = false;
|
|
239
|
+
const appGlobalCommand = appCommands?.cache.find((cmd) => cmd.name === command.data.name);
|
|
240
|
+
if (appGlobalCommand) {
|
|
241
|
+
const commandsAreDifferent = areSlashCommandsDifferent(appGlobalCommand, commandData);
|
|
242
|
+
if (commandsAreDifferent) {
|
|
243
|
+
appGlobalCommand.edit(commandData).then(() => {
|
|
244
|
+
console.log(`\u2705 Edited command "${commandData.name}" globally.`.green);
|
|
245
|
+
}).catch((error) => {
|
|
246
|
+
console.log(`\u274C Failed to edit command "${commandData.name}" globally.`.red);
|
|
247
|
+
console.error(error);
|
|
248
|
+
});
|
|
249
|
+
editedCommand = true;
|
|
250
|
+
}
|
|
251
|
+
}
|
|
252
|
+
for (const guildCommands of devGuildCommands) {
|
|
253
|
+
const appGuildCommand = guildCommands.cache.find((cmd) => cmd.name === commandData.name);
|
|
254
|
+
if (appGuildCommand) {
|
|
255
|
+
const commandsAreDifferent = areSlashCommandsDifferent(appGuildCommand, commandData);
|
|
256
|
+
if (commandsAreDifferent) {
|
|
257
|
+
appGuildCommand.edit(commandData).then(() => {
|
|
258
|
+
console.log(
|
|
259
|
+
`\u2705 Edited command "${commandData.name}" in ${guildCommands.guild.name}.`.green
|
|
260
|
+
);
|
|
261
|
+
}).catch((error) => {
|
|
262
|
+
console.log(
|
|
263
|
+
`\u274C Failed to edit command "${commandData.name}" in ${guildCommands.guild.name}.`.red
|
|
264
|
+
);
|
|
265
|
+
console.error(error);
|
|
266
|
+
});
|
|
267
|
+
editedCommand = true;
|
|
268
|
+
}
|
|
269
|
+
}
|
|
270
|
+
}
|
|
271
|
+
if (editedCommand)
|
|
272
|
+
continue;
|
|
273
|
+
if (command.options?.devOnly) {
|
|
274
|
+
if (!devGuilds.length) {
|
|
275
|
+
console.log(
|
|
276
|
+
`\u23E9 Ignoring: Cannot register command "${command.data.name}" as no valid "devGuildIds" were provided.`.yellow
|
|
277
|
+
);
|
|
278
|
+
continue;
|
|
279
|
+
}
|
|
280
|
+
for (const guild of devGuilds) {
|
|
281
|
+
const cmdExists = guild.commands.cache.some((cmd) => cmd.name === command.data.name);
|
|
282
|
+
if (cmdExists)
|
|
283
|
+
continue;
|
|
284
|
+
guild?.commands.create(command.data).then(() => {
|
|
285
|
+
console.log(`\u2705 Registered command "${command.data.name}" in ${guild.name}.`.green);
|
|
286
|
+
}).catch((error) => {
|
|
287
|
+
console.log(`\u274C Failed to register command "${command.data.name}" in ${guild.name}.`.red);
|
|
288
|
+
console.error(error);
|
|
289
|
+
});
|
|
290
|
+
}
|
|
291
|
+
} else {
|
|
292
|
+
const cmdExists = appCommands?.cache.some((cmd) => cmd.name === command.data.name);
|
|
293
|
+
if (cmdExists)
|
|
294
|
+
continue;
|
|
295
|
+
appCommands?.create(command.data).then(() => {
|
|
296
|
+
console.log(`\u2705 Registered command "${command.data.name}" globally.`.green);
|
|
297
|
+
}).catch((error) => {
|
|
298
|
+
console.log(`\u274C Failed to register command "${command.data.name}" globally.`.red);
|
|
299
|
+
console.error(error);
|
|
300
|
+
});
|
|
301
|
+
}
|
|
302
|
+
}
|
|
303
|
+
});
|
|
304
|
+
}
|
|
305
|
+
|
|
306
|
+
// src/handlers/command-handler/functions/handleCommands.ts
|
|
307
|
+
function handleCommands(commandHandler) {
|
|
308
|
+
const client = commandHandler._data.client;
|
|
309
|
+
client.on("interactionCreate", async (interaction) => {
|
|
310
|
+
if (!interaction.isChatInputCommand() && !interaction.isContextMenuCommand())
|
|
311
|
+
return;
|
|
312
|
+
const targetCommand = commandHandler._data.commands.find((cmd) => cmd.data.name === interaction.commandName);
|
|
313
|
+
if (!targetCommand)
|
|
314
|
+
return;
|
|
315
|
+
const { data, options, run, ...rest } = targetCommand;
|
|
316
|
+
const commandObj = {
|
|
317
|
+
data: targetCommand.data,
|
|
318
|
+
options: targetCommand.options,
|
|
319
|
+
...rest
|
|
320
|
+
};
|
|
321
|
+
let canRun = true;
|
|
322
|
+
for (const validationFunction of commandHandler._data.customValidations) {
|
|
323
|
+
const stopValidationLoop = await validationFunction({
|
|
324
|
+
interaction,
|
|
325
|
+
client,
|
|
326
|
+
commandObj
|
|
327
|
+
});
|
|
328
|
+
if (stopValidationLoop) {
|
|
329
|
+
canRun = false;
|
|
330
|
+
break;
|
|
331
|
+
}
|
|
332
|
+
}
|
|
333
|
+
if (!canRun)
|
|
334
|
+
return;
|
|
335
|
+
if (!commandHandler._data.skipBuiltInValidations) {
|
|
336
|
+
for (const validation of commandHandler._data.builtInValidations) {
|
|
337
|
+
const stopValidationLoop = validation({
|
|
338
|
+
targetCommand,
|
|
339
|
+
interaction,
|
|
340
|
+
handlerData: commandHandler._data
|
|
341
|
+
});
|
|
342
|
+
if (stopValidationLoop) {
|
|
343
|
+
canRun = false;
|
|
344
|
+
break;
|
|
345
|
+
}
|
|
346
|
+
}
|
|
347
|
+
}
|
|
348
|
+
if (!canRun)
|
|
349
|
+
return;
|
|
350
|
+
targetCommand.run({ interaction, client });
|
|
351
|
+
});
|
|
352
|
+
}
|
|
353
|
+
|
|
354
|
+
// src/handlers/command-handler/CommandHandler.ts
|
|
355
|
+
var import_colors2 = require("colors");
|
|
356
|
+
var CommandHandler = class {
|
|
357
|
+
_data;
|
|
358
|
+
constructor({ ...options }) {
|
|
359
|
+
this._data = {
|
|
360
|
+
...options,
|
|
361
|
+
builtInValidations: [],
|
|
362
|
+
commands: []
|
|
363
|
+
};
|
|
364
|
+
}
|
|
365
|
+
async init() {
|
|
366
|
+
await this.#buildCommands();
|
|
367
|
+
this.#buildValidations();
|
|
368
|
+
await this.#registerCommands();
|
|
369
|
+
this.#handleCommands();
|
|
370
|
+
}
|
|
371
|
+
async #buildCommands() {
|
|
372
|
+
const commandFilePaths = getFilePaths(this._data.commandsPath, true).filter(
|
|
373
|
+
(path3) => path3.endsWith(".js") || path3.endsWith(".ts")
|
|
374
|
+
);
|
|
375
|
+
for (const commandFilePath of commandFilePaths) {
|
|
376
|
+
const modulePath = toFileURL(commandFilePath);
|
|
377
|
+
let commandObj = await import(modulePath);
|
|
378
|
+
const compactFilePath = commandFilePath.split(process.cwd())[1] || commandFilePath;
|
|
379
|
+
if (commandObj.default)
|
|
380
|
+
commandObj = commandObj.default;
|
|
381
|
+
if (!commandObj.data) {
|
|
382
|
+
console.log(`\u23E9 Ignoring: Command ${compactFilePath} does not export "data".`.yellow);
|
|
383
|
+
continue;
|
|
384
|
+
}
|
|
385
|
+
if (!commandObj.run) {
|
|
386
|
+
console.log(`\u23E9 Ignoring: Command ${compactFilePath} does not export "run".`.yellow);
|
|
387
|
+
continue;
|
|
388
|
+
}
|
|
389
|
+
this._data.commands.push(commandObj);
|
|
390
|
+
}
|
|
391
|
+
}
|
|
392
|
+
#buildValidations() {
|
|
393
|
+
for (const validationFunction of validations_default) {
|
|
394
|
+
this._data.builtInValidations.push(validationFunction);
|
|
395
|
+
}
|
|
396
|
+
}
|
|
397
|
+
async #registerCommands() {
|
|
398
|
+
await registerCommands(this);
|
|
399
|
+
}
|
|
400
|
+
#handleCommands() {
|
|
401
|
+
handleCommands(this);
|
|
402
|
+
}
|
|
403
|
+
get commands() {
|
|
404
|
+
return this._data.commands;
|
|
405
|
+
}
|
|
406
|
+
};
|
|
407
|
+
|
|
408
|
+
// src/handlers/event-handler/EventHandler.ts
|
|
409
|
+
var import_colors3 = require("colors");
|
|
410
|
+
var EventHandler = class {
|
|
411
|
+
#data;
|
|
412
|
+
constructor({ ...options }) {
|
|
413
|
+
this.#data = {
|
|
414
|
+
...options,
|
|
415
|
+
events: []
|
|
416
|
+
};
|
|
417
|
+
}
|
|
418
|
+
async init() {
|
|
419
|
+
await this.#buildEvents();
|
|
420
|
+
this.#registerEvents();
|
|
421
|
+
}
|
|
422
|
+
async #buildEvents() {
|
|
423
|
+
const eventFolderPaths = getFolderPaths(this.#data.eventsPath);
|
|
424
|
+
for (const eventFolderPath of eventFolderPaths) {
|
|
425
|
+
const eventName = eventFolderPath.replace(/\\/g, "/").split("/").pop();
|
|
426
|
+
const eventFilePaths = getFilePaths(eventFolderPath, true).filter(
|
|
427
|
+
(path3) => path3.endsWith(".js") || path3.endsWith(".ts")
|
|
428
|
+
);
|
|
429
|
+
const eventObj = {
|
|
430
|
+
name: eventName,
|
|
431
|
+
functions: []
|
|
432
|
+
};
|
|
433
|
+
this.#data.events.push(eventObj);
|
|
434
|
+
for (const eventFilePath of eventFilePaths) {
|
|
435
|
+
const modulePath = toFileURL(eventFilePath);
|
|
436
|
+
let eventFunction = (await import(modulePath)).default;
|
|
437
|
+
const compactFilePath = eventFilePath.split(process.cwd())[1] || eventFilePath;
|
|
438
|
+
if (typeof eventFunction !== "function") {
|
|
439
|
+
console.log(`\u23E9 Ignoring: Event ${compactFilePath} does not export a function.`.yellow);
|
|
440
|
+
continue;
|
|
441
|
+
}
|
|
442
|
+
eventObj.functions.push(eventFunction);
|
|
443
|
+
}
|
|
444
|
+
}
|
|
445
|
+
}
|
|
446
|
+
#registerEvents() {
|
|
447
|
+
const client = this.#data.client;
|
|
448
|
+
for (const eventObj of this.#data.events) {
|
|
449
|
+
client.on(eventObj.name, async (...params) => {
|
|
450
|
+
for (const eventFunction of eventObj.functions) {
|
|
451
|
+
const stopEventLoop = await eventFunction(...params, client);
|
|
452
|
+
if (stopEventLoop) {
|
|
453
|
+
break;
|
|
454
|
+
}
|
|
455
|
+
}
|
|
456
|
+
});
|
|
457
|
+
}
|
|
458
|
+
}
|
|
459
|
+
get events() {
|
|
460
|
+
return this.#data.events;
|
|
461
|
+
}
|
|
462
|
+
};
|
|
463
|
+
|
|
464
|
+
// src/handlers/validation-handler/ValidationHandler.ts
|
|
465
|
+
var import_colors4 = require("colors");
|
|
466
|
+
var ValidationHandler = class {
|
|
467
|
+
#data;
|
|
468
|
+
constructor({ ...options }) {
|
|
469
|
+
this.#data = {
|
|
470
|
+
...options,
|
|
471
|
+
validations: []
|
|
472
|
+
};
|
|
473
|
+
}
|
|
474
|
+
async init() {
|
|
475
|
+
await this.#buildValidations();
|
|
476
|
+
}
|
|
477
|
+
async #buildValidations() {
|
|
478
|
+
const validationFilePaths = getFilePaths(this.#data.validationsPath, true).filter(
|
|
479
|
+
(path3) => path3.endsWith(".js") || path3.endsWith(".ts")
|
|
480
|
+
);
|
|
481
|
+
for (const validationFilePath of validationFilePaths) {
|
|
482
|
+
const modulePath = toFileURL(validationFilePath);
|
|
483
|
+
let validationFunction = (await import(modulePath)).default;
|
|
484
|
+
const compactFilePath = validationFilePath.split(process.cwd())[1] || validationFilePath;
|
|
485
|
+
if (typeof validationFunction !== "function") {
|
|
486
|
+
console.log(`\u23E9 Ignoring: Validation ${compactFilePath} does not export a function.`.yellow);
|
|
487
|
+
continue;
|
|
488
|
+
}
|
|
489
|
+
this.#data.validations.push(validationFunction);
|
|
490
|
+
}
|
|
491
|
+
}
|
|
492
|
+
get validations() {
|
|
493
|
+
return this.#data.validations;
|
|
494
|
+
}
|
|
495
|
+
};
|
|
496
|
+
|
|
497
|
+
// src/CommandKit.ts
|
|
498
|
+
var import_colors5 = require("colors");
|
|
499
|
+
var CommandKit = class {
|
|
500
|
+
#data;
|
|
501
|
+
constructor({ ...options }) {
|
|
502
|
+
if (!options.client) {
|
|
503
|
+
throw new Error('"client" is required when instantiating CommandKit.'.red);
|
|
504
|
+
}
|
|
505
|
+
if (options.validationsPath && !options.commandsPath) {
|
|
506
|
+
throw new Error('"commandsPath" is required when "validationsPath" is set.'.red);
|
|
507
|
+
}
|
|
508
|
+
this.#data = {
|
|
509
|
+
...options,
|
|
510
|
+
commands: []
|
|
511
|
+
};
|
|
512
|
+
this.#init();
|
|
513
|
+
}
|
|
514
|
+
async #init() {
|
|
515
|
+
if (this.#data.eventsPath) {
|
|
516
|
+
const eventHandler = new EventHandler({
|
|
517
|
+
client: this.#data.client,
|
|
518
|
+
eventsPath: this.#data.eventsPath
|
|
519
|
+
});
|
|
520
|
+
await eventHandler.init();
|
|
521
|
+
}
|
|
522
|
+
let validationFunctions = [];
|
|
523
|
+
if (this.#data.validationsPath) {
|
|
524
|
+
const validationHandler = new ValidationHandler({
|
|
525
|
+
validationsPath: this.#data.validationsPath
|
|
526
|
+
});
|
|
527
|
+
await validationHandler.init();
|
|
528
|
+
validationHandler.validations.forEach((v) => validationFunctions.push(v));
|
|
529
|
+
}
|
|
530
|
+
if (this.#data.commandsPath) {
|
|
531
|
+
const commandHandler = new CommandHandler({
|
|
532
|
+
client: this.#data.client,
|
|
533
|
+
commandsPath: this.#data.commandsPath,
|
|
534
|
+
devGuildIds: this.#data.devGuildIds || [],
|
|
535
|
+
devUserIds: this.#data.devUserIds || [],
|
|
536
|
+
devRoleIds: this.#data.devRoleIds || [],
|
|
537
|
+
customValidations: validationFunctions,
|
|
538
|
+
skipBuiltInValidations: this.#data.skipBuiltInValidations || false
|
|
539
|
+
});
|
|
540
|
+
await commandHandler.init();
|
|
541
|
+
this.#data.commands = commandHandler.commands;
|
|
542
|
+
}
|
|
543
|
+
}
|
|
544
|
+
get commands() {
|
|
545
|
+
return this.#data.commands.map((cmd) => {
|
|
546
|
+
const { run, ...command } = cmd;
|
|
547
|
+
return command;
|
|
548
|
+
});
|
|
549
|
+
}
|
|
550
|
+
};
|
|
551
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
552
|
+
0 && (module.exports = {
|
|
553
|
+
CommandKit
|
|
554
|
+
});
|