11import createJiti from 'jiti'
22import { filename } from 'pathe/utils'
3- import { getAllFiles } from './utils '
4- import type { CommandExecute , CommandOptions , CommandResult } from '../types '
3+ import type { CommandExecute , CommandResult } from '../types '
4+ import { dirname } from 'pathe '
55
66const jiti = createJiti ( undefined as unknown as string , {
77 interopDefault : true ,
@@ -10,34 +10,77 @@ const jiti = createJiti(undefined as unknown as string, {
1010 extensions : [ '.ts' , '.js' ]
1111} )
1212
13- const commandsPath = getAllFiles ( './playground/commands' )
14-
15- const parseFileName = ( fileName : string ) => {
16- const parts = fileName . split ( '.' )
17- const name = parts [ 0 ]
18- const hasSlash = parts [ parts . length - 1 ] === 'slash'
13+ interface CommandContext {
14+ name : string
15+ category : string
16+ slash : boolean
17+ execute : CommandExecute < boolean >
18+ }
1919
20- return [ name , hasSlash ]
20+ interface CommandOptions {
21+ name ?: string
22+ description ?: string
23+ category ?: string
24+ arguments ?: any [ ]
25+ nsfw ?: boolean
26+ slash ?: boolean
27+ ownerOnly ?: boolean
28+ guildOnly ?: boolean
29+ userPermissions ?: any [ ]
30+ botPermissions ?: any [ ]
31+ cooldown ?: number
2132}
2233
23- export const loadCommands = ( ) => {
24- const commands = Object . fromEntries (
25- commandsPath . map ( ( path ) => {
26- const [ name , hasSlash ] = parseFileName ( filename ( path ) ) !
34+ export class Command {
35+ public name : string
36+ public description : string
37+ public category : string
38+ public arguments : any [ ]
39+ public nsfw : boolean
40+ public slash : boolean
41+ public ownerOnly : boolean
42+ public guildOnly : boolean
43+ public userPermissions : any [ ]
44+ public botPermissions : any [ ]
45+ public cooldown : number
46+
47+ public execute : CommandExecute < boolean >
48+
49+ constructor ( context : CommandContext , options : CommandOptions = { } ) {
50+ this . name = options . name ?? context . name
51+ this . description = options . description ?? ''
52+ this . category = options . category ?? context . category
53+ this . arguments = options . arguments ?? [ ]
54+ this . nsfw = options . nsfw ?? false
55+ this . slash = options . slash || context . slash
56+ this . ownerOnly = options . ownerOnly ?? false
57+ this . guildOnly = options . guildOnly ?? false
58+ this . userPermissions = options . userPermissions ?? [ ]
59+ this . botPermissions = options . botPermissions ?? [ ]
60+ this . cooldown = options . cooldown ?? 0
2761
28- return [ name , ( ) => ( { slash : hasSlash , ...jiti ( path ) } ) ]
29- } )
30- ) as Record < string , ( ) => { slash : boolean } & CommandResult >
62+ this . execute = context . execute
63+ }
64+ }
65+
66+ export const getCommand = ( path : string ) => {
67+ const command = jiti ( path ) as CommandResult < boolean >
68+ const context : CommandContext = {
69+ name : filename ( path ) . split ( '.' ) [ 0 ] ,
70+ category : filename ( dirname ( path ) ) ,
71+ slash : filename ( path ) . endsWith ( '.slash' ) ,
72+ execute : command . execute
73+ }
3174
32- return commands
75+ return new Command ( context , command . options )
3376}
3477
35- export const defineCommand = (
36- options : CommandOptions ,
37- execute : CommandExecute
38- ) : CommandResult => {
78+ export const defineCommand = < Slash extends boolean > (
79+ options : CommandOptions & { slash ?: Slash } ,
80+ execute : CommandExecute < Slash >
81+ ) : CommandResult < Slash > => {
3982 return {
40- data : options ,
83+ options,
4184 execute
4285 }
4386}
0 commit comments