commandkit 0.0.8 → 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.
@@ -1,55 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.CommandKit = void 0;
4
- const handlers_1 = require("./handlers");
5
- class CommandKit {
6
- _data;
7
- constructor({ ...options }) {
8
- if (!options.client) {
9
- throw new Error('"client" is required when instantiating CommandKit.');
10
- }
11
- if (options.validationsPath && !options.commandsPath) {
12
- throw new Error('"commandsPath" is required when "validationsPath" is set.');
13
- }
14
- this._data = {
15
- ...options,
16
- commands: [],
17
- };
18
- this._init();
19
- }
20
- _init() {
21
- // Event handler
22
- if (this._data.eventsPath) {
23
- new handlers_1.EventHandler({
24
- client: this._data.client,
25
- eventsPath: this._data.eventsPath,
26
- });
27
- }
28
- // Validation handler
29
- let validationFunctions = [];
30
- if (this._data.validationsPath) {
31
- const validationHandler = new handlers_1.ValidationHandler({
32
- validationsPath: this._data.validationsPath,
33
- });
34
- validationFunctions = validationHandler.getValidations();
35
- }
36
- // Command handler
37
- if (this._data.commandsPath) {
38
- const commandHandler = new handlers_1.CommandHandler({
39
- client: this._data.client,
40
- commandsPath: this._data.commandsPath,
41
- devGuildIds: this._data.devGuildIds || [],
42
- devUserIds: this._data.devUserIds || [],
43
- validations: validationFunctions,
44
- });
45
- this._data.commands = commandHandler.getCommands();
46
- }
47
- }
48
- get commands() {
49
- return this._data.commands.map((cmd) => {
50
- const { run, ...command } = cmd;
51
- return command;
52
- });
53
- }
54
- }
55
- exports.CommandKit = CommandKit;
@@ -1,12 +0,0 @@
1
- import { CommandHandlerData, CommandHandlerOptions } from './typings';
2
- import { ContextCommandObject, SlashCommandObject } from '../../../typings';
3
- export declare class CommandHandler {
4
- _data: CommandHandlerData;
5
- constructor({ ...options }: CommandHandlerOptions);
6
- _init(): void;
7
- _buildCommands(): void;
8
- _registerCommands(): void;
9
- _handleCommands(): void;
10
- _areSlashCommandsDifferent(appCommand: any, localCommand: any): true | undefined;
11
- getCommands(): (SlashCommandObject | ContextCommandObject)[];
12
- }
@@ -1,258 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.CommandHandler = void 0;
4
- const get_paths_1 = require("../../utils/get-paths");
5
- class CommandHandler {
6
- _data;
7
- constructor({ ...options }) {
8
- this._data = {
9
- ...options,
10
- commands: [],
11
- };
12
- this._init();
13
- }
14
- _init() {
15
- this._buildCommands();
16
- this._registerCommands();
17
- this._handleCommands();
18
- }
19
- _buildCommands() {
20
- const commandFilePaths = (0, get_paths_1.getFilePaths)(this._data.commandsPath, true).filter((path) => path.endsWith('.js') || path.endsWith('.ts'));
21
- for (const commandFilePath of commandFilePaths) {
22
- const commandObj = require(commandFilePath);
23
- if (!commandObj.data) {
24
- console.log(`⏩ Ignoring: Command ${commandFilePath} does not export "data".`);
25
- continue;
26
- }
27
- if (!commandObj.run) {
28
- console.log(`⏩ Ignoring: Command ${commandFilePath} does not export "run".`);
29
- continue;
30
- }
31
- this._data.commands.push(commandObj);
32
- }
33
- }
34
- _registerCommands() {
35
- const client = this._data.client;
36
- const commands = this._data.commands;
37
- client.once('ready', async () => {
38
- const devGuilds = [];
39
- for (const devGuildId of this._data.devGuildIds) {
40
- const guild = client.guilds.cache.get(devGuildId);
41
- if (!guild) {
42
- console.log(`⏩ Ignoring: Guild ${devGuildId} does not exist or client isn't in this guild.`);
43
- continue;
44
- }
45
- devGuilds.push(guild);
46
- }
47
- const appCommands = client.application?.commands;
48
- await appCommands?.fetch();
49
- const devGuildCommands = [];
50
- for (const guild of devGuilds) {
51
- const guildCommands = guild.commands;
52
- await guildCommands?.fetch();
53
- devGuildCommands.push(guildCommands);
54
- }
55
- for (const command of commands) {
56
- // <!-- Delete command if options.deleted -->
57
- if (command.options?.deleted) {
58
- const targetCommand = appCommands?.cache.find((cmd) => cmd.name === command.data.name);
59
- if (!targetCommand) {
60
- console.log(`⏩ Ignoring: Command "${command.data.name}" is globally marked as deleted.`);
61
- }
62
- else {
63
- targetCommand.delete().then(() => {
64
- console.log(`🚮 Deleted command "${command.data.name}" globally.`);
65
- });
66
- }
67
- for (const guildCommands of devGuildCommands) {
68
- const targetCommand = guildCommands.cache.find((cmd) => cmd.name === command.data.name);
69
- if (!targetCommand) {
70
- console.log(`⏩ Ignoring: Command "${command.data.name}" is marked as deleted for ${guildCommands.guild.name}.`);
71
- }
72
- else {
73
- targetCommand.delete().then(() => {
74
- console.log(`🚮 Deleted command "${command.data.name}" in ${guildCommands.guild.name}.`);
75
- });
76
- }
77
- }
78
- continue;
79
- }
80
- // <!-- Edit command if there's any changes -->
81
- let commandData = command.data;
82
- let editedCommand = false;
83
- (() => {
84
- // global
85
- const appGlobalCommand = appCommands?.cache.find((cmd) => cmd.name === command.data.name);
86
- if (appGlobalCommand) {
87
- const commandsAreDifferent = this._areSlashCommandsDifferent(appGlobalCommand, commandData);
88
- if (commandsAreDifferent) {
89
- appGlobalCommand
90
- .edit(commandData)
91
- .then(() => {
92
- console.log(`✅ Edited command "${commandData.name}" globally.`);
93
- })
94
- .catch((error) => {
95
- console.log(`❌ Failed to edit command "${commandData.name}" globally.`);
96
- console.error(error);
97
- });
98
- editedCommand = true;
99
- }
100
- }
101
- // guilds
102
- for (const guildCommands of devGuildCommands) {
103
- const appGuildCommand = guildCommands.cache.find((cmd) => cmd.name === commandData.name);
104
- if (appGuildCommand) {
105
- const commandsAreDifferent = this._areSlashCommandsDifferent(appGuildCommand, commandData);
106
- if (commandsAreDifferent) {
107
- appGuildCommand
108
- .edit(commandData)
109
- .then(() => {
110
- console.log(`✅ Edited command "${commandData.name}" in ${guildCommands.guild.name}.`);
111
- })
112
- .catch((error) => {
113
- console.log(`❌ Failed to edit command "${commandData.name}" in ${guildCommands.guild.name}.`);
114
- console.error(error);
115
- });
116
- editedCommand = true;
117
- }
118
- }
119
- }
120
- })();
121
- if (editedCommand)
122
- continue;
123
- // <!-- Registration -->
124
- // guild-based command registration
125
- if (command.options?.devOnly) {
126
- if (!devGuilds.length) {
127
- console.log(`⏩ Ignoring: Cannot register command "${command.data.name}" as no valid "devGuildIds" were provided.`);
128
- continue;
129
- }
130
- for (const guild of devGuilds) {
131
- const cmdExists = guild.commands.cache.some((cmd) => cmd.name === command.data.name);
132
- if (cmdExists)
133
- continue;
134
- guild?.commands
135
- .create(command.data)
136
- .then(() => {
137
- console.log(`✅ Registered command "${command.data.name}" in ${guild.name}.`);
138
- })
139
- .catch((error) => {
140
- console.log(`❌ Failed to register command "${command.data.name}" in ${guild.name}.`);
141
- console.error(error);
142
- });
143
- }
144
- }
145
- // global command registration
146
- else {
147
- const cmdExists = appCommands?.cache.some((cmd) => cmd.name === command.data.name);
148
- if (cmdExists)
149
- continue;
150
- appCommands
151
- ?.create(command.data)
152
- .then(() => {
153
- console.log(`✅ Registered command "${command.data.name}" globally.`);
154
- })
155
- .catch((error) => {
156
- console.log(`❌ Failed to register command "${command.data.name}" globally.`);
157
- console.error(error);
158
- });
159
- }
160
- }
161
- });
162
- }
163
- _handleCommands() {
164
- const client = this._data.client;
165
- client.on('interactionCreate', async (interaction) => {
166
- if (!interaction.isChatInputCommand() && !interaction.isContextMenuCommand())
167
- return;
168
- const targetCommand = this._data.commands.find((cmd) => cmd.data.name === interaction.commandName);
169
- if (!targetCommand)
170
- return;
171
- // Options validation
172
- // options.guildOnly
173
- if (targetCommand.options?.guildOnly && !interaction.inGuild()) {
174
- interaction.reply({
175
- content: '❌ This command can only be used inside a server.',
176
- ephemeral: true,
177
- });
178
- return;
179
- }
180
- // options.devOnly
181
- if (targetCommand.options?.devOnly) {
182
- const isDevUser = this._data.devUserIds.includes(interaction.user.id);
183
- if (!isDevUser) {
184
- interaction.reply({
185
- content: '❌ This command can only be used by developers.',
186
- ephemeral: true,
187
- });
188
- return;
189
- }
190
- }
191
- // options.userPermissions
192
- const memberPermissions = interaction.memberPermissions;
193
- if (targetCommand.options?.userPermissions && memberPermissions) {
194
- for (const permission of targetCommand.options.userPermissions) {
195
- const hasPermission = memberPermissions.has(permission);
196
- if (!hasPermission) {
197
- interaction.reply({
198
- content: `❌ You do not have enough permission to run this command. Required permission: \`${permission}\``,
199
- ephemeral: true,
200
- });
201
- return;
202
- }
203
- }
204
- }
205
- // options.botPermissions
206
- const botMember = interaction.guild?.members.me;
207
- if (targetCommand.options?.botPermissions && botMember) {
208
- for (const permission of targetCommand.options.botPermissions) {
209
- const hasPermission = botMember.permissions.has(permission);
210
- if (!hasPermission) {
211
- interaction.reply({
212
- content: `❌ I do not have enough permission to execute this command. Required permission: \`${permission}\``,
213
- ephemeral: true,
214
- });
215
- return;
216
- }
217
- }
218
- }
219
- // Run user validation functions
220
- const validationFunctions = this._data.validations;
221
- const { data, options, run, ...rest } = targetCommand;
222
- const commandObj = {
223
- data: targetCommand.data,
224
- options: targetCommand.options,
225
- ...rest,
226
- };
227
- let canRun = true;
228
- for (const validationFunction of validationFunctions) {
229
- const stopValidationLoop = await validationFunction({ interaction, client, commandObj });
230
- if (stopValidationLoop) {
231
- canRun = false;
232
- break;
233
- }
234
- }
235
- if (canRun) {
236
- targetCommand.run({ interaction, client });
237
- }
238
- });
239
- }
240
- _areSlashCommandsDifferent(appCommand, localCommand) {
241
- if (!appCommand.options)
242
- appCommand.options = [];
243
- if (!localCommand.options)
244
- localCommand.options = [];
245
- if (!appCommand.description)
246
- appCommand.description = '';
247
- if (!localCommand.description)
248
- localCommand.description = '';
249
- if (localCommand.description !== appCommand.description ||
250
- localCommand.options.length !== appCommand.options.length) {
251
- return true;
252
- }
253
- }
254
- getCommands() {
255
- return this._data.commands;
256
- }
257
- }
258
- exports.CommandHandler = CommandHandler;
@@ -1,11 +0,0 @@
1
- import { EventHandlerOptions, EventHandlerData } from './typings';
2
- export declare class EventHandler {
3
- _data: EventHandlerData;
4
- constructor({ ...options }: EventHandlerOptions);
5
- _buildEvents(): void;
6
- _registerEvents(): void;
7
- getEvents(): {
8
- name: string;
9
- functions: Function[];
10
- }[];
11
- }
@@ -1,52 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.EventHandler = void 0;
4
- const get_paths_1 = require("../../utils/get-paths");
5
- class EventHandler {
6
- _data;
7
- constructor({ ...options }) {
8
- this._data = {
9
- ...options,
10
- events: [],
11
- };
12
- this._buildEvents();
13
- this._registerEvents();
14
- }
15
- _buildEvents() {
16
- const eventFolderPaths = (0, get_paths_1.getFolderPaths)(this._data.eventsPath);
17
- for (const eventFolderPath of eventFolderPaths) {
18
- const eventName = eventFolderPath.replace(/\\/g, '/').split('/').pop();
19
- const eventFilePaths = (0, get_paths_1.getFilePaths)(eventFolderPath, true).filter((path) => path.endsWith('.js') || path.endsWith('.ts'));
20
- const eventObj = {
21
- name: eventName,
22
- functions: [],
23
- };
24
- this._data.events.push(eventObj);
25
- for (const eventFilePath of eventFilePaths) {
26
- const eventFunction = require(eventFilePath);
27
- if (typeof eventFunction !== 'function') {
28
- console.log(`Ignoring: Event ${eventFilePath} does not export a function.`);
29
- continue;
30
- }
31
- eventObj.functions.push(eventFunction);
32
- }
33
- }
34
- }
35
- _registerEvents() {
36
- const client = this._data.client;
37
- for (const eventObj of this._data.events) {
38
- client.on(eventObj.name, async (...params) => {
39
- for (const eventFunction of eventObj.functions) {
40
- const stopEventLoop = await eventFunction(...params, client);
41
- if (stopEventLoop) {
42
- break;
43
- }
44
- }
45
- });
46
- }
47
- }
48
- getEvents() {
49
- return this._data.events;
50
- }
51
- }
52
- exports.EventHandler = EventHandler;
@@ -1,3 +0,0 @@
1
- export * from './command-handler/CommandHandler';
2
- export * from './event-handler/EventHandler';
3
- export * from './validation-handler/ValidationHandler';
@@ -1,19 +0,0 @@
1
- "use strict";
2
- var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
- if (k2 === undefined) k2 = k;
4
- var desc = Object.getOwnPropertyDescriptor(m, k);
5
- if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
- desc = { enumerable: true, get: function() { return m[k]; } };
7
- }
8
- Object.defineProperty(o, k2, desc);
9
- }) : (function(o, m, k, k2) {
10
- if (k2 === undefined) k2 = k;
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);
15
- };
16
- Object.defineProperty(exports, "__esModule", { value: true });
17
- __exportStar(require("./command-handler/CommandHandler"), exports);
18
- __exportStar(require("./event-handler/EventHandler"), exports);
19
- __exportStar(require("./validation-handler/ValidationHandler"), exports);
@@ -1,7 +0,0 @@
1
- import { ValidationHandlerData, ValidationHandlerOptions } from './typings';
2
- export declare class ValidationHandler {
3
- _data: ValidationHandlerData;
4
- constructor({ ...options }: ValidationHandlerOptions);
5
- _buildValidations(): void;
6
- getValidations(): Function[];
7
- }
@@ -1,29 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.ValidationHandler = void 0;
4
- const get_paths_1 = require("../../utils/get-paths");
5
- class ValidationHandler {
6
- _data;
7
- constructor({ ...options }) {
8
- this._data = {
9
- ...options,
10
- validations: [],
11
- };
12
- this._buildValidations();
13
- }
14
- _buildValidations() {
15
- const validationFilePaths = (0, get_paths_1.getFilePaths)(this._data.validationsPath, true).filter((path) => path.endsWith('.js') || path.endsWith('.ts'));
16
- for (const validationFilePath of validationFilePaths) {
17
- const validationFunction = require(validationFilePath);
18
- if (typeof validationFunction !== 'function') {
19
- console.log(`Ignoring: Validation ${validationFilePath} does not export a function.`);
20
- continue;
21
- }
22
- this._data.validations.push(validationFunction);
23
- }
24
- }
25
- getValidations() {
26
- return this._data.validations;
27
- }
28
- }
29
- exports.ValidationHandler = ValidationHandler;
@@ -1,3 +0,0 @@
1
- declare function getFilePaths(directory: string, nesting?: boolean): string[];
2
- declare function getFolderPaths(directory: string, nesting?: boolean): string[];
3
- export { getFilePaths, getFolderPaths };
@@ -1,42 +0,0 @@
1
- "use strict";
2
- var __importDefault = (this && this.__importDefault) || function (mod) {
3
- return (mod && mod.__esModule) ? mod : { "default": mod };
4
- };
5
- Object.defineProperty(exports, "__esModule", { value: true });
6
- exports.getFolderPaths = exports.getFilePaths = void 0;
7
- const path_1 = __importDefault(require("path"));
8
- const fs_1 = __importDefault(require("fs"));
9
- function getFilePaths(directory, nesting) {
10
- let filePaths = [];
11
- if (!directory)
12
- return filePaths;
13
- const files = fs_1.default.readdirSync(directory, { withFileTypes: true });
14
- for (const file of files) {
15
- const filePath = path_1.default.join(directory, file.name);
16
- if (file.isFile()) {
17
- filePaths.push(filePath);
18
- }
19
- if (nesting && file.isDirectory()) {
20
- filePaths = [...filePaths, ...getFilePaths(filePath, true)];
21
- }
22
- }
23
- return filePaths;
24
- }
25
- exports.getFilePaths = getFilePaths;
26
- function getFolderPaths(directory, nesting) {
27
- let folderPaths = [];
28
- if (!directory)
29
- return folderPaths;
30
- const folders = fs_1.default.readdirSync(directory, { withFileTypes: true });
31
- for (const folder of folders) {
32
- const folderPath = path_1.default.join(directory, folder.name);
33
- if (folder.isDirectory()) {
34
- folderPaths.push(folderPath);
35
- if (nesting) {
36
- folderPaths = [...folderPaths, ...getFolderPaths(folderPath, true)];
37
- }
38
- }
39
- }
40
- return folderPaths;
41
- }
42
- exports.getFolderPaths = getFolderPaths;
package/tsconfig.json DELETED
@@ -1,14 +0,0 @@
1
- {
2
- "compilerOptions": {
3
- "outDir": "dist",
4
- "strict": true,
5
- "noImplicitAny": true,
6
- "esModuleInterop": true,
7
- "strictNullChecks": true,
8
- "target": "ES2022",
9
- "moduleResolution": "Node",
10
- "module": "CommonJS",
11
- "declaration": true
12
- },
13
- "include": ["src/**/*"]
14
- }
package/typings.d.ts DELETED
@@ -1,61 +0,0 @@
1
- import {
2
- Client,
3
- APIApplicationCommandOption,
4
- ContextMenuCommandType,
5
- Interaction,
6
- PermissionResolvable,
7
- SlashCommandBuilder,
8
- ContextMenuCommandBuilder,
9
- } from 'discord.js';
10
-
11
- export interface CommandKitOptions {
12
- client: Client;
13
- commandsPath?: string;
14
- eventsPath?: string;
15
- validationsPath?: string;
16
- devGuildIds?: string[];
17
- devUserIds?: string[];
18
- }
19
-
20
- export interface CommandKitData extends CommandKitOptions {
21
- commands: Array<SlashCommandObject | ContextCommandObject>;
22
- }
23
-
24
- export interface SlashCommandObject {
25
- data:
26
- | SlashCommandBuilder
27
- | {
28
- name: string;
29
- name_localizations?: any;
30
- description: string;
31
- dm_permission?: boolean;
32
- options?: APIApplicationCommandOption[];
33
- };
34
- options?: {
35
- guildOnly?: boolean;
36
- devOnly?: boolean;
37
- deleted?: boolean;
38
- userPermissions?: PermissionResolvable[];
39
- botPermissions?: PermissionResolvable[];
40
- };
41
- run: ({}: { interaction: Interaction; client: Client }) => void;
42
- }
43
-
44
- export interface ContextCommandObject {
45
- data:
46
- | ContextMenuCommandBuilder
47
- | {
48
- name: string;
49
- name_localizations?: any;
50
- type: ContextMenuCommandType;
51
- dm_permission?: boolean;
52
- };
53
- options?: {
54
- guildOnly?: boolean;
55
- devOnly?: boolean;
56
- deleted?: boolean;
57
- userPermissions?: PermissionResolvable[];
58
- botPermissions?: PermissionResolvable[];
59
- };
60
- run: ({}: { interaction: Interaction; client: Client }) => void;
61
- }