Skip to content

Commit a133349

Browse files
committed
👽️ feat: use new decorator
1 parent e3e3c73 commit a133349

File tree

8 files changed

+286
-291
lines changed

8 files changed

+286
-291
lines changed

src/client/client.ts

Lines changed: 18 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import { ChannelsManager } from '../managers/channels.ts'
1010
import { ClientPresence } from '../structures/presence.ts'
1111
import { EmojisManager } from '../managers/emojis.ts'
1212
import { ActivityGame, ClientActivity } from '../types/presence.ts'
13-
import type { Extension } from '../commands/extension.ts'
13+
import { Extension } from '../commands/extension.ts'
1414
import { InteractionsClient } from '../interactions/client.ts'
1515
import { ShardManager } from './shard.ts'
1616
import { Application } from '../structures/application.ts'
@@ -217,16 +217,6 @@ export class Client extends HarmonyEventEmitter<ClientEvents> {
217217
}
218218
if (options.compress !== undefined) this.compress = options.compress
219219

220-
if (
221-
(this as any)._decoratedEvents !== undefined &&
222-
Object.keys((this as any)._decoratedEvents).length !== 0
223-
) {
224-
Object.entries((this as any)._decoratedEvents).forEach((entry) => {
225-
this.on(entry[0] as keyof ClientEvents, (entry as any)[1].bind(this))
226-
})
227-
;(this as any)._decoratedEvents = undefined
228-
}
229-
230220
Object.defineProperty(this, 'clientProperties', {
231221
value:
232222
options.clientProperties === undefined
@@ -499,23 +489,24 @@ export class Client extends HarmonyEventEmitter<ClientEvents> {
499489
// eslint-disable-next-line @typescript-eslint/explicit-function-return-type
500490
export function event(name?: keyof ClientEvents) {
501491
return function (
502-
client: Client | Extension,
503-
prop: keyof ClientEvents | string
492+
original: (...args: any[]) => any,
493+
{
494+
name: prop,
495+
addInitializer,
496+
private: _private
497+
}: ClassMethodDecoratorContext<Client | Extension>
504498
) {
505-
// eslint-disable-next-line @typescript-eslint/no-unnecessary-type-assertion
506-
const c = client as any
507-
const listener = (
508-
client as unknown as {
509-
[name in keyof ClientEvents]: (...args: ClientEvents[name]) => any
499+
if (_private === true)
500+
throw new TypeError('Not supported on private methods.')
501+
502+
addInitializer(function () {
503+
const key = name === undefined ? prop.toString() : name
504+
if (this instanceof Client) {
505+
this.on(key as keyof ClientEvents, original.bind(this))
506+
} else if (this instanceof Extension) {
507+
this.listen(key as keyof ClientEvents, original.bind(this))
510508
}
511-
)[prop as unknown as keyof ClientEvents]
512-
if (typeof listener !== 'function') {
513-
throw new Error('@event decorator requires a function')
514-
}
515-
516-
if (c._decoratedEvents === undefined) c._decoratedEvents = {}
517-
const key = name === undefined ? prop : name
518-
519-
c._decoratedEvents[key] = listener
509+
})
510+
return original
520511
}
521512
}

src/commands/client.ts

Lines changed: 48 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -151,14 +151,6 @@ export class CommandClient extends Client implements CommandClientOptions {
151151
this.globalCommandCooldown = options.globalCommandCooldown ?? 0
152152
this.globalCooldown = options.globalCooldown ?? 0
153153

154-
const self = this as any
155-
if (self._decoratedCommands !== undefined) {
156-
Object.values(self._decoratedCommands).forEach((entry: any) => {
157-
this.commands.add(entry)
158-
})
159-
self._decoratedCommands = undefined
160-
}
161-
162154
this.on(
163155
'messageCreate',
164156
async (msg: Message) => await this.processMessage(msg)
@@ -527,49 +519,62 @@ export class CommandClient extends Client implements CommandClientOptions {
527519
* Command decorator. Decorates the function with optional metadata as a Command registered upon constructing class.
528520
*/
529521
export function command(options?: CommandOptions) {
530-
return function (target: CommandClient | Extension, name: string) {
531-
// eslint-disable-next-line @typescript-eslint/no-unnecessary-type-assertion
532-
const c = target as any
533-
if (c._decoratedCommands === undefined) c._decoratedCommands = {}
534-
535-
const prop = c[name]
536-
537-
if (typeof prop !== 'function')
538-
throw new Error('@command decorator can only be used on class methods')
539-
540-
const command = new Command()
541-
542-
command.name = name
543-
command.execute = prop
544-
545-
if (options !== undefined) Object.assign(command, options)
522+
return function (
523+
original: (...args: any[]) => any,
524+
{
525+
name,
526+
addInitializer,
527+
private: _private
528+
}: ClassMethodDecoratorContext<CommandClient | Extension>
529+
) {
530+
if (_private === true)
531+
throw new TypeError('Not supported on private methods.')
532+
533+
addInitializer(function () {
534+
const command = new Command()
535+
536+
command.name = name.toString()
537+
command.execute = original.bind(this)
538+
539+
if (options !== undefined) Object.assign(command, options)
540+
541+
if (this instanceof Extension) {
542+
command.extension = this
543+
}
546544

547-
if (target instanceof Extension) command.extension = target
545+
this.commands.add(command)
546+
})
548547

549-
c._decoratedCommands[command.name] = command
548+
return original
550549
}
551550
}
552551

553552
/**
554553
* Sub Command decorator. Decorates the function with optional metadata as a Sub Command registered upon constructing class.
555554
*/
556555
export function subcommand(options?: CommandOptions) {
557-
return function (target: Command, name: string) {
558-
// eslint-disable-next-line @typescript-eslint/no-unnecessary-type-assertion
559-
const c = target as any
560-
if (c._decoratedSubCommands === undefined) c._decoratedSubCommands = []
561-
562-
const prop = c[name]
563-
564-
if (typeof prop !== 'function')
565-
throw new Error('@command decorator can only be used on class methods')
566-
567-
const command = new Command()
568-
569-
command.name = name
570-
command.execute = prop
571-
572-
if (options !== undefined) Object.assign(command, options)
573-
c._decoratedSubCommands.push(command)
556+
return function (
557+
original: (...args: any[]) => any,
558+
{
559+
name,
560+
addInitializer,
561+
private: _private
562+
}: ClassMethodDecoratorContext<Command>
563+
) {
564+
if (_private === true)
565+
throw new TypeError('Not supported on private methods.')
566+
567+
addInitializer(function () {
568+
const command = new Command()
569+
570+
command.name = name.toString()
571+
command.execute = original.bind(this)
572+
573+
if (options !== undefined) Object.assign(command, options)
574+
if (this.subCommands === undefined) this.subCommands = []
575+
this.subCommands.push(command)
576+
})
577+
578+
return original
574579
}
575580
}

src/commands/command.ts

Lines changed: 1 addition & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -111,8 +111,6 @@ export class Command implements CommandOptions {
111111
/** Global command cooldown in MS */
112112
globalCooldown?: number
113113

114-
declare readonly _decoratedSubCommands?: Command[]
115-
116114
/** Method called when the command errors */
117115
onError(ctx: CommandContext, error: Error): unknown | Promise<unknown> {
118116
return
@@ -153,24 +151,9 @@ export class Command implements CommandOptions {
153151
}`
154152
}
155153

156-
constructor() {
157-
if (
158-
this._decoratedSubCommands !== undefined &&
159-
this._decoratedSubCommands.length > 0
160-
) {
161-
if (this.subCommands === undefined) this.subCommands = []
162-
const commands = this._decoratedSubCommands
163-
delete (this as unknown as Record<string, unknown>)._decoratedSubCommands
164-
Object.defineProperty(this, '_decoratedSubCommands', {
165-
value: commands,
166-
enumerable: false
167-
})
168-
}
169-
}
170-
171154
/** Get an Array of Sub Commands, including decorated ones */
172155
getSubCommands(): Command[] {
173-
return [...(this._decoratedSubCommands ?? []), ...(this.subCommands ?? [])]
156+
return this.subCommands ?? []
174157
}
175158
}
176159

src/interactions/client.ts

Lines changed: 2 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -131,60 +131,6 @@ export class InteractionsClient extends HarmonyEventEmitter<InteractionsClientEv
131131

132132
this.enabled = options.enabled ?? true
133133

134-
// eslint-disable-next-line @typescript-eslint/no-unnecessary-type-assertion
135-
const client = this.client as unknown as {
136-
_decoratedAppCmd: ApplicationCommandHandler[]
137-
_decoratedAutocomplete: AutocompleteHandler[]
138-
_decoratedComponents: ComponentInteractionHandler[]
139-
}
140-
if (client?._decoratedAppCmd !== undefined) {
141-
client._decoratedAppCmd.forEach((e) => {
142-
e.handler = e.handler.bind(this.client)
143-
this.handlers.push(e)
144-
})
145-
}
146-
147-
if (client?._decoratedAutocomplete !== undefined) {
148-
client._decoratedAutocomplete.forEach((e) => {
149-
e.handler = e.handler.bind(this.client)
150-
this.autocompleteHandlers.push(e)
151-
})
152-
}
153-
154-
if (client?._decoratedComponents !== undefined) {
155-
client._decoratedComponents.forEach((e) => {
156-
e.handler = e.handler.bind(this.client)
157-
this.componentHandlers.push(e)
158-
})
159-
}
160-
161-
const self = this as unknown as InteractionsClient & {
162-
_decoratedAppCmd: ApplicationCommandHandler[]
163-
_decoratedAutocomplete: AutocompleteHandler[]
164-
_decoratedComponents: ComponentInteractionHandler[]
165-
}
166-
167-
if (self._decoratedAppCmd !== undefined) {
168-
self._decoratedAppCmd.forEach((e) => {
169-
e.handler = e.handler.bind(this.client)
170-
self.handlers.push(e)
171-
})
172-
}
173-
174-
if (self._decoratedAutocomplete !== undefined) {
175-
self._decoratedAutocomplete.forEach((e) => {
176-
e.handler = e.handler.bind(this.client)
177-
self.autocompleteHandlers.push(e)
178-
})
179-
}
180-
181-
if (self._decoratedComponents !== undefined) {
182-
self._decoratedComponents.forEach((e) => {
183-
e.handler = e.handler.bind(this.client)
184-
self.componentHandlers.push(e)
185-
})
186-
}
187-
188134
Object.defineProperty(this, 'rest', {
189135
value:
190136
options.client === undefined
@@ -540,9 +486,9 @@ export class InteractionsClient extends HarmonyEventEmitter<InteractionsClientEv
540486
])
541487

542488
return edverify(
543-
decodeHex(encodeText(this.publicKey)),
489+
decodeHex(this.publicKey),
544490
decodeHex(
545-
signature instanceof Uint8Array ? signature : encodeText(signature)
491+
signature instanceof Uint8Array ? decodeText(signature) : signature
546492
),
547493
fullBody
548494
)

src/interactions/commandModule.ts

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -10,20 +10,6 @@ export class ApplicationCommandsModule {
1010
autocomplete: AutocompleteHandler[] = []
1111
components: ComponentInteractionHandler[] = []
1212

13-
constructor() {
14-
if ((this as any)._decoratedAppCmd !== undefined) {
15-
this.commands = (this as any)._decoratedAppCmd
16-
}
17-
18-
if ((this as any)._decoratedAutocomplete !== undefined) {
19-
this.autocomplete = (this as any)._decoratedAutocomplete
20-
}
21-
22-
if ((this as any)._decoratedComponents !== undefined) {
23-
this.components = (this as any)._decoratedComponents
24-
}
25-
}
26-
2713
add(handler: ApplicationCommandHandler): this {
2814
this.commands.push(handler)
2915
return this

0 commit comments

Comments
 (0)