@@ -18,6 +18,7 @@ import {
1818} from './types'
1919import 'dotenv/config'
2020import { slashToJSON , contextMenuToJSON } from './commands'
21+ import { createError } from './harmonix'
2122
2223export const initCient = ( harmonixOptions : Harmonix [ 'options' ] ) => {
2324 const client = new Client ( { intents : harmonixOptions . intents } )
@@ -37,7 +38,7 @@ export const refreshApplicationCommands = async (
3738 consola . info ( 'Started refreshing application commands.' )
3839 await rest . put (
3940 Routes . applicationCommands (
40- harmonix . options . clientId || process . env . HARMONIX_CLIENT_ID !
41+ harmonix . options . clientId || process . env . HARMONIX_CLIENT_ID || ''
4142 ) ,
4243 {
4344 body : commands . map ( ( cmd ) =>
@@ -47,7 +48,7 @@ export const refreshApplicationCommands = async (
4748 )
4849 consola . success ( 'Successfully reloaded application commands.' )
4950 } catch {
50- consola . error ( 'Failed to reload application commands.' )
51+ createError ( 'Failed to reload application commands.' )
5152 }
5253}
5354
@@ -83,6 +84,8 @@ export const registerCommands = (
8384
8485 if ( ! command ) return
8586 const cmd = commands . find ( ( cmd ) => cmd . options . name === command )
87+
88+ if ( ! cmd || cmd . options . slash ) return
8689 const resolvedArgs = await Promise . all (
8790 ( cmd ?. options . args || [ ] ) . map ( async ( arg , i ) => [
8891 arg . name ,
@@ -91,7 +94,17 @@ export const registerCommands = (
9194 )
9295 const fullArgs = Object . fromEntries ( resolvedArgs )
9396
94- if ( ! cmd || cmd . options . slash ) return
97+ if ( cmd . options . ownerOnly ) {
98+ if ( ! harmonix . options . ownerId ) {
99+ createError ( 'Owner ID is required for ownerOnly commands.' )
100+ }
101+ if ( ! isOwner ( harmonix , message . author . id ) ) return // TODO: Make possibility to customize ownerOnly message
102+ }
103+ if ( cmd . options . userPermissions ) {
104+ for ( const perm of cmd . options . userPermissions ) {
105+ if ( ! message . member ?. permissions . has ( perm ) ) return // TODO: Make possibility to customize userPermissions message
106+ }
107+ }
95108 cmd . execute ( harmonix . client ! , message , { slash : false , args : fullArgs } )
96109 } )
97110}
@@ -112,15 +125,18 @@ export const registerSlashCommands = (
112125 opt . name ,
113126 await resolveArgument (
114127 interaction ,
115- commandArgType ( opt . type ) ! ,
128+ commandArgType ( opt . type ) ,
116129 String ( opt . value )
117130 )
118131 ] )
119132 )
120133 const fullArgs = Object . fromEntries ( resolvedArgs )
121134
135+ if ( cmd . options . ownerOnly && harmonix . options . ownerId ) {
136+ if ( ! isOwner ( harmonix , interaction . user . id ) ) return // TODO: Make possibility to customize ownerOnly message
137+ }
122138 cmd . execute ( harmonix . client ! , interaction , {
123- slash : true ,
139+ slash : false ,
124140 args : fullArgs
125141 } )
126142 } )
@@ -141,6 +157,20 @@ export const registerContextMenu = (
141157 } )
142158}
143159
160+ const isOwner = ( harmonix : Harmonix , authorId : string ) => {
161+ if (
162+ Array . isArray ( harmonix . options . ownerId ) &&
163+ ! harmonix . options . ownerId . includes ( authorId )
164+ )
165+ return false
166+ if (
167+ typeof harmonix . options . ownerId === 'string' &&
168+ authorId !== harmonix . options . ownerId
169+ )
170+ return false
171+ return true
172+ }
173+
144174const isHarmonixCommand = (
145175 command : HarmonixCommand < true , CommandArg [ ] > | HarmonixContextMenu
146176) : command is HarmonixCommand < true , CommandArg [ ] > => {
@@ -151,7 +181,7 @@ const isHarmonixCommand = (
151181
152182export const resolveArgument = async (
153183 entity : MessageOrInteraction ,
154- type : CommandArgType ,
184+ type : CommandArgType | null ,
155185 value : string
156186) => {
157187 switch ( type ) {
@@ -208,7 +238,7 @@ const resolveRole = async (entity: MessageOrInteraction, value: string) => {
208238 )
209239}
210240
211- const commandArgType = ( type : ApplicationCommandOptionType ) => {
241+ const commandArgType = ( type : ApplicationCommandOptionType | null ) => {
212242 switch ( type ) {
213243 case ApplicationCommandOptionType . String :
214244 return CommandArgType . String
@@ -224,5 +254,7 @@ const commandArgType = (type: ApplicationCommandOptionType) => {
224254 return CommandArgType . Role
225255 case ApplicationCommandOptionType . Number :
226256 return CommandArgType . Number
257+ default :
258+ return null
227259 }
228260}
0 commit comments