|
1 | | -import createJiti from 'jiti' |
2 | | -import { filename } from 'pathe/utils' |
3 | | -import type { CommandExecute, CommandResult } from '../types' |
| 1 | +import jiti from 'jiti' |
4 | 2 | import { dirname } from 'pathe' |
5 | | - |
6 | | -const jiti = createJiti(undefined as unknown as string, { |
7 | | - interopDefault: true, |
8 | | - requireCache: false, |
9 | | - esmResolve: true, |
10 | | - extensions: ['.ts', '.js'] |
11 | | -}) |
12 | | - |
13 | | -interface CommandContext { |
14 | | - name: string |
15 | | - category: string |
16 | | - slash: boolean |
17 | | - execute: CommandExecute<boolean> |
18 | | -} |
19 | | - |
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 |
32 | | -} |
33 | | - |
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 |
61 | | - |
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 |
| 3 | +import { filename } from 'pathe/utils' |
| 4 | +import type { |
| 5 | + CommandExecute, |
| 6 | + CommandOptions, |
| 7 | + HarmonyCommand, |
| 8 | + HarmonyCommandInput |
| 9 | +} from '../types' |
| 10 | +import { Harmony } from './harmony' |
| 11 | + |
| 12 | +export const resolveHarmonyCommand = ( |
| 13 | + cmd: HarmonyCommandInput, |
| 14 | + harmonyOptions: Harmony['options'] |
| 15 | +): HarmonyCommand<boolean> => { |
| 16 | + if (typeof cmd === 'string') { |
| 17 | + const _jiti = jiti(harmonyOptions.rootDir, { |
| 18 | + interopDefault: true |
| 19 | + }) |
| 20 | + const _cmdPath = _jiti.resolve(cmd) |
| 21 | + const command = _jiti(_cmdPath) as HarmonyCommand<boolean> |
| 22 | + const options: CommandOptions = { |
| 23 | + name: filename(_cmdPath).split('.')[0], |
| 24 | + category: filename(dirname(_cmdPath)), |
| 25 | + slash: command.options.slash || filename(_cmdPath).endsWith('.slash'), |
| 26 | + ...command.options |
| 27 | + } |
| 28 | + |
| 29 | + return { options, execute: command.execute } |
| 30 | + } else { |
| 31 | + return cmd |
73 | 32 | } |
74 | | - |
75 | | - return new Command(context, command.options) |
76 | 33 | } |
77 | 34 |
|
78 | 35 | export const defineCommand = <Slash extends boolean>( |
79 | 36 | options: CommandOptions & { slash?: Slash }, |
80 | 37 | execute: CommandExecute<Slash> |
81 | | -): CommandResult<Slash> => { |
| 38 | +): HarmonyCommand<Slash> => { |
82 | 39 | return { |
83 | 40 | options, |
84 | 41 | execute |
|
0 commit comments