Skip to content

Commit 7a36812

Browse files
committed
fix: format
1 parent af90cab commit 7a36812

7 files changed

Lines changed: 30 additions & 33 deletions

File tree

README.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,3 @@ Command handler for discord.js
1010
- Command type (global/guild)
1111
- Command options/arguments/sub-commands
1212
- Listen for commands
13-

eslint.config.mjs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
import globals from 'globals';
21
import eslint from '@eslint/js';
32
import importPlugin from 'eslint-plugin-import';
43
import tseslint from 'typescript-eslint';
4+
import globals from 'globals';
55

66
export default tseslint.config(eslint.configs.recommended, tseslint.configs.recommended, {
7-
files: ['**/*.{ts,js}'],
7+
files: ['**/*.{ts,js,mjs}'],
88
languageOptions: {
99
parserOptions: {
1010
ecmaVersion: 'latest',

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,8 @@
4444
"scripts": {
4545
"build": "tsc --project tsconfig.build.json",
4646
"build:ci": "tsc --project tsconfig.build.json --noEmit",
47-
"format": "prettier --write . eslint --fix --format=pretty src",
48-
"lint": "prettier --check . eslint --format=pretty src",
47+
"format": "prettier --write . && eslint --fix --format=pretty src",
48+
"lint": "prettier --check . && eslint --format=pretty src",
4949
"prepare": "npm run build"
5050
}
5151
}

src/handler/AppCommandHandler.ts

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,14 @@
1-
import { Dirent, readdirSync } from 'node:fs';
2-
import { stat } from 'node:fs/promises';
3-
import { join } from 'node:path';
41
import ky from 'ky';
5-
import { AppCommand } from '../models/AppCommand.js';
6-
import { InvalidAppCommandError, InvalidHookPath } from '../models/errors.js';
7-
import { Awaitable } from '../typings.js';
8-
import { Router } from '../router/Router.js';
92
import { AppCommandType } from '../models/AppCommandShape.js';
3+
import { CommandUpdateError, InvalidAppCommandError } from '../models/errors.js';
4+
import { Router } from '../router/Router.js';
105

116
export class AppCommandHandler<Args extends unknown[]> {
127
private readonly router: Router<Args>;
138
private readonly clientId: string;
149
private readonly token: string;
1510

16-
constructor(router: Router<Args>, { clientId, token }: { clientId: string, token: string; }) {
11+
constructor(router: Router<Args>, { clientId, token }: { clientId: string; token: string }) {
1712
this.router = router;
1813
this.clientId = clientId;
1914
this.token = token;
@@ -22,19 +17,19 @@ export class AppCommandHandler<Args extends unknown[]> {
2217
public async overwriteCommands(): Promise<void> {
2318
try {
2419
await ky.put(`https://discord.com/api/v10/applications/${this.clientId}/commands`, {
25-
headers: { 'Authorization': `Bot ${this.token}` },
20+
headers: { Authorization: `Bot ${this.token}` },
2621
json: this.router.commandData,
2722
});
2823
} catch (error) {
29-
throw new Error('Failed to update Global Commands.');
24+
throw new CommandUpdateError('Failed to overwrite commands:\n\t' + error);
3025
}
3126
}
3227

3328
public async run(commandName: string, commandType: AppCommandType, ...args: Args): Promise<void> {
3429
const commandFn = this.router.get(commandName, commandType);
3530

3631
if (!commandFn) {
37-
throw new InvalidAppCommandError(`Command ${commandName} does not exist.`, 'INVALID_COMMAND');
32+
throw new InvalidAppCommandError(`Command ${commandName} does not exist.`);
3833
}
3934

4035
await commandFn(...args);

src/models/AppCommand.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,7 @@ import { Awaitable } from '../typings.js';
44
export abstract class AppCommand<R = void> {
55
private readonly shape: AppCommandShape;
66

7-
constructor({
8-
shape,
9-
}: AppCommandParams) {
7+
constructor({ shape }: AppCommandParams) {
108
this.shape = shape;
119
}
1210

src/models/errors.ts

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,21 @@
1-
export class InvalidAppCommandError extends Error {
2-
public readonly code: string;
1+
export abstract class CommanderError extends Error {
2+
public abstract readonly code: string;
3+
}
4+
5+
export class InvalidAppCommandError extends CommanderError {
6+
public readonly code = 'INVALID_APP_COMMAND';
37

4-
constructor(message: string, code: string) {
8+
constructor(message: string) {
59
super(message);
610
this.name = this.constructor.name;
7-
this.code = code;
811
}
912
}
1013

11-
export class InvalidHookPath extends Error {
12-
public readonly code: string;
14+
export class CommandUpdateError extends CommanderError {
15+
public readonly code: string = 'COMMAND_UPDATE_ERROR';
1316

14-
constructor(message: string, code: string) {
17+
constructor(message: string) {
1518
super(message);
1619
this.name = this.constructor.name;
17-
this.code = code;
1820
}
1921
}

src/router/Router.ts

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,17 @@ export class Router<Args extends unknown[]> {
55
private readonly _commands: [AppCommandShape, CommandFn<Args>][] = [];
66

77
/**
8-
* TODO: JAREN
8+
* Get all command data
9+
* @returns {readonly AppCommandShape[]}
910
*/
1011
public get commandData(): readonly AppCommandShape[] {
11-
return this._commands.map(([commandData, _]) => commandData);
12+
return this._commands.map(command => command[0]);
1213
}
1314

1415
/**
1516
* Register a command
16-
* @param {AppCommandShape} commandData
17-
* @param {CommandFn} command
17+
* @param {AppCommandShape} commandData
18+
* @param {CommandFn} command
1819
* @returns {void}
1920
*/
2021
public command(commandData: AppCommandShape, command: CommandFn<Args>): void {
@@ -25,11 +26,13 @@ export class Router<Args extends unknown[]> {
2526
* Get a command by name and type
2627
* @param {string} commandName
2728
* @param {AppCommandType} commandType
28-
*
29+
*
2930
* @returns CommandHandler | null
3031
*/
3132
public get(commandName: string, commandType: AppCommandType): CommandFn<Args> | null {
32-
const command = this._commands.find(([commandData]) => commandData.name === commandName && commandData.type === commandType);
33+
const command = this._commands.find(
34+
([commandData]) => commandData.name === commandName && commandData.type === commandType,
35+
);
3336

3437
return command?.[1] ?? null;
3538
}

0 commit comments

Comments
 (0)