commandkit 0.0.1 → 0.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +9 -0
- package/README.md +76 -1
- package/dist/CommandKit.d.ts +36 -0
- package/dist/CommandKit.js +55 -0
- package/dist/handlers/command-handler/CommandHandler.d.ts +11 -0
- package/dist/handlers/command-handler/CommandHandler.js +198 -0
- package/dist/handlers/event-handler/EventHandler.d.ts +11 -0
- package/dist/handlers/event-handler/EventHandler.js +53 -0
- package/dist/handlers/index.d.ts +3 -0
- package/dist/handlers/index.js +19 -0
- package/dist/handlers/validation-handler/ValidationHandler.d.ts +7 -0
- package/dist/handlers/validation-handler/ValidationHandler.js +29 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.js +17 -0
- package/dist/utils/get-paths.d.ts +3 -0
- package/dist/utils/get-paths.js +42 -0
- package/package.json +8 -6
- package/tsconfig.json +14 -0
- package/typings.d.ts +61 -0
- package/index.js +0 -0
package/CHANGELOG.md
ADDED
package/README.md
CHANGED
|
@@ -1 +1,76 @@
|
|
|
1
|
-
#
|
|
1
|
+
# CommandKit
|
|
2
|
+
|
|
3
|
+
CommandKit is a library that makes it easy to handle commands (+validations), and events in your Discord.js projects.
|
|
4
|
+
|
|
5
|
+
_Tested with Discord.js version `v14.11.0`_
|
|
6
|
+
|
|
7
|
+
# Features
|
|
8
|
+
|
|
9
|
+
- Very beginner friendly 🚀
|
|
10
|
+
- Support for slash and context menu commands ✅
|
|
11
|
+
- Automatic command registrations and deletion 🤖
|
|
12
|
+
- Supports multiple development servers 🤝
|
|
13
|
+
- Supports multiple users as bot developers 👥
|
|
14
|
+
- Object oriented 💻
|
|
15
|
+
|
|
16
|
+
# Documentation
|
|
17
|
+
|
|
18
|
+
_Coming soon_
|
|
19
|
+
~~You can find the full documentation [here](https://commandkit.underctrl.io)~~
|
|
20
|
+
|
|
21
|
+
# Installation
|
|
22
|
+
|
|
23
|
+
[](https://nodei.co/npm/commandkit/)
|
|
24
|
+
|
|
25
|
+
To install CommandKit, simply run the following command:
|
|
26
|
+
|
|
27
|
+
For npm:
|
|
28
|
+
|
|
29
|
+
```bash
|
|
30
|
+
npm install commandkit
|
|
31
|
+
```
|
|
32
|
+
|
|
33
|
+
For yarn:
|
|
34
|
+
|
|
35
|
+
```bash
|
|
36
|
+
yarn add commandkit
|
|
37
|
+
```
|
|
38
|
+
|
|
39
|
+
# Usage
|
|
40
|
+
|
|
41
|
+
This is a simple overview of how to set up this library with all the options.
|
|
42
|
+
|
|
43
|
+
~~**It's highly recommended you check out the documentation to fully understand how to work with this library.**~~
|
|
44
|
+
|
|
45
|
+
```js
|
|
46
|
+
// index.js
|
|
47
|
+
const { Client, IntentsBitField } = require('discord.js');
|
|
48
|
+
const { CommandKit } = require('commandkit');
|
|
49
|
+
const path = require('path');
|
|
50
|
+
|
|
51
|
+
const client = new Client({
|
|
52
|
+
intents: [IntentsBitField.Flags.Guilds],
|
|
53
|
+
});
|
|
54
|
+
|
|
55
|
+
new CommandKit({
|
|
56
|
+
// Your discord.js client object
|
|
57
|
+
client,
|
|
58
|
+
|
|
59
|
+
// Path to the commands folder
|
|
60
|
+
commandsPath: path.join(__dirname, 'commands'),
|
|
61
|
+
|
|
62
|
+
// Path to the events folder
|
|
63
|
+
eventsPath: path.join(__dirname, 'events'),
|
|
64
|
+
|
|
65
|
+
// Path to the validations folder (only valid if "commandsPath" was provided)
|
|
66
|
+
validationsPath: path.join(__dirname, 'validations'),
|
|
67
|
+
|
|
68
|
+
// Array of development server IDs (used to register and run devOnly commands)
|
|
69
|
+
devGuildIds: ['DEV_SERVER_ID_1', 'DEV_SERVER_ID_2'],
|
|
70
|
+
|
|
71
|
+
// Array of developer user IDs (used for devOnly commands)
|
|
72
|
+
devUserIds: ['DEV_USER_ID_1', 'DEV_USER_ID_2'],
|
|
73
|
+
});
|
|
74
|
+
|
|
75
|
+
client.login('YOUR_TOKEN_HERE');
|
|
76
|
+
```
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import { CommandKitOptions } from '../typings';
|
|
2
|
+
export declare class CommandKit {
|
|
3
|
+
private _data;
|
|
4
|
+
constructor({ ...options }: CommandKitOptions);
|
|
5
|
+
private _init;
|
|
6
|
+
get commands(): ({
|
|
7
|
+
data: import("@discordjs/builders").SlashCommandBuilder | {
|
|
8
|
+
name: string;
|
|
9
|
+
name_localizations?: any;
|
|
10
|
+
description: string;
|
|
11
|
+
dm_permission?: boolean | undefined;
|
|
12
|
+
options: import("discord.js").APIApplicationCommandOption[];
|
|
13
|
+
};
|
|
14
|
+
options?: {
|
|
15
|
+
guildOnly?: boolean | undefined;
|
|
16
|
+
devOnly?: boolean | undefined;
|
|
17
|
+
deleted?: boolean | undefined;
|
|
18
|
+
userPermissions?: import("discord.js").PermissionResolvable[] | undefined;
|
|
19
|
+
botPermissions?: import("discord.js").PermissionResolvable[] | undefined;
|
|
20
|
+
} | undefined;
|
|
21
|
+
} | {
|
|
22
|
+
data: import("@discordjs/builders").ContextMenuCommandBuilder | {
|
|
23
|
+
name: string;
|
|
24
|
+
name_localizations?: any;
|
|
25
|
+
type: import("@discordjs/builders").ContextMenuCommandType;
|
|
26
|
+
dm_permission?: boolean | undefined;
|
|
27
|
+
};
|
|
28
|
+
options?: {
|
|
29
|
+
guildOnly?: boolean | undefined;
|
|
30
|
+
devOnly?: boolean | undefined;
|
|
31
|
+
deleted?: boolean | undefined;
|
|
32
|
+
userPermissions?: import("discord.js").PermissionResolvable[] | undefined;
|
|
33
|
+
botPermissions?: import("discord.js").PermissionResolvable[] | undefined;
|
|
34
|
+
} | undefined;
|
|
35
|
+
})[];
|
|
36
|
+
}
|
|
@@ -0,0 +1,55 @@
|
|
|
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;
|
|
@@ -0,0 +1,11 @@
|
|
|
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
|
+
getCommands(): (SlashCommandObject | ContextCommandObject)[];
|
|
11
|
+
}
|
|
@@ -0,0 +1,198 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.CommandHandler = void 0;
|
|
4
|
+
const discord_js_1 = require("discord.js");
|
|
5
|
+
const get_paths_1 = require("../../utils/get-paths");
|
|
6
|
+
class CommandHandler {
|
|
7
|
+
_data;
|
|
8
|
+
constructor({ ...options }) {
|
|
9
|
+
this._data = {
|
|
10
|
+
...options,
|
|
11
|
+
commands: [],
|
|
12
|
+
};
|
|
13
|
+
this._init();
|
|
14
|
+
}
|
|
15
|
+
_init() {
|
|
16
|
+
this._buildCommands();
|
|
17
|
+
this._registerCommands();
|
|
18
|
+
this._handleCommands();
|
|
19
|
+
}
|
|
20
|
+
_buildCommands() {
|
|
21
|
+
const commandFilePaths = (0, get_paths_1.getFilePaths)(this._data.commandsPath, true).filter((path) => path.endsWith('.js') || path.endsWith('.ts'));
|
|
22
|
+
for (const commandFilePath of commandFilePaths) {
|
|
23
|
+
const commandObj = require(commandFilePath);
|
|
24
|
+
if (!commandObj.data) {
|
|
25
|
+
console.log(`⏩ Ignoring: Command ${commandFilePath} does not export "data".`);
|
|
26
|
+
continue;
|
|
27
|
+
}
|
|
28
|
+
if (!commandObj.run) {
|
|
29
|
+
console.log(`⏩ Ignoring: Command ${commandFilePath} does not export "run".`);
|
|
30
|
+
continue;
|
|
31
|
+
}
|
|
32
|
+
this._data.commands.push(commandObj);
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
_registerCommands() {
|
|
36
|
+
const client = this._data.client;
|
|
37
|
+
const commands = this._data.commands;
|
|
38
|
+
client.once('ready', async () => {
|
|
39
|
+
const devGuilds = [];
|
|
40
|
+
for (const devGuildId of this._data.devGuildIds) {
|
|
41
|
+
const guild = client.guilds.cache.get(devGuildId);
|
|
42
|
+
if (!guild) {
|
|
43
|
+
console.log(`⏩ Ignoring: Guild ${devGuildId} does not exist or client isn't in this guild.`);
|
|
44
|
+
continue;
|
|
45
|
+
}
|
|
46
|
+
devGuilds.push(guild);
|
|
47
|
+
}
|
|
48
|
+
const appCommands = client.application?.commands;
|
|
49
|
+
await appCommands?.fetch();
|
|
50
|
+
const devGuildCommands = [];
|
|
51
|
+
for (const guild of devGuilds) {
|
|
52
|
+
const guildCommands = guild.commands;
|
|
53
|
+
await guildCommands?.fetch();
|
|
54
|
+
devGuildCommands.push(guildCommands);
|
|
55
|
+
}
|
|
56
|
+
for (const command of commands) {
|
|
57
|
+
const commandData = command.data;
|
|
58
|
+
if (commandData instanceof discord_js_1.SlashCommandBuilder ||
|
|
59
|
+
commandData instanceof discord_js_1.ContextMenuCommandBuilder) {
|
|
60
|
+
try {
|
|
61
|
+
commandData.toJSON();
|
|
62
|
+
}
|
|
63
|
+
catch (error) { }
|
|
64
|
+
}
|
|
65
|
+
// <!-- Delete command if options.deleted -->
|
|
66
|
+
if (command.options?.deleted) {
|
|
67
|
+
const targetCommand = appCommands?.cache.find((cmd) => cmd.name === command.data.name);
|
|
68
|
+
targetCommand?.delete().then(() => {
|
|
69
|
+
console.log(`🗑️ Deleted command "${command.data.name}" globally.`);
|
|
70
|
+
});
|
|
71
|
+
for (const guildCommands of devGuildCommands) {
|
|
72
|
+
const targetCommand = guildCommands.cache.find((cmd) => cmd.name === command.data.name);
|
|
73
|
+
targetCommand?.delete().then(() => {
|
|
74
|
+
console.log(`🗑️ Deleted command "${command.data.name}" in ${guildCommands.guild.name}.`);
|
|
75
|
+
});
|
|
76
|
+
}
|
|
77
|
+
continue;
|
|
78
|
+
}
|
|
79
|
+
// <!-- Registration -->
|
|
80
|
+
// guild-based command registration
|
|
81
|
+
if (command.options?.devOnly) {
|
|
82
|
+
if (!devGuilds.length) {
|
|
83
|
+
console.log(`⏩ Ignoring: Cannot register command "${command.data.name}" as no valid "devGuildIds" were provided.`);
|
|
84
|
+
continue;
|
|
85
|
+
}
|
|
86
|
+
for (const guild of devGuilds) {
|
|
87
|
+
const cmdExists = guild.commands.cache.some((cmd) => cmd.name === command.data.name);
|
|
88
|
+
if (cmdExists)
|
|
89
|
+
continue;
|
|
90
|
+
guild?.commands
|
|
91
|
+
.create(command.data)
|
|
92
|
+
.then(() => {
|
|
93
|
+
console.log(`✅ Registered command "${command.data.name}" in ${guild.name}.`);
|
|
94
|
+
})
|
|
95
|
+
.catch((error) => {
|
|
96
|
+
console.log(`❌ Failed to register command "${command.data.name}" in ${guild.name}.`);
|
|
97
|
+
console.error(error);
|
|
98
|
+
});
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
// global command registration
|
|
102
|
+
else {
|
|
103
|
+
const cmdExists = appCommands?.cache.some((cmd) => cmd.name === command.data.name);
|
|
104
|
+
if (cmdExists)
|
|
105
|
+
continue;
|
|
106
|
+
appCommands
|
|
107
|
+
?.create(command.data)
|
|
108
|
+
.then(() => {
|
|
109
|
+
console.log(`✅ Registered command "${command.data.name}" globally.`);
|
|
110
|
+
})
|
|
111
|
+
.catch((error) => {
|
|
112
|
+
console.log(`❌ Failed to register command "${command.data.name}" globally.`);
|
|
113
|
+
console.error(error);
|
|
114
|
+
});
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
});
|
|
118
|
+
}
|
|
119
|
+
_handleCommands() {
|
|
120
|
+
const client = this._data.client;
|
|
121
|
+
client.on('interactionCreate', async (interaction) => {
|
|
122
|
+
if (!interaction.isChatInputCommand() && !interaction.isContextMenuCommand())
|
|
123
|
+
return;
|
|
124
|
+
const targetCommand = this._data.commands.find((cmd) => cmd.data.name === interaction.commandName);
|
|
125
|
+
if (!targetCommand)
|
|
126
|
+
return;
|
|
127
|
+
// Options validation
|
|
128
|
+
// options.guildOnly
|
|
129
|
+
if (targetCommand.options?.guildOnly && !interaction.inGuild()) {
|
|
130
|
+
interaction.reply({
|
|
131
|
+
content: '❌ This command can only be used inside a server.',
|
|
132
|
+
ephemeral: true,
|
|
133
|
+
});
|
|
134
|
+
return;
|
|
135
|
+
}
|
|
136
|
+
// options.devOnly
|
|
137
|
+
if (targetCommand.options?.devOnly) {
|
|
138
|
+
const isDevUser = this._data.devUserIds.includes(interaction.user.id);
|
|
139
|
+
if (!isDevUser) {
|
|
140
|
+
interaction.reply({
|
|
141
|
+
content: '❌ This command can only be used by developers.',
|
|
142
|
+
ephemeral: true,
|
|
143
|
+
});
|
|
144
|
+
return;
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
// options.userPermissions
|
|
148
|
+
const memberPermissions = interaction.memberPermissions;
|
|
149
|
+
if (targetCommand.options?.userPermissions && memberPermissions) {
|
|
150
|
+
for (const permission of targetCommand.options.userPermissions) {
|
|
151
|
+
const hasPermission = memberPermissions.has(permission);
|
|
152
|
+
if (!hasPermission) {
|
|
153
|
+
interaction.reply({
|
|
154
|
+
content: `❌ You do not have enough permission to run this command. Required permission: \`${permission}\``,
|
|
155
|
+
ephemeral: true,
|
|
156
|
+
});
|
|
157
|
+
return;
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
// options.botPermissions
|
|
162
|
+
const botMember = interaction.guild?.members.me;
|
|
163
|
+
if (targetCommand.options?.botPermissions && botMember) {
|
|
164
|
+
for (const permission of targetCommand.options.botPermissions) {
|
|
165
|
+
const hasPermission = botMember.permissions.has(permission);
|
|
166
|
+
if (!hasPermission) {
|
|
167
|
+
interaction.reply({
|
|
168
|
+
content: `❌ I do not have enough permission to execute this command. Required permission: \`${permission}\``,
|
|
169
|
+
ephemeral: true,
|
|
170
|
+
});
|
|
171
|
+
return;
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
// Run user validation functions
|
|
176
|
+
const validationFunctions = this._data.validations;
|
|
177
|
+
const commandObj = {
|
|
178
|
+
data: targetCommand.data,
|
|
179
|
+
options: targetCommand.options,
|
|
180
|
+
};
|
|
181
|
+
let canRun = true;
|
|
182
|
+
for (const validationFunction of validationFunctions) {
|
|
183
|
+
const stopValidationLoop = await validationFunction({ interaction, client, commandObj });
|
|
184
|
+
if (stopValidationLoop) {
|
|
185
|
+
canRun = false;
|
|
186
|
+
break;
|
|
187
|
+
}
|
|
188
|
+
}
|
|
189
|
+
if (canRun) {
|
|
190
|
+
targetCommand.run({ interaction, client });
|
|
191
|
+
}
|
|
192
|
+
});
|
|
193
|
+
}
|
|
194
|
+
getCommands() {
|
|
195
|
+
return this._data.commands;
|
|
196
|
+
}
|
|
197
|
+
}
|
|
198
|
+
exports.CommandHandler = CommandHandler;
|
|
@@ -0,0 +1,11 @@
|
|
|
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
|
+
}
|
|
@@ -0,0 +1,53 @@
|
|
|
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 eventFolderPathChunks = eventFolderPath.replace('\\', '/').split('/');
|
|
19
|
+
const eventName = eventFolderPathChunks[eventFolderPathChunks.length - 1];
|
|
20
|
+
const eventFilePaths = (0, get_paths_1.getFilePaths)(eventFolderPath).filter((path) => path.endsWith('.js') || path.endsWith('.ts'));
|
|
21
|
+
const eventObj = {
|
|
22
|
+
name: eventName,
|
|
23
|
+
functions: [],
|
|
24
|
+
};
|
|
25
|
+
this._data.events.push(eventObj);
|
|
26
|
+
for (const eventFilePath of eventFilePaths) {
|
|
27
|
+
const eventFunction = require(eventFilePath);
|
|
28
|
+
if (typeof eventFunction !== 'function') {
|
|
29
|
+
console.log(`Ignoring: Event ${eventFilePath} does not export a function.`);
|
|
30
|
+
continue;
|
|
31
|
+
}
|
|
32
|
+
eventObj.functions.push(eventFunction);
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
_registerEvents() {
|
|
37
|
+
const client = this._data.client;
|
|
38
|
+
for (const eventObj of this._data.events) {
|
|
39
|
+
client.on(eventObj.name, async (...params) => {
|
|
40
|
+
for (const eventFunction of eventObj.functions) {
|
|
41
|
+
const stopEventLoop = await eventFunction(...params, client);
|
|
42
|
+
if (stopEventLoop) {
|
|
43
|
+
break;
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
});
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
getEvents() {
|
|
50
|
+
return this._data.events;
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
exports.EventHandler = EventHandler;
|
|
@@ -0,0 +1,19 @@
|
|
|
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);
|
|
@@ -0,0 +1,7 @@
|
|
|
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
|
+
}
|
|
@@ -0,0 +1,29 @@
|
|
|
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;
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './CommandKit';
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
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("./CommandKit"), exports);
|
|
@@ -0,0 +1,42 @@
|
|
|
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/package.json
CHANGED
|
@@ -1,9 +1,11 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "commandkit",
|
|
3
|
-
"version": "0.0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
"
|
|
7
|
-
|
|
8
|
-
|
|
3
|
+
"version": "0.0.2",
|
|
4
|
+
"main": "dist/index.js",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"devDependencies": {
|
|
7
|
+
"discord.js": "^14.11.0",
|
|
8
|
+
"tsc": "^2.0.4"
|
|
9
|
+
},
|
|
10
|
+
"dependencies": {}
|
|
9
11
|
}
|
package/tsconfig.json
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
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
ADDED
|
@@ -0,0 +1,61 @@
|
|
|
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
|
+
}
|
package/index.js
DELETED
|
File without changes
|