@ezzi/base 1.1.0 → 2.0.0

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.
@@ -80,7 +80,6 @@ export interface GroupOptionData<Contexts> extends Omit<BaseOptionData, "require
80
80
  export type SlashCommandOptionData<Contexts> = SlashCommandPrimitiveOptionData<Contexts> | GroupOptionData<Contexts> | SubCommandOptionData<Contexts>;
81
81
  export type RunInteraction<T, Contexts> = T extends ApplicationCommandType.Message ? MessageContextMenuCommandInteraction<CacheMode<Contexts>> : T extends ApplicationCommandType.User ? UserContextMenuCommandInteraction<CacheMode<Contexts>> : ChatInputCommandInteraction<CacheMode<Contexts>>;
82
82
  interface CommandRunThis {
83
- /** Blocks the flow of executions */
84
83
  block(): never;
85
84
  }
86
85
  type BaseAppCommandData = Omit<BaseApplicationCommandData, "contexts" | "dmPermission"> & Pick<BaseOptionData, "descriptionLocalizations">;
@@ -133,6 +132,7 @@ declare class GroupCommandModule<Type, Contexts extends readonly InteractionCont
133
132
  export declare class Command<Type, Contexts extends readonly InteractionContextType[] = readonly InteractionContextType[], Return = unknown> {
134
133
  readonly modules: CommandModule[];
135
134
  readonly data: AppCommandData<Type, Contexts, Return>;
135
+ moduleListener?: (module: CommandModule) => void;
136
136
  constructor(data: AppCommandData<Type, Contexts, Return>);
137
137
  group<ModuleReturn = Return>(data: SubCommandGroupModuleData<Contexts, Return, ModuleReturn>): GroupCommandModule<Type, Contexts, Return, ModuleReturn>;
138
138
  subcommand<R = Return>(data: SubCommandModuleData<Contexts, R>): this;
@@ -26,6 +26,7 @@ class GroupCommandModule {
26
26
  class Command {
27
27
  modules = [];
28
28
  data;
29
+ moduleListener;
29
30
  constructor(data) {
30
31
  this.data = data;
31
32
  this.data.type ??= ApplicationCommandType.ChatInput;
@@ -41,17 +42,21 @@ class Command {
41
42
  }
42
43
  }
43
44
  group(data) {
44
- this.modules.push({
45
+ const module = {
45
46
  ...data,
46
47
  type: ApplicationCommandOptionType.SubcommandGroup
47
- });
48
+ };
49
+ this.modules.push(module);
50
+ this.moduleListener?.(module);
48
51
  return new GroupCommandModule(this, data);
49
52
  }
50
53
  subcommand(data) {
51
- this.modules.push({
54
+ const module = {
52
55
  ...data,
53
56
  type: ApplicationCommandOptionType.Subcommand
54
- });
57
+ };
58
+ this.modules.push(module);
59
+ this.moduleListener?.(module);
55
60
  return this;
56
61
  }
57
62
  }
@@ -66,8 +66,6 @@ class CommandManager {
66
66
  for (const [k, v] of this.aliasCollection) {
67
67
  this.runtimeCollection.set(k, v);
68
68
  }
69
- this.collection.clear();
70
- this.aliasCollection.clear();
71
69
  }
72
70
  getAutocompleteHandler(...path) {
73
71
  const commandName = path[0];
@@ -162,6 +160,9 @@ class CommandManager {
162
160
  options: subcommands,
163
161
  defaultMemberPermissions,
164
162
  botPermissions,
163
+ run: _groupRun,
164
+ ignore: _groupIgnore,
165
+ aliases: _groupAliases,
165
166
  ...data
166
167
  } = option;
167
168
  if (botPermissions?.length) {
@@ -180,6 +181,11 @@ class CommandManager {
180
181
  options: subOpts,
181
182
  defaultMemberPermissions,
182
183
  botPermissions,
184
+ run: _subRun,
185
+ ignore: _subIgnore,
186
+ aliases: _subAliases,
187
+ shortcut: _subShortcut,
188
+ group: _subGroup,
183
189
  ...data
184
190
  } = option;
185
191
  if (botPermissions?.length) {
@@ -272,21 +278,22 @@ class CommandManager {
272
278
  const type = data.type ?? ApplicationCommandType.ChatInput;
273
279
  const name = this.formatName(data.name, type);
274
280
  const dmPermission = data.dmPermission ?? false;
281
+ const existingData = this.collection.get(name);
275
282
  const commandData = {
276
283
  ...data,
277
284
  name,
278
285
  type,
279
- dmPermission
286
+ dmPermission,
287
+ modules: existingData?.modules ?? []
280
288
  };
281
- if (this.collection.has(name)) {
282
- const existing = this.collection.get(name);
289
+ if (existingData) {
283
290
  const canonicalPath2 = `/${type}/${name}`;
284
291
  this.commandRunners.delete(canonicalPath2);
285
292
  this.optionDefs.delete(canonicalPath2);
286
293
  this.botPermissionsMap.delete(canonicalPath2);
287
294
  this.autocompleteRunners.delete(canonicalPath2);
288
- if (existing.aliases?.length) {
289
- for (const alias of existing.aliases) {
295
+ if (existingData.aliases?.length) {
296
+ for (const alias of existingData.aliases) {
290
297
  this.aliasCollection.delete(this.formatName(alias, type));
291
298
  }
292
299
  }
@@ -323,6 +330,9 @@ class CommandManager {
323
330
  descriptionLocalizations,
324
331
  category,
325
332
  botPermissions: _bp,
333
+ run: _run,
334
+ ignore: _ignore,
335
+ aliases: _aliases,
326
336
  ...data
327
337
  } = raw;
328
338
  const path = `/${data.type}/${data.name}`;
@@ -331,7 +341,7 @@ class CommandManager {
331
341
  descriptionLocalizations,
332
342
  options: this.buildOptions([
333
343
  ...options ?? [],
334
- ...this.resolveModules(modules ?? [], path, data.run)
344
+ ...this.resolveModules(modules ?? [], path, raw.run)
335
345
  ], path)
336
346
  } : {};
337
347
  return { ...data, ...slashData, category };
@@ -344,8 +354,10 @@ class CommandManager {
344
354
  }
345
355
  addModule(commandName, module) {
346
356
  const command = this.collection.get(commandName);
347
- if (!command)
357
+ if (!command) {
358
+ console.warn(`[EzziApp] Comando "${commandName}" não encontrado ao registrar o módulo "${module.name}". Verifique se createCommand foi chamado antes de subcommand/group para esse comando.`);
348
359
  return;
360
+ }
349
361
  command.modules ??= [];
350
362
  command.modules.push(module);
351
363
  }
@@ -513,6 +525,10 @@ class CommandManager {
513
525
  const messages = [];
514
526
  const pluralize = (n) => n > 1 ? "s" : "";
515
527
  const commands = this.build();
528
+ const stripInternal = (cmd) => {
529
+ const { global: _global, category: _category, ...rest } = cmd;
530
+ return rest;
531
+ };
516
532
  const createVerboseLogs = (cmdsCollection) => cmdsCollection.map(({ id, name, type, client: cl, createdAt, guild }) => {
517
533
  const [icon] = this.getTitle({
518
534
  type
@@ -528,21 +544,26 @@ class CommandManager {
528
544
  }
529
545
  };
530
546
  const targetGuilds = client.guilds.cache.filter(({ id }) => this.config.guilds?.includes(id));
531
- if (targetGuilds.size) {
532
- const globalCommands = commands.filter((c) => c.global);
533
- const guildCommands = commands.filter((c) => !c.global);
534
- await client.application.commands.set(globalCommands).then((cmds) => {
535
- if (!cmds.size)
536
- return;
537
- logRegistration(cmds, "globally");
538
- });
539
- for (const guild of targetGuilds.values()) {
540
- const cmds = await guild.commands.set(guildCommands);
541
- logRegistration(cmds, `in ${ck.underline(guild.name)} guild`);
547
+ try {
548
+ if (targetGuilds.size) {
549
+ const globalCommands = commands.filter((c) => c.global).map(stripInternal);
550
+ const guildCommands = commands.filter((c) => !c.global).map(stripInternal);
551
+ await client.application.commands.set(globalCommands).then((cmds) => {
552
+ if (!cmds.size)
553
+ return;
554
+ logRegistration(cmds, "globally");
555
+ });
556
+ for (const guild of targetGuilds.values()) {
557
+ const cmds = await guild.commands.set(guildCommands);
558
+ logRegistration(cmds, `in ${ck.underline(guild.name)} guild`);
559
+ }
560
+ } else {
561
+ await Promise.all(client.guilds.cache.map((guild) => guild.commands.set([])));
562
+ const globalCommands = commands.map(stripInternal);
563
+ await client.application.commands.set(globalCommands).then((cmds) => logRegistration(cmds, "globally"));
542
564
  }
543
- } else {
544
- await Promise.all(client.guilds.cache.map((guild) => guild.commands.set([])));
545
- await client.application.commands.set(commands).then((cmds) => logRegistration(cmds, "globally"));
565
+ } catch (err) {
566
+ console.error("[EzziApp] Falha ao registrar comandos no Discord:", err);
546
567
  }
547
568
  const prefixCount = this.getPrefixCommandCount();
548
569
  if (prefixCount > 0 && this.config.prefix) {
@@ -11,13 +11,6 @@ export interface SetupCreatorsOptions {
11
11
  responders?: Partial<BaseRespondersConfig>;
12
12
  events?: Partial<BaseEventsConfig>;
13
13
  }
14
- /**
15
- * Initializes the Ezzi command/event/responder creation system.
16
- *
17
- * This function configures the Ezzi application’s internal registries
18
- * for commands, events, and responders, and returns a set of factory
19
- * functions used to create each type of component.
20
- */
21
14
  export declare function setupCreators(options?: SetupCreatorsOptions): {
22
15
  createCommand<T extends CommandType = ApplicationCommandType.ChatInput, const C extends readonly InteractionContextType[] = [InteractionContextType.Guild], R = void>(data: AppCommandData<T, C, R>): Command<T, C, R>;
23
16
  createEvent<EventName extends keyof ClientEvents>(data: EventData<EventName>): void;
@@ -29,6 +29,9 @@ function setupCreators(options = {}) {
29
29
  data.botPermissions = defaultBotPerms;
30
30
  const command = new Command(data);
31
31
  const resolved = currentApp.commands.set(command.data);
32
+ command.moduleListener = (module) => {
33
+ currentApp.commands.addModule(resolved.name, module);
34
+ };
32
35
  if (typeof currentApp.commands.addLog === "function") {
33
36
  currentApp.commands.addLog(resolved);
34
37
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ezzi/base",
3
- "version": "1.1.0",
3
+ "version": "2.0.0",
4
4
  "description": "Library with structures and functions for creating modern Discord applications.",
5
5
  "license": "MIT",
6
6
  "type": "module",