11import { Events } from 'discord.js'
22import consola from 'consola'
33import { colors } from 'consola/utils'
4- import { optionToArg , resolveArgument } from './utils'
4+ import { toOption , resolveOption } from './utils'
55import { ctx } from './harmonix'
6- import type { Harmonix , HarmonixEvents , ParsedArgs } from './types'
6+ import type { Harmonix , HarmonixEvents , ParsedOptions } from './types'
77
88export const registerEvents = ( harmonix : Harmonix ) => {
99 for ( const [ , event ] of harmonix . events . filter ( ( evt ) => ! evt . options . type ) ) {
@@ -36,86 +36,32 @@ export const registerEvents = (harmonix: Harmonix) => {
3636 } )
3737}
3838
39- export const registerMessageCommands = ( harmonix : Harmonix ) => {
40- harmonix . client ?. on ( Events . MessageCreate , async ( message ) => {
41- if ( message . author . bot ) return
42- const prefix = harmonix . options . defaultPrefix
43- const rawArgs = message . content . slice ( prefix . length ) . trim ( ) . split ( / + / )
44- const command = rawArgs . shift ( ) ?. toLowerCase ( )
45-
46- if ( ! command ) return
47- const cmd = harmonix . commands
48- . filter ( ( cmd ) => ! cmd . options . slash )
49- . find ( ( cmd ) => cmd . options . name === command )
39+ export const registerCommands = ( harmonix : Harmonix ) => {
40+ harmonix . client ?. on ( Events . InteractionCreate , async ( interaction ) => {
41+ if ( ! interaction . isChatInputCommand ( ) ) return
42+ const cmd = harmonix . commands . find (
43+ ( cmd ) => cmd . config . name === interaction . commandName
44+ )
5045
5146 if ( ! cmd ) return
52- const args = await Object . keys ( cmd . options . args || { } ) . reduce <
53- Promise < ParsedArgs >
54- > ( async ( acc , arg , index ) => {
47+ const options = await interaction . options . data . reduce <
48+ Promise < ParsedOptions >
49+ > ( async ( acc , opt ) => {
5550 const resolvedAcc = await acc
56- const resolvedArg = await resolveArgument (
57- message ,
58- cmd . options . args ! [ arg ] . type ,
59- rawArgs [ index ]
51+ const resolvedOption = await resolveOption (
52+ interaction ,
53+ toOption ( opt . type ) ,
54+ String ( opt . value )
6055 )
6156
6257 return {
6358 ...resolvedAcc ,
64- [ arg ] : resolvedArg
59+ [ opt . name ] : resolvedOption
6560 }
6661 } , Promise . resolve ( { } ) )
6762
68- if ( cmd . options . preconditions ) {
69- for ( const prc of cmd . options . preconditions ) {
70- const precondition = harmonix . preconditions . get ( prc )
71-
72- if ( ! precondition ) {
73- consola . warn ( `Precondition ${ colors . cyan ( prc ) } not found.` )
74- continue
75- }
76- const result = ctx . call ( harmonix , ( ) => {
77- return precondition . callback ( {
78- type : 'message' ,
79- message
80- } )
81- } )
82-
83- if ( ! result ) return
84- }
85- }
86- ctx . call ( harmonix , ( ) => {
87- cmd . execute ( harmonix . client ! , message , { slash : false , args } )
88- } )
89- } )
90- }
91-
92- export const registerSlashCommands = ( harmonix : Harmonix ) => {
93- harmonix . client ?. on ( Events . InteractionCreate , async ( interaction ) => {
94- if ( ! interaction . isChatInputCommand ( ) ) return
95- const cmd = harmonix . commands
96- . filter ( ( cmd ) => cmd . options . slash )
97- . find ( ( cmd ) => cmd . options . name === interaction . commandName )
98-
99- if ( ! cmd ) return
100- const args = await interaction . options . data . reduce < Promise < ParsedArgs > > (
101- async ( acc , opt ) => {
102- const resolvedAcc = await acc
103- const resolvedArg = await resolveArgument (
104- interaction ,
105- optionToArg ( opt . type ) ,
106- String ( opt . value )
107- )
108-
109- return {
110- ...resolvedAcc ,
111- [ opt . name ] : resolvedArg
112- }
113- } ,
114- Promise . resolve ( { } )
115- )
116-
117- if ( cmd . options . preconditions ) {
118- for ( const prc of cmd . options . preconditions ) {
63+ if ( cmd . config . preconditions ) {
64+ for ( const prc of cmd . config . preconditions ) {
11965 const precondition = harmonix . preconditions . get ( prc )
12066
12167 if ( ! precondition ) {
@@ -134,8 +80,7 @@ export const registerSlashCommands = (harmonix: Harmonix) => {
13480 }
13581 ctx . call ( harmonix , ( ) => {
13682 cmd . execute ( harmonix . client ! , interaction , {
137- slash : false ,
138- args
83+ options
13984 } )
14085 } )
14186 } )
@@ -145,12 +90,12 @@ export const registerContextMenu = (harmonix: Harmonix) => {
14590 harmonix . client ?. on ( Events . InteractionCreate , async ( interaction ) => {
14691 if ( ! interaction . isContextMenuCommand ( ) ) return
14792 const ctm = harmonix . contextMenus . find (
148- ( ctm ) => ctm . options . name === interaction . commandName
93+ ( ctm ) => ctm . config . name === interaction . commandName
14994 )
15095
15196 if ( ! ctm ) return
152- if ( ctm . options . preconditions ) {
153- for ( const prc of ctm . options . preconditions ) {
97+ if ( ctm . config . preconditions ) {
98+ for ( const prc of ctm . config . preconditions ) {
15499 const precondition = harmonix . preconditions . get ( prc )
155100
156101 if ( ! precondition ) {
0 commit comments