Skip to content

Commit b5ae82b

Browse files
committed
refactor: use map instead of record for commands and events
1 parent 30d76f4 commit b5ae82b

File tree

1 file changed

+29
-37
lines changed

1 file changed

+29
-37
lines changed

src/harmony.ts

Lines changed: 29 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,14 @@ import { getAllFiles } from './utils'
66
import { resolve } from 'path'
77
import { Client } from 'discord.js'
88

9+
type DeepRequired<T> = {
10+
[P in keyof T]: Required<DeepRequired<T[P]>>
11+
}
12+
913
export class Harmony {
10-
private _config: Required<HarmonyConfig> | null = null
11-
private _commands: Record<string, Command> = {}
12-
private _events: Record<string, Event> = {}
14+
private _config: Required<DeepRequired<HarmonyConfig>> | null = null
15+
private _commands: Map<string, Command> = new Map()
16+
private _events: Map<string, Event> = new Map()
1317
private _cwd: string | null = null
1418
private _client: Client | null = null
1519

@@ -46,53 +50,43 @@ export class Harmony {
4650
if (!this._config || !this._cwd) {
4751
throw new Error('Configuration not initialized')
4852
}
49-
const filesPath = getAllFiles(
50-
resolve(this._cwd, this._config!.dir.commands as string)
51-
)
53+
const filesPath = getAllFiles(resolve(this._cwd, this._config.dir.commands))
5254

53-
this._commands = Object.fromEntries(
54-
filesPath.map((path) => {
55-
const command = getCommand(path)
55+
for (const path of filesPath) {
56+
const command = getCommand(resolve(this._cwd, path))
5657

57-
return [command.name, command]
58-
})
59-
)
58+
this._commands.set(command.name, command)
59+
}
6060
}
6161

6262
private _initEvents() {
6363
if (!this._config || !this._cwd) {
6464
throw new Error('Configuration not initialized')
6565
}
66-
const filesPath = getAllFiles(
67-
resolve(this._cwd, this._config.dir.events as string)
68-
)
66+
const filesPath = getAllFiles(resolve(this._cwd, this._config.dir.events))
6967

70-
this._events = Object.fromEntries(
71-
filesPath.map((path) => {
72-
const event = getEvent(resolve(this._cwd as string, path))
68+
for (const path of filesPath) {
69+
const event = getEvent(resolve(this._cwd, path))
7370

74-
return [event.name, event]
75-
})
76-
)
71+
this._events.set(event.name, event)
72+
}
7773
}
7874

7975
private _refreshSlashCommands() {
8076
if (!this._client) {
8177
return
8278
}
8379

84-
for (const command in this._commands) {
85-
const c = this._commands[command]
86-
87-
if (!c.slash) {
80+
for (const [, command] of this._commands) {
81+
if (!command.slash) {
8882
continue
8983
}
9084

9185
this._client.application?.commands.create({
92-
name: c.name,
93-
description: c.description,
94-
options: c.arguments,
95-
nsfw: c.nsfw
86+
name: command.name,
87+
description: command.description,
88+
options: command.arguments,
89+
nsfw: command.nsfw
9690
})
9791
}
9892
}
@@ -104,13 +98,11 @@ export class Harmony {
10498

10599
this._refreshSlashCommands()
106100

107-
for (const event in this._events) {
108-
const e = this._events[event]
109-
110-
if (e.once) {
111-
this._client.once(e.name, e.callback)
101+
for (const [, event] of this._events) {
102+
if (event.once) {
103+
this._client.once(event.name, event.callback)
112104
} else {
113-
this._client.on(e.name, e.callback)
105+
this._client.on(event.name, event.callback)
114106
}
115107
}
116108

@@ -124,7 +116,7 @@ export class Harmony {
124116
const [commandName] = message.content
125117
.slice(this._config.defaultPrefix.length)
126118
.split(' ')
127-
const command = this._commands[commandName]
119+
const command = this._commands.get(commandName)
128120

129121
if (!command || command.slash) {
130122
return
@@ -136,7 +128,7 @@ export class Harmony {
136128
if (!this._config || !interaction.isChatInputCommand()) {
137129
return
138130
}
139-
const command = this._commands[interaction.commandName]
131+
const command = this._commands.get(interaction.commandName)
140132

141133
if (!command || !command.slash) {
142134
return

0 commit comments

Comments
 (0)