djs-next 0.0.1
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.
Potentially problematic release.
This version of djs-next might be problematic. Click here for more details.
- package/README.md +250 -0
- package/assets/djs-next.png +0 -0
- package/bin/create-djs-next.js +78 -0
- package/dist/index.d.mts +161 -0
- package/dist/index.d.ts +161 -0
- package/dist/index.js +1283 -0
- package/dist/index.js.map +1 -0
- package/dist/index.mjs +1259 -0
- package/dist/index.mjs.map +1 -0
- package/package.json +45 -0
- package/src/client.ts +492 -0
- package/src/handlers/commandHandler.ts +139 -0
- package/src/handlers/componentHandler.ts +31 -0
- package/src/handlers/eventHandler.ts +37 -0
- package/src/handlers/taskHandler.ts +38 -0
- package/src/handlers/utils.ts +25 -0
- package/src/index.ts +10 -0
- package/src/plugins/dnxt.ts +418 -0
- package/src/templates/cjs.ts +54 -0
- package/src/templates/esm.ts +50 -0
- package/src/templates/ts.ts +53 -0
- package/src/test/client.test.ts +8 -0
- package/src/types.ts +94 -0
- package/src/utils/PaginationBuilder.ts +94 -0
- package/src/utils/configLoader.ts +27 -0
- package/src/utils/i18n.ts +57 -0
- package/src/utils/paginate.ts +90 -0
- package/src/utils/prompts.ts +76 -0
- package/test_payload.js +3 -0
- package/test_reply.js +4 -0
- package/tsconfig.json +16 -0
- package/tsup.config.ts +10 -0
package/src/client.ts
ADDED
|
@@ -0,0 +1,492 @@
|
|
|
1
|
+
import { Client, Collection, Interaction, Message } from 'discord.js';
|
|
2
|
+
import { DJSNextClientOptions, FileCommand, FileComponent, DJSNextConfig } from './types.js';
|
|
3
|
+
import { loadAndDeployCommands } from './handlers/commandHandler.js';
|
|
4
|
+
import { loadEvents } from './handlers/eventHandler.js';
|
|
5
|
+
import { loadComponents } from './handlers/componentHandler.js';
|
|
6
|
+
import { loadTasks } from './handlers/taskHandler.js';
|
|
7
|
+
import path from 'path';
|
|
8
|
+
import fs from 'fs';
|
|
9
|
+
import { pathToFileURL } from 'url';
|
|
10
|
+
|
|
11
|
+
import { loadConfig } from './utils/configLoader.js';
|
|
12
|
+
import { loadLocales, translate } from './utils/i18n.js';
|
|
13
|
+
import { handleDNXT } from './plugins/dnxt.js';
|
|
14
|
+
|
|
15
|
+
export class DJSNextClient<DB = any> extends Client {
|
|
16
|
+
public commands: Collection<string, FileCommand>;
|
|
17
|
+
public components: Collection<string, FileComponent>;
|
|
18
|
+
public cooldowns: Collection<string, Collection<string, number>>;
|
|
19
|
+
public config: DJSNextConfig = {};
|
|
20
|
+
public t = translate;
|
|
21
|
+
public db!: DB;
|
|
22
|
+
|
|
23
|
+
private _commandsDir?: string;
|
|
24
|
+
private _eventsDir?: string;
|
|
25
|
+
private _componentsDir?: string;
|
|
26
|
+
private _tasksDir?: string;
|
|
27
|
+
private _localesDir?: string;
|
|
28
|
+
private _clientId?: string;
|
|
29
|
+
private _guildId?: string;
|
|
30
|
+
private _developers: string[];
|
|
31
|
+
private _middleware?: (interaction: Interaction | Message, client: Client) => Promise<boolean> | boolean;
|
|
32
|
+
private _prefixes: string[];
|
|
33
|
+
private _enableSlashCommands: boolean;
|
|
34
|
+
private _enableTextCommands: boolean;
|
|
35
|
+
private _enableMentionPrefix: boolean | string[];
|
|
36
|
+
private _enableNoPrefix: boolean | string[];
|
|
37
|
+
|
|
38
|
+
constructor(options: DJSNextClientOptions) {
|
|
39
|
+
super(options);
|
|
40
|
+
this.commands = new Collection();
|
|
41
|
+
this.components = new Collection();
|
|
42
|
+
this.cooldowns = new Collection();
|
|
43
|
+
|
|
44
|
+
this._commandsDir = options.commandsDir ? path.resolve(process.cwd(), options.commandsDir) : undefined;
|
|
45
|
+
this._eventsDir = options.eventsDir ? path.resolve(process.cwd(), options.eventsDir) : undefined;
|
|
46
|
+
this._componentsDir = options.componentsDir ? path.resolve(process.cwd(), options.componentsDir) : undefined;
|
|
47
|
+
this._tasksDir = options.tasksDir ? path.resolve(process.cwd(), options.tasksDir) : undefined;
|
|
48
|
+
this._clientId = options.clientId;
|
|
49
|
+
this._guildId = options.guildId;
|
|
50
|
+
this._developers = options.developers || [];
|
|
51
|
+
this._middleware = options.middleware as any;
|
|
52
|
+
|
|
53
|
+
const prefs = options.prefixes || [];
|
|
54
|
+
this._prefixes = Array.isArray(prefs) ? prefs : [prefs];
|
|
55
|
+
this._prefixes = this._prefixes.filter(p => p !== ''); // Remove empty strings, handle explicitly via enableNoPrefix
|
|
56
|
+
|
|
57
|
+
this._enableSlashCommands = options.enableSlashCommands ?? true;
|
|
58
|
+
this._enableTextCommands = options.enableTextCommands ?? true;
|
|
59
|
+
this._enableMentionPrefix = options.enableMentionPrefix ?? true;
|
|
60
|
+
this._enableNoPrefix = options.enableNoPrefix ?? false;
|
|
61
|
+
if (options.db) this.db = options.db as any;
|
|
62
|
+
|
|
63
|
+
this.attachCoreListeners();
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
private attachCoreListeners() {
|
|
67
|
+
this.on('interactionCreate', async (interaction: Interaction) => {
|
|
68
|
+
// 1. Global Middleware execution
|
|
69
|
+
if (this._middleware) {
|
|
70
|
+
try {
|
|
71
|
+
const shouldContinue = await this._middleware(interaction, this);
|
|
72
|
+
if (!shouldContinue) return; // Middleware halted execution
|
|
73
|
+
} catch (error) {
|
|
74
|
+
console.error(`[djs-next] Middleware error:`, error);
|
|
75
|
+
return;
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
// 2. Chat Input Commands Execution
|
|
80
|
+
if (interaction.isChatInputCommand()) {
|
|
81
|
+
if (!this._enableSlashCommands) return;
|
|
82
|
+
|
|
83
|
+
let commandKey = interaction.commandName;
|
|
84
|
+
const group = interaction.options.getSubcommandGroup(false);
|
|
85
|
+
const sub = interaction.options.getSubcommand(false);
|
|
86
|
+
|
|
87
|
+
if (group) commandKey += ` ${group}`;
|
|
88
|
+
if (sub) commandKey += ` ${sub}`;
|
|
89
|
+
|
|
90
|
+
const command = this.commands.get(commandKey);
|
|
91
|
+
if (!command || !command.execute) return;
|
|
92
|
+
|
|
93
|
+
if (!(await this.handlePreconditions(command, interaction, commandKey))) return;
|
|
94
|
+
|
|
95
|
+
try {
|
|
96
|
+
await command.execute(interaction, this);
|
|
97
|
+
} catch (error) {
|
|
98
|
+
await this.handleCommandError(error, commandKey, interaction);
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
// 3. Autocomplete Routing
|
|
103
|
+
if (interaction.isAutocomplete()) {
|
|
104
|
+
let commandKey = interaction.commandName;
|
|
105
|
+
const group = interaction.options.getSubcommandGroup(false);
|
|
106
|
+
const sub = interaction.options.getSubcommand(false);
|
|
107
|
+
|
|
108
|
+
if (group) commandKey += ` ${group}`;
|
|
109
|
+
if (sub) commandKey += ` ${sub}`;
|
|
110
|
+
|
|
111
|
+
const command = this.commands.get(commandKey);
|
|
112
|
+
if (command && command.autocomplete) {
|
|
113
|
+
try {
|
|
114
|
+
await command.autocomplete(interaction, this);
|
|
115
|
+
} catch (error) {
|
|
116
|
+
console.error(`[djs-next] Error executing autocomplete for: "${commandKey}"`, error);
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
// 4. Component Routing (Buttons, Modals, Menus)
|
|
122
|
+
if (interaction.isMessageComponent() || interaction.isModalSubmit()) {
|
|
123
|
+
// Skip pagination built-in buttons
|
|
124
|
+
if (interaction.customId === 'djs_prev' || interaction.customId === 'djs_next') return;
|
|
125
|
+
|
|
126
|
+
let component = this.components.get(interaction.customId);
|
|
127
|
+
let params: Record<string, string> = {};
|
|
128
|
+
|
|
129
|
+
if (!component) {
|
|
130
|
+
for (const [key, comp] of this.components) {
|
|
131
|
+
if (!key.includes('[')) continue;
|
|
132
|
+
// Escape regex chars except brackets
|
|
133
|
+
const escapedKey = key.replace(/[.*+?^${}()|\\-]/g, '\\$&');
|
|
134
|
+
// Transform \[id\] into (?<id>.+)
|
|
135
|
+
const regexStr = '^' + escapedKey.replace(/\\\[([^\]]+)\\\]/g, '(?<$1>.+)') + '$';
|
|
136
|
+
const match = interaction.customId.match(new RegExp(regexStr));
|
|
137
|
+
if (match) {
|
|
138
|
+
component = comp;
|
|
139
|
+
if (match.groups) params = match.groups;
|
|
140
|
+
break;
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
if (component) {
|
|
145
|
+
if (!(await this.handlePreconditions(component, interaction, interaction.customId))) return;
|
|
146
|
+
|
|
147
|
+
try {
|
|
148
|
+
await component.execute(interaction, this, params);
|
|
149
|
+
} catch (error) {
|
|
150
|
+
await this.handleCommandError(error, interaction.customId, interaction);
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
});
|
|
155
|
+
|
|
156
|
+
this.on('messageCreate', async (message: Message) => {
|
|
157
|
+
if (message.author.bot) return;
|
|
158
|
+
if (!this._enableTextCommands) return;
|
|
159
|
+
|
|
160
|
+
// Global Middleware execution for Messages
|
|
161
|
+
if (this._middleware) {
|
|
162
|
+
try {
|
|
163
|
+
const shouldContinue = await this._middleware(message, this);
|
|
164
|
+
if (!shouldContinue) return;
|
|
165
|
+
} catch (error) {
|
|
166
|
+
console.error(`[djs-next] Middleware error (Message):`, error);
|
|
167
|
+
return;
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
let content = message.content.trim();
|
|
172
|
+
let matchedPrefix = '';
|
|
173
|
+
let isCommand = false;
|
|
174
|
+
|
|
175
|
+
// 1. Check Mention Prefix
|
|
176
|
+
const mentionRegex = new RegExp(`^<@!?${this.user?.id}>\\s*`);
|
|
177
|
+
const canUseMention = this._enableMentionPrefix === true || (Array.isArray(this._enableMentionPrefix) && this._enableMentionPrefix.includes(message.author.id));
|
|
178
|
+
|
|
179
|
+
if (canUseMention && mentionRegex.test(content)) {
|
|
180
|
+
matchedPrefix = content.match(mentionRegex)![0];
|
|
181
|
+
isCommand = true;
|
|
182
|
+
} else {
|
|
183
|
+
// 2. Check Standard Prefixes
|
|
184
|
+
const sortedPrefs = [...this._prefixes].sort((a, b) => b.length - a.length);
|
|
185
|
+
|
|
186
|
+
for (const p of sortedPrefs) {
|
|
187
|
+
if (content.startsWith(p)) {
|
|
188
|
+
matchedPrefix = p;
|
|
189
|
+
isCommand = true;
|
|
190
|
+
break;
|
|
191
|
+
}
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
// 3. Check No Prefix
|
|
195
|
+
const canUseNoPrefix = this._enableNoPrefix === true || (Array.isArray(this._enableNoPrefix) && this._enableNoPrefix.includes(message.author.id));
|
|
196
|
+
if (!isCommand && canUseNoPrefix) {
|
|
197
|
+
isCommand = true;
|
|
198
|
+
matchedPrefix = '';
|
|
199
|
+
}
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
if (!isCommand) return;
|
|
203
|
+
|
|
204
|
+
const args = content.slice(matchedPrefix.length).trim().split(/ +/g);
|
|
205
|
+
const commandName = args.shift()?.toLowerCase();
|
|
206
|
+
if (!commandName) return;
|
|
207
|
+
|
|
208
|
+
// Find the command
|
|
209
|
+
let command = this.commands.get(commandName);
|
|
210
|
+
if (!command) {
|
|
211
|
+
// Check aliases
|
|
212
|
+
command = this.commands.find(c => c.aliases?.includes(commandName) || false);
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
if (!command || !command.executeText) return;
|
|
216
|
+
|
|
217
|
+
if (!(await this.handlePreconditions(command, message, commandName))) return;
|
|
218
|
+
|
|
219
|
+
// Execute Text Command
|
|
220
|
+
try {
|
|
221
|
+
await command.executeText(message, args, this);
|
|
222
|
+
} catch (error) {
|
|
223
|
+
await this.handleCommandError(error, commandName, message);
|
|
224
|
+
}
|
|
225
|
+
});
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
public async start(token: string): Promise<void> {
|
|
229
|
+
if (!token) throw new Error("[djs-next] A token must be provided to start the bot.");
|
|
230
|
+
|
|
231
|
+
this.config = await loadConfig();
|
|
232
|
+
|
|
233
|
+
// Fallback options to config, then to default 'src' folders
|
|
234
|
+
this._guildId = this._guildId || this.config.devGuildId;
|
|
235
|
+
|
|
236
|
+
if (!this._commandsDir) this._commandsDir = path.resolve(process.cwd(), this.config.directories?.commands || 'src/commands');
|
|
237
|
+
if (!this._eventsDir) this._eventsDir = path.resolve(process.cwd(), this.config.directories?.events || 'src/events');
|
|
238
|
+
if (!this._componentsDir) this._componentsDir = path.resolve(process.cwd(), this.config.directories?.components || 'src/components');
|
|
239
|
+
if (!this._tasksDir) this._tasksDir = path.resolve(process.cwd(), this.config.directories?.tasks || 'src/tasks');
|
|
240
|
+
if (!this._localesDir) this._localesDir = path.resolve(process.cwd(), this.config.directories?.locales || 'src/locales');
|
|
241
|
+
|
|
242
|
+
// Only load if the directories actually exist
|
|
243
|
+
if (fs.existsSync(this._localesDir)) loadLocales(this._localesDir, this.config.defaultLocale);
|
|
244
|
+
|
|
245
|
+
// Load middleware from root
|
|
246
|
+
if (!this._middleware) {
|
|
247
|
+
const exts = ['.js', '.mjs', '.cjs', '.ts', '.mts', '.cts'];
|
|
248
|
+
const cwd = process.cwd();
|
|
249
|
+
for (const ext of exts) {
|
|
250
|
+
const mwPath = path.join(cwd, `middleware${ext}`);
|
|
251
|
+
if (fs.existsSync(mwPath)) {
|
|
252
|
+
try {
|
|
253
|
+
const mwModule = await import(pathToFileURL(mwPath).href);
|
|
254
|
+
this._middleware = mwModule.default?.middleware || mwModule.middleware || mwModule.default || mwModule;
|
|
255
|
+
if (this._middleware) {
|
|
256
|
+
console.log(`[djs-next] Loaded global middleware.`);
|
|
257
|
+
break;
|
|
258
|
+
}
|
|
259
|
+
} catch (err) {
|
|
260
|
+
console.error(`[djs-next] Error loading middleware file ${mwPath}:`, err);
|
|
261
|
+
}
|
|
262
|
+
}
|
|
263
|
+
}
|
|
264
|
+
}
|
|
265
|
+
|
|
266
|
+
if (fs.existsSync(this._eventsDir)) await loadEvents(this, this._eventsDir);
|
|
267
|
+
if (fs.existsSync(this._componentsDir)) this.components = await loadComponents(this._componentsDir);
|
|
268
|
+
if (fs.existsSync(this._tasksDir)) await loadTasks(this, this._tasksDir);
|
|
269
|
+
|
|
270
|
+
await this.login(token);
|
|
271
|
+
console.log(`[djs-next] Bot is ready and logged in as ${this.user?.tag}!`);
|
|
272
|
+
|
|
273
|
+
// Load and deploy commands AFTER login so we can automatically use this.user.id
|
|
274
|
+
if (fs.existsSync(this._commandsDir)) {
|
|
275
|
+
this._clientId = this._clientId || this.user!.id;
|
|
276
|
+
this.commands = await loadAndDeployCommands(this._commandsDir, token, this._clientId, this._guildId);
|
|
277
|
+
}
|
|
278
|
+
}
|
|
279
|
+
|
|
280
|
+
public enableDevTools(prefix: 'dnxt' | 'nxt' = 'dnxt'): void {
|
|
281
|
+
if (prefix !== 'dnxt' && prefix !== 'nxt') {
|
|
282
|
+
throw new Error(`[djs-next] Developer Tools prefix must be either 'dnxt' or 'nxt'. Received: ${prefix}`);
|
|
283
|
+
}
|
|
284
|
+
|
|
285
|
+
this.on('messageCreate', async (message) => {
|
|
286
|
+
await handleDNXT(message, this, prefix);
|
|
287
|
+
});
|
|
288
|
+
|
|
289
|
+
console.log(`[djs-next] 🛠️ Developer Tools enabled. Use "${prefix}" in chat. Ensure MessageContent intent is enabled!`);
|
|
290
|
+
}
|
|
291
|
+
|
|
292
|
+
public async enableHMR(): Promise<void> {
|
|
293
|
+
try {
|
|
294
|
+
const chokidar = await import('chokidar');
|
|
295
|
+
console.log(`[djs-next] 🔄 HMR Enabled. Watching for file changes...`);
|
|
296
|
+
|
|
297
|
+
const watcher = chokidar.watch([
|
|
298
|
+
this._commandsDir,
|
|
299
|
+
this._eventsDir,
|
|
300
|
+
this._componentsDir,
|
|
301
|
+
this._localesDir
|
|
302
|
+
].filter(Boolean) as string[], { ignoreInitial: true });
|
|
303
|
+
|
|
304
|
+
watcher.on('change', async (filePath) => {
|
|
305
|
+
console.log(`[djs-next] ♻️ File changed: ${filePath}. Reloading...`);
|
|
306
|
+
// We will just naively re-run the loaders.
|
|
307
|
+
// For events, we need to remove all listeners first to avoid memory leaks.
|
|
308
|
+
this.removeAllListeners();
|
|
309
|
+
this.commands.clear();
|
|
310
|
+
this.components.clear();
|
|
311
|
+
|
|
312
|
+
// Re-attach core listener
|
|
313
|
+
this.attachCoreListeners();
|
|
314
|
+
|
|
315
|
+
// Reload
|
|
316
|
+
if (this._eventsDir) await loadEvents(this, this._eventsDir);
|
|
317
|
+
if (this._componentsDir) this.components = await loadComponents(this._componentsDir);
|
|
318
|
+
if (this._commandsDir && this._clientId) {
|
|
319
|
+
this.commands = await loadAndDeployCommands(this._commandsDir, this.token!, this._clientId, this._guildId);
|
|
320
|
+
}
|
|
321
|
+
if (this._localesDir) loadLocales(this._localesDir, this.config.defaultLocale);
|
|
322
|
+
|
|
323
|
+
console.log(`[djs-next] ✅ Hot Reload complete.`);
|
|
324
|
+
});
|
|
325
|
+
} catch (e) {
|
|
326
|
+
console.warn(`[djs-next] chokidar not installed. HMR is disabled. Please run "npm install chokidar" to use this feature.`);
|
|
327
|
+
}
|
|
328
|
+
}
|
|
329
|
+
|
|
330
|
+
private async handleCommandError(error: any, commandKey: string, context: Interaction | Message) {
|
|
331
|
+
console.error(`[djs-next] Error executing command/component: "${commandKey}"`, error);
|
|
332
|
+
const djs = require('discord.js');
|
|
333
|
+
if (this.config.responses?.errorBoundary === null) {
|
|
334
|
+
// Developer explicitly opted out of error boundary user messages
|
|
335
|
+
} else {
|
|
336
|
+
try {
|
|
337
|
+
const errorContent = this.config.responses?.errorBoundary || `### ⚠️ Execution Error\n> The framework encountered a fatal error while executing \`${commandKey}\`.`;
|
|
338
|
+
const codeBlock = `\`\`\`js\n${String(error.stack || error.message).substring(0, 1500)}\n\`\`\``;
|
|
339
|
+
|
|
340
|
+
if ('commandName' in context || 'customId' in context) {
|
|
341
|
+
const container = new djs.ContainerBuilder()
|
|
342
|
+
.setAccentColor(0xED4245)
|
|
343
|
+
.addTextDisplayComponents(new djs.TextDisplayBuilder().setContent(errorContent))
|
|
344
|
+
.addSeparatorComponents(new djs.SeparatorBuilder())
|
|
345
|
+
.addTextDisplayComponents(new djs.TextDisplayBuilder().setContent(codeBlock));
|
|
346
|
+
|
|
347
|
+
const payload = { components: [container], flags: 32768, ephemeral: true };
|
|
348
|
+
const i = context as Interaction;
|
|
349
|
+
if (i.isRepliable()) {
|
|
350
|
+
if (i.replied || i.deferred) await i.followUp(payload);
|
|
351
|
+
else await i.reply(payload);
|
|
352
|
+
}
|
|
353
|
+
} else {
|
|
354
|
+
const embed = new djs.EmbedBuilder()
|
|
355
|
+
.setColor(0xED4245)
|
|
356
|
+
.setDescription(`${errorContent}\n\n${codeBlock}`);
|
|
357
|
+
|
|
358
|
+
const m = context as Message;
|
|
359
|
+
await m.reply({ embeds: [embed] });
|
|
360
|
+
}
|
|
361
|
+
} catch (e) {
|
|
362
|
+
// Fallback ignore if channel is dead
|
|
363
|
+
}
|
|
364
|
+
}
|
|
365
|
+
|
|
366
|
+
if (this.config.errorLogChannelId) {
|
|
367
|
+
try {
|
|
368
|
+
const channel = await this.channels.fetch(this.config.errorLogChannelId);
|
|
369
|
+
if (channel && channel.isTextBased()) {
|
|
370
|
+
const userId = 'user' in context ? context.user.id : context.author.id;
|
|
371
|
+
await (channel as any).send(`🚨 **Global Error Boundary Caught Exception**\n**Context:** \`${commandKey}\`\n**User:** <@${userId}>\n\`\`\`js\n${String(error.stack || error.message).substring(0, 1900)}\n\`\`\``);
|
|
372
|
+
}
|
|
373
|
+
} catch (e) {
|
|
374
|
+
console.error(`[djs-next] Failed to send error to log channel:`, e);
|
|
375
|
+
}
|
|
376
|
+
}
|
|
377
|
+
}
|
|
378
|
+
|
|
379
|
+
private async handlePreconditions(command: FileCommand | FileComponent, context: Interaction | Message, commandKey: string): Promise<boolean> {
|
|
380
|
+
const userId = 'user' in context ? context.user.id : context.author.id;
|
|
381
|
+
const isGuild = 'guild' in context && context.guild !== null;
|
|
382
|
+
const member = context.member as any;
|
|
383
|
+
|
|
384
|
+
const sendErr = async (msg: string | null) => {
|
|
385
|
+
if (msg === null) return;
|
|
386
|
+
if ('reply' in context && typeof context.reply === 'function') {
|
|
387
|
+
try {
|
|
388
|
+
if ('replied' in context && (context as any).isRepliable() && ((context as any).replied || (context as any).deferred)) {
|
|
389
|
+
await (context as any).followUp({ content: msg, ephemeral: true });
|
|
390
|
+
} else {
|
|
391
|
+
await (context as any).reply({ content: msg, ephemeral: true, allowedMentions: { repliedUser: false } });
|
|
392
|
+
}
|
|
393
|
+
} catch(e) {}
|
|
394
|
+
}
|
|
395
|
+
};
|
|
396
|
+
|
|
397
|
+
if ('developerOnly' in command && command.developerOnly && !this._developers.includes(userId)) {
|
|
398
|
+
await sendErr(this.config.responses?.developerOnly !== undefined ? this.config.responses.developerOnly : 'Only developers can use this command.');
|
|
399
|
+
return false;
|
|
400
|
+
}
|
|
401
|
+
if ('guildOnly' in command && command.guildOnly && !isGuild) {
|
|
402
|
+
await sendErr(this.config.responses?.guildOnly !== undefined ? this.config.responses.guildOnly : 'This command can only be used in a server.');
|
|
403
|
+
return false;
|
|
404
|
+
}
|
|
405
|
+
if ('userPermissions' in command && command.userPermissions && member?.permissions) {
|
|
406
|
+
const missing = member.permissions.missing(command.userPermissions);
|
|
407
|
+
if (missing.length > 0) {
|
|
408
|
+
await sendErr(this.config.responses?.missingPerms !== undefined ? (this.config.responses.missingPerms === null ? null : this.config.responses.missingPerms.replace('{perms}', missing.join(', '))) : `You are missing permissions: \`${missing.join(', ')}\``);
|
|
409
|
+
return false;
|
|
410
|
+
}
|
|
411
|
+
}
|
|
412
|
+
if ('botPermissions' in command && command.botPermissions && context.guild?.members.me?.permissions) {
|
|
413
|
+
const missing = context.guild.members.me.permissions.missing(command.botPermissions);
|
|
414
|
+
if (missing.length > 0) {
|
|
415
|
+
await sendErr(this.config.responses?.missingPerms !== undefined ? (this.config.responses.missingPerms === null ? null : this.config.responses.missingPerms.replace('{perms}', missing.join(', '))) : `I am missing permissions to run this: \`${missing.join(', ')}\``);
|
|
416
|
+
return false;
|
|
417
|
+
}
|
|
418
|
+
}
|
|
419
|
+
|
|
420
|
+
let totalCooldown = 'cooldown' in command && command.cooldown ? command.cooldown * 1000 : 0;
|
|
421
|
+
|
|
422
|
+
if (command.preconditions) {
|
|
423
|
+
for (const pre of command.preconditions) {
|
|
424
|
+
const parts = pre.split(':');
|
|
425
|
+
const type = parts[0].toLowerCase();
|
|
426
|
+
const val = parts.slice(1).join(':');
|
|
427
|
+
|
|
428
|
+
if (type === 'owneronly' || type === 'developeronly') {
|
|
429
|
+
if (!this._developers.includes(userId)) {
|
|
430
|
+
await sendErr(this.config.responses?.developerOnly !== undefined ? this.config.responses.developerOnly : 'Only developers can use this command.');
|
|
431
|
+
return false;
|
|
432
|
+
}
|
|
433
|
+
} else if (type === 'guildonly') {
|
|
434
|
+
if (!isGuild) {
|
|
435
|
+
await sendErr(this.config.responses?.guildOnly !== undefined ? this.config.responses.guildOnly : 'This command can only be used in a server.');
|
|
436
|
+
return false;
|
|
437
|
+
}
|
|
438
|
+
} else if (type === 'requireperms') {
|
|
439
|
+
const perms = val.split(',').map(p => p.trim());
|
|
440
|
+
if (member?.permissions) {
|
|
441
|
+
const missing = member.permissions.missing(perms);
|
|
442
|
+
if (missing.length > 0) {
|
|
443
|
+
await sendErr(this.config.responses?.missingPerms !== undefined ? (this.config.responses.missingPerms === null ? null : this.config.responses.missingPerms.replace('{perms}', missing.join(', '))) : `You are missing permissions: \`${missing.join(', ')}\``);
|
|
444
|
+
return false;
|
|
445
|
+
}
|
|
446
|
+
}
|
|
447
|
+
} else if (type === 'cooldown') {
|
|
448
|
+
const match = val.match(/(\d+)(s|m|h)/);
|
|
449
|
+
if (match) {
|
|
450
|
+
let amount = parseInt(match[1]) * 1000;
|
|
451
|
+
if (match[2] === 'm') amount *= 60;
|
|
452
|
+
if (match[2] === 'h') amount *= 3600;
|
|
453
|
+
totalCooldown = Math.max(totalCooldown, amount);
|
|
454
|
+
}
|
|
455
|
+
}
|
|
456
|
+
}
|
|
457
|
+
}
|
|
458
|
+
|
|
459
|
+
if (totalCooldown > 0) {
|
|
460
|
+
const now = Date.now();
|
|
461
|
+
|
|
462
|
+
if (this.config.cooldownAdapter) {
|
|
463
|
+
const lastUsed = await this.config.cooldownAdapter.get(commandKey, userId);
|
|
464
|
+
if (lastUsed) {
|
|
465
|
+
const expirationTime = lastUsed + totalCooldown;
|
|
466
|
+
if (now < expirationTime) {
|
|
467
|
+
const timeStr = `<t:${Math.round(expirationTime / 1000)}:R>`;
|
|
468
|
+
await sendErr(this.config.responses?.cooldown !== undefined ? (this.config.responses.cooldown === null ? null : this.config.responses.cooldown.replace('{time}', timeStr)) : `Please wait, you are on a cooldown. You can use it again ${timeStr}.`);
|
|
469
|
+
return false;
|
|
470
|
+
}
|
|
471
|
+
}
|
|
472
|
+
await this.config.cooldownAdapter.set(commandKey, userId, now);
|
|
473
|
+
} else {
|
|
474
|
+
if (!this.cooldowns.has(commandKey)) this.cooldowns.set(commandKey, new Collection());
|
|
475
|
+
const timestamps = this.cooldowns.get(commandKey)!;
|
|
476
|
+
|
|
477
|
+
if (timestamps.has(userId)) {
|
|
478
|
+
const expirationTime = timestamps.get(userId)! + totalCooldown;
|
|
479
|
+
if (now < expirationTime) {
|
|
480
|
+
const timeStr = `<t:${Math.round(expirationTime / 1000)}:R>`;
|
|
481
|
+
await sendErr(this.config.responses?.cooldown !== undefined ? (this.config.responses.cooldown === null ? null : this.config.responses.cooldown.replace('{time}', timeStr)) : `Please wait, you are on a cooldown. You can use it again ${timeStr}.`);
|
|
482
|
+
return false;
|
|
483
|
+
}
|
|
484
|
+
}
|
|
485
|
+
timestamps.set(userId, now);
|
|
486
|
+
setTimeout(() => timestamps.delete(userId), totalCooldown);
|
|
487
|
+
}
|
|
488
|
+
}
|
|
489
|
+
|
|
490
|
+
return true;
|
|
491
|
+
}
|
|
492
|
+
}
|
|
@@ -0,0 +1,139 @@
|
|
|
1
|
+
import { Collection, REST, Routes } from 'discord.js';
|
|
2
|
+
import fs from 'fs';
|
|
3
|
+
import path from 'path';
|
|
4
|
+
import { pathToFileURL } from 'url';
|
|
5
|
+
import { FileCommand } from '../types.js';
|
|
6
|
+
import { getAllFiles } from './utils.js';
|
|
7
|
+
|
|
8
|
+
interface CommandNode {
|
|
9
|
+
name: string;
|
|
10
|
+
description: string;
|
|
11
|
+
options: any[];
|
|
12
|
+
execute?: Function;
|
|
13
|
+
children: Map<string, CommandNode>;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
export async function loadAndDeployCommands(
|
|
17
|
+
commandsDir: string,
|
|
18
|
+
token: string,
|
|
19
|
+
clientId: string,
|
|
20
|
+
guildId?: string
|
|
21
|
+
): Promise<Collection<string, FileCommand>> {
|
|
22
|
+
const flatCommands = new Collection<string, FileCommand>();
|
|
23
|
+
|
|
24
|
+
if (!fs.existsSync(commandsDir)) {
|
|
25
|
+
console.warn(`[djs-next] Commands directory "${commandsDir}" does not exist.`);
|
|
26
|
+
return flatCommands;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
const commandFiles = getAllFiles(commandsDir);
|
|
30
|
+
const rootNodes = new Map<string, CommandNode>();
|
|
31
|
+
|
|
32
|
+
function getOrCreateNode(pathParts: string[]): CommandNode {
|
|
33
|
+
let currentMap = rootNodes;
|
|
34
|
+
let currentNode: CommandNode | undefined;
|
|
35
|
+
|
|
36
|
+
for (const part of pathParts) {
|
|
37
|
+
if (!currentMap.has(part)) {
|
|
38
|
+
currentMap.set(part, {
|
|
39
|
+
name: part,
|
|
40
|
+
description: `${part} command`, // Fallback description
|
|
41
|
+
options: [],
|
|
42
|
+
children: new Map()
|
|
43
|
+
});
|
|
44
|
+
}
|
|
45
|
+
currentNode = currentMap.get(part)!;
|
|
46
|
+
currentMap = currentNode.children;
|
|
47
|
+
}
|
|
48
|
+
return currentNode!;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
for (const file of commandFiles) {
|
|
52
|
+
const relativePath = path.relative(commandsDir, file);
|
|
53
|
+
const parsed = path.parse(relativePath);
|
|
54
|
+
|
|
55
|
+
// Normalize path separators
|
|
56
|
+
const dirParts = parsed.dir ? parsed.dir.split(path.sep) : [];
|
|
57
|
+
const name = parsed.name;
|
|
58
|
+
|
|
59
|
+
const pathParts = [...dirParts];
|
|
60
|
+
if (name !== 'index') {
|
|
61
|
+
pathParts.push(name);
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
if (pathParts.length === 0) continue;
|
|
65
|
+
if (pathParts.length > 3) {
|
|
66
|
+
console.warn(`[djs-next] Command path too deep (Discord allows max 3 levels): ${relativePath}`);
|
|
67
|
+
continue;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
const module = await import(pathToFileURL(file).href);
|
|
71
|
+
const commandData: FileCommand = module.default || module.command || module;
|
|
72
|
+
if (commandData) commandData.filepath = file;
|
|
73
|
+
|
|
74
|
+
const node = getOrCreateNode(pathParts);
|
|
75
|
+
if (commandData.description) node.description = commandData.description;
|
|
76
|
+
if (commandData.options) node.options = commandData.options;
|
|
77
|
+
if (commandData.execute) {
|
|
78
|
+
node.execute = commandData.execute;
|
|
79
|
+
// Map entire FileCommand object to a space-separated string (e.g. "economy balance")
|
|
80
|
+
flatCommands.set(pathParts.join(' '), commandData);
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
// Build JSON payloads for Discord
|
|
85
|
+
function buildCommandJSON(node: CommandNode, depth: number): any {
|
|
86
|
+
const json: any = {
|
|
87
|
+
name: node.name,
|
|
88
|
+
description: node.description,
|
|
89
|
+
};
|
|
90
|
+
|
|
91
|
+
if (node.children.size > 0) {
|
|
92
|
+
json.options = [];
|
|
93
|
+
for (const [_, childNode] of node.children) {
|
|
94
|
+
const childJson = buildCommandJSON(childNode, depth + 1);
|
|
95
|
+
|
|
96
|
+
// 1 = SUB_COMMAND, 2 = SUB_COMMAND_GROUP
|
|
97
|
+
if (depth === 0) {
|
|
98
|
+
childJson.type = childNode.children.size > 0 ? 2 : 1;
|
|
99
|
+
} else if (depth === 1) {
|
|
100
|
+
childJson.type = 1;
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
json.options.push(childJson);
|
|
104
|
+
}
|
|
105
|
+
} else if (node.options && node.options.length > 0) {
|
|
106
|
+
json.options = node.options;
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
return json;
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
const commandsData = Array.from(rootNodes.values()).map(node => buildCommandJSON(node, 0));
|
|
113
|
+
|
|
114
|
+
// Deploy commands
|
|
115
|
+
const rest = new REST({ version: '10' }).setToken(token);
|
|
116
|
+
|
|
117
|
+
try {
|
|
118
|
+
console.log(`[djs-next] Started refreshing ${commandsData.length} File-System application (/) commands.`);
|
|
119
|
+
|
|
120
|
+
let data: any;
|
|
121
|
+
if (guildId) {
|
|
122
|
+
data = await rest.put(
|
|
123
|
+
Routes.applicationGuildCommands(clientId, guildId),
|
|
124
|
+
{ body: commandsData },
|
|
125
|
+
);
|
|
126
|
+
} else {
|
|
127
|
+
data = await rest.put(
|
|
128
|
+
Routes.applicationCommands(clientId),
|
|
129
|
+
{ body: commandsData },
|
|
130
|
+
);
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
console.log(`[djs-next] Successfully reloaded ${data.length} File-System application (/) commands.`);
|
|
134
|
+
} catch (error) {
|
|
135
|
+
console.error(`[djs-next] Failed to deploy commands:`, error);
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
return flatCommands;
|
|
139
|
+
}
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import { Collection } from 'discord.js';
|
|
2
|
+
import fs from 'fs';
|
|
3
|
+
import path from 'path';
|
|
4
|
+
import { pathToFileURL } from 'url';
|
|
5
|
+
import { FileComponent } from '../types.js';
|
|
6
|
+
import { getAllFiles } from './utils.js';
|
|
7
|
+
|
|
8
|
+
export async function loadComponents(componentsDir: string): Promise<Collection<string, FileComponent>> {
|
|
9
|
+
const components = new Collection<string, FileComponent>();
|
|
10
|
+
|
|
11
|
+
if (!fs.existsSync(componentsDir)) return components;
|
|
12
|
+
|
|
13
|
+
const files = getAllFiles(componentsDir);
|
|
14
|
+
|
|
15
|
+
for (const file of files) {
|
|
16
|
+
const module = await import(pathToFileURL(file).href);
|
|
17
|
+
const componentData: FileComponent = module.default || module.component || module;
|
|
18
|
+
if (componentData) componentData.filepath = file;
|
|
19
|
+
|
|
20
|
+
if (!componentData || !componentData.execute) continue;
|
|
21
|
+
|
|
22
|
+
// Infer customId from filename if not explicitly provided
|
|
23
|
+
const parsed = path.parse(file);
|
|
24
|
+
const customId = componentData.customId || parsed.name;
|
|
25
|
+
|
|
26
|
+
components.set(customId, componentData);
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
console.log(`[djs-next] Successfully loaded ${components.size} components.`);
|
|
30
|
+
return components;
|
|
31
|
+
}
|