1- import { Client , REST , Routes , Events } from 'discord.js'
2- import type { Harmonix , HarmonixCommand , HarmonixEvent } from './types'
1+ import {
2+ Client ,
3+ REST ,
4+ Routes ,
5+ Events ,
6+ type User ,
7+ ApplicationCommandOptionType
8+ } from 'discord.js'
9+ import {
10+ type CommandArg ,
11+ CommandArgType ,
12+ type Harmonix ,
13+ type HarmonixCommand ,
14+ type HarmonixEvent ,
15+ type MessageOrInteraction
16+ } from './types'
317import 'dotenv/config'
418import { toJSON } from './commands'
519
@@ -13,48 +27,64 @@ export const initCient = (harmonixOptions: Harmonix['options']) => {
1327
1428export const registerCommands = (
1529 harmonix : Harmonix ,
16- commands : HarmonixCommand < false > [ ]
30+ commands : HarmonixCommand < false , CommandArg [ ] > [ ]
1731) => {
18- harmonix . client ?. on ( Events . MessageCreate , ( message ) => {
32+ harmonix . client ?. on ( Events . MessageCreate , async ( message ) => {
1933 if ( message . author . bot ) return
2034 const prefix = harmonix . options . defaultPrefix
2135 const args = message . content . slice ( prefix . length ) . trim ( ) . split ( / + / )
2236 const command = args . shift ( ) ?. toLowerCase ( )
2337
2438 if ( ! command ) return
2539 const cmd = commands . find ( ( cmd ) => cmd . options . name === command )
26-
27- const params = Object . fromEntries (
28- cmd ?. options . args ?. map ( ( arg , i ) => [ arg . name , args [ i ] ] ) || [ ]
40+ const resolvedArgs = await Promise . all (
41+ ( cmd ?. options . args || [ ] ) . map ( async ( arg , i ) => [
42+ arg . name ,
43+ await resolveArgument ( message , arg . type , args [ i ] )
44+ ] )
2945 )
46+ const fullArgs = Object . fromEntries ( resolvedArgs )
47+
3048 if ( ! cmd || cmd . options . slash ) return
31- cmd . execute ( harmonix . client ! , message , { slash : false , params : params } )
49+ cmd . execute ( harmonix . client ! , message , { slash : false , args : fullArgs } )
3250 } )
3351}
3452
3553export const registerSlashCommands = async (
3654 harmonix : Harmonix ,
37- commands : HarmonixCommand < true > [ ]
55+ commands : HarmonixCommand < true , CommandArg [ ] > [ ]
3856) => {
3957 if ( commands . length === 0 ) return
40- const rest = new REST ( ) . setToken ( process . env . HARMONIX_CLIENT_TOKEN || '' )
58+ const rest = new REST ( ) . setToken ( process . env . HARMONIX_CLIENT_TOKEN ! )
4159
4260 await rest . put (
4361 Routes . applicationCommands (
44- harmonix . options . clientId || process . env . HARMONIX_CLIENT_ID || ''
62+ harmonix . options . clientId || process . env . HARMONIX_CLIENT_ID !
4563 ) ,
4664 { body : commands . map ( ( cmd ) => toJSON ( cmd ) ) }
4765 )
48- harmonix . client ?. on ( 'interactionCreate' , ( interaction ) => {
66+ harmonix . client ?. on ( 'interactionCreate' , async ( interaction ) => {
4967 if ( ! interaction . isChatInputCommand ( ) ) return
5068 const cmd = commands . find (
5169 ( cmd ) => cmd . options . name === interaction . commandName
5270 )
5371
5472 if ( ! cmd || ! cmd . options . slash ) return
73+ const resolvedArgs = await Promise . all (
74+ interaction . options . data . map ( async ( opt ) => [
75+ opt . name ,
76+ await resolveArgument (
77+ interaction ,
78+ commandArgType ( opt . type ) ! ,
79+ String ( opt . value )
80+ )
81+ ] )
82+ )
83+ const fullArgs = Object . fromEntries ( resolvedArgs )
84+
5585 cmd . execute ( harmonix . client ! , interaction , {
5686 slash : true ,
57- params : interaction . options
87+ args : fullArgs
5888 } )
5989 } )
6090}
@@ -68,3 +98,81 @@ export const registerEvents = (harmonix: Harmonix, events: HarmonixEvent[]) => {
6898 }
6999 }
70100}
101+
102+ export const resolveArgument = async (
103+ entity : MessageOrInteraction ,
104+ type : CommandArgType ,
105+ value : string
106+ ) => {
107+ switch ( type ) {
108+ case CommandArgType . String :
109+ return value
110+ case CommandArgType . Integer :
111+ return parseInt ( value )
112+ case CommandArgType . Boolean :
113+ return value === 'true'
114+ case CommandArgType . User :
115+ const user = await resolveUser ( entity , value )
116+
117+ return user
118+ case CommandArgType . Channel :
119+ const channel = await resolveChannel ( entity , value )
120+
121+ return channel
122+ case CommandArgType . Role :
123+ const role = await resolveRole ( entity , value )
124+
125+ return role
126+ case CommandArgType . Number :
127+ return parseFloat ( value )
128+ }
129+ }
130+
131+ const resolveUser = async (
132+ entity : MessageOrInteraction ,
133+ value : string
134+ ) : Promise < User | undefined > => {
135+ return entity . guild ?. members . cache . find (
136+ ( member ) =>
137+ member . user . username === value ||
138+ member . nickname === value ||
139+ member . id === value ||
140+ value == `<@${ member . id } >` ||
141+ value == `<@!${ member . id } >`
142+ ) ?. user
143+ }
144+
145+ const resolveChannel = async ( entity : MessageOrInteraction , value : string ) => {
146+ return entity . guild ?. channels . cache . find (
147+ ( channel ) =>
148+ channel . name === value ||
149+ channel . id === value ||
150+ value == `<#${ channel . id } >`
151+ )
152+ }
153+
154+ const resolveRole = async ( entity : MessageOrInteraction , value : string ) => {
155+ return entity . guild ?. roles . cache . find (
156+ ( role ) =>
157+ role . name === value || role . id === value || value == `<@&${ role . id } >`
158+ )
159+ }
160+
161+ const commandArgType = ( type : ApplicationCommandOptionType ) => {
162+ switch ( type ) {
163+ case ApplicationCommandOptionType . String :
164+ return CommandArgType . String
165+ case ApplicationCommandOptionType . Integer :
166+ return CommandArgType . Integer
167+ case ApplicationCommandOptionType . Boolean :
168+ return CommandArgType . Boolean
169+ case ApplicationCommandOptionType . User :
170+ return CommandArgType . User
171+ case ApplicationCommandOptionType . Channel :
172+ return CommandArgType . Channel
173+ case ApplicationCommandOptionType . Role :
174+ return CommandArgType . Role
175+ case ApplicationCommandOptionType . Number :
176+ return CommandArgType . Number
177+ }
178+ }
0 commit comments