zumito-framework 1.7.3 → 1.8.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.
@@ -15,11 +15,19 @@ export class SlashCommandRefresher {
15
15
  const commands = Array.from(this.framework.commands.getAll().values())
16
16
  .filter((command) => (command.type == CommandType.slash ||
17
17
  command.type == CommandType.separated ||
18
- command.type == CommandType.any)
19
- && !command.parent)
18
+ command.type == CommandType.any) &&
19
+ !command.parent)
20
20
  .map(command => this.mapCommand(command));
21
- const data = await rest.put(Routes.applicationCommands(this.framework.settings.discordClientOptions.clientId), { body: commands });
22
- console.debug(`Successfully reloaded ${data.length} of ${commands.length} application (/) commands.`);
21
+ const uniqueCommands = [];
22
+ const names = new Set();
23
+ for (const cmd of commands) {
24
+ if (!names.has(cmd.name)) {
25
+ uniqueCommands.push(cmd);
26
+ names.add(cmd.name);
27
+ }
28
+ }
29
+ const data = await rest.put(Routes.applicationCommands(this.framework.settings.discordClientOptions.clientId), { body: uniqueCommands });
30
+ console.debug(`Successfully reloaded ${data.length} of ${uniqueCommands.length} application (/) commands.`);
23
31
  }
24
32
  mapCommand(command, commandBuilder) {
25
33
  const slashCommand = commandBuilder || new SlashCommandBuilder();
@@ -77,9 +85,9 @@ export class SlashCommandRefresher {
77
85
  Array.from(this.framework.commands.getAll().values())
78
86
  .filter((subcommand) => (subcommand.type == CommandType.slash ||
79
87
  subcommand.type == CommandType.separated ||
80
- subcommand.type == CommandType.any)
81
- && subcommand.parent
82
- && subcommand.parent == command.name)
88
+ subcommand.type == CommandType.any) &&
89
+ subcommand.parent &&
90
+ subcommand.parent == command.name)
83
91
  .forEach(subcommand => {
84
92
  if (!commandBuilder) {
85
93
  slashCommand.addSubcommand((subcommandSlashBuilder) => {
@@ -24,6 +24,6 @@ export declare class ErrorHandler {
24
24
  handleError(error: any, options: ErrorOptions): void;
25
25
  handleCommandError(error: Error, options: CommandErrorOptions): void;
26
26
  handleShapeShiftErrors(error: any): void;
27
- printErrorStack(error: Error): void;
27
+ printErrorStack(error: any): void;
28
28
  }
29
29
  export {};
@@ -11,28 +11,30 @@ export class ErrorHandler {
11
11
  if (options?.type == ErrorType.CommandInstance || options?.type == ErrorType.CommandLoad || options?.type == ErrorType.CommandRun) {
12
12
  this.handleCommandError(error, options);
13
13
  }
14
- else if (error.constructor.name == 'CombinedError') {
14
+ else if (error?.constructor?.name == 'CombinedError') {
15
15
  this.handleShapeShiftErrors(error);
16
16
  }
17
- else if (error.constructor?.name == "ExpectedValidationError") {
17
+ else if (error?.constructor?.name == "ExpectedValidationError") {
18
18
  console.error(`Validation error: Expected ${error.expected}, but received ${error.given}.`);
19
19
  console.line('');
20
20
  }
21
- else if (error.constructor.name == "ValidationError") {
21
+ else if (error?.constructor?.name == "ValidationError") {
22
22
  console.error(`Validation error: ${error.validator} received invalid input: ${error.given}`);
23
23
  console.line('');
24
24
  }
25
25
  else if (options?.type == ErrorType.Api) {
26
26
  console.group(`[❌] Error in API endpoint ${options.endpoint} (${options.method})`);
27
27
  console.line(chalk.red('Error:'));
28
- console.line(error.toString());
28
+ console.line(error?.toString?.() || 'Unknown error');
29
29
  console.line('');
30
30
  console.groupEnd();
31
31
  }
32
32
  else {
33
- console.error(error.toString());
33
+ console.error(error?.toString?.() || 'Unknown error');
34
34
  console.line('');
35
35
  }
36
+ // Emit framework error event so external modules can listen and report
37
+ this.framework.eventEmitter.emit('error', error, options);
36
38
  this.printErrorStack(error);
37
39
  if (options.exit)
38
40
  process.exit(1);
@@ -49,19 +51,19 @@ export class ErrorHandler {
49
51
  console.group(`[❌] Error running command ${options.command.name}`);
50
52
  break;
51
53
  }
52
- if (error.constructor.name == 'CombinedError') {
54
+ if (error?.constructor?.name == 'CombinedError') {
53
55
  console.groupEnd();
54
56
  this.handleShapeShiftErrors(error);
55
57
  }
56
58
  else {
57
59
  console.line(chalk.red('Error:'));
58
- console.line(error.toString());
60
+ console.line(error?.toString?.() || 'Unknown error');
59
61
  console.line('');
60
62
  console.groupEnd();
61
63
  }
62
64
  }
63
65
  handleShapeShiftErrors(error) {
64
- if (error.constructor.name == 'CombinedError') {
66
+ if (error?.constructor?.name == 'CombinedError') {
65
67
  error.errors.forEach(err => {
66
68
  this.handleError(err, {
67
69
  type: ErrorType.Other
@@ -70,7 +72,15 @@ export class ErrorHandler {
70
72
  }
71
73
  }
72
74
  printErrorStack(error) {
73
- const stackParsedError = ErrorStackParser.parse(error);
75
+ if (!error || !(error.stack || error.stacktrace))
76
+ return;
77
+ let stackParsedError;
78
+ try {
79
+ stackParsedError = ErrorStackParser.parse(error);
80
+ }
81
+ catch {
82
+ return;
83
+ }
74
84
  let functionColor = 'blue';
75
85
  const lines = [];
76
86
  let codeFragment;
@@ -126,9 +126,10 @@ export class CommandManager {
126
126
  async refreshSlashCommands() {
127
127
  const rest = new REST({ version: '10' }).setToken(this.framework.settings.discordClientOptions.token);
128
128
  const commands = Array.from(this.commands.values())
129
- .filter((command) => command.type == CommandType.slash ||
129
+ .filter((command) => (command.type == CommandType.slash ||
130
130
  command.type == CommandType.separated ||
131
- command.type == CommandType.any)
131
+ command.type == CommandType.any) &&
132
+ !command.parent)
132
133
  .map((command) => {
133
134
  const slashCommand = new SlashCommandBuilder()
134
135
  .setName(command.name)
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "zumito-framework",
3
- "version": "1.7.3",
3
+ "version": "1.8.0",
4
4
  "description": "Discord.js bot framework",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",