1
1
import type { ApplicationCommandData , ApplicationCommandPermissionData , ChatInputApplicationCommandData , MessageApplicationCommandData , UserApplicationCommandData } from 'discord.js'
2
2
import type { Command } from '@sapphire/framework'
3
+ import { container } from '@sapphire/framework'
3
4
4
5
const types = [ 'chatInput' , 'message' , 'user' ] as const
5
6
@@ -10,11 +11,22 @@ export class ApplicationCommand {
10
11
public messageApplicationIdHints : Set < string > | null = null
11
12
public userApplicationIdHints : Set < string > | null = null
12
13
13
- public constructor ( options : ApplicationCommandOptions ) {
14
+ private constructor ( options : ApplicationCommandOptions ) {
14
15
this . command = options . command
15
16
17
+ if ( this . command . options . chatInputApplicationOptions ) {
18
+ this . command . options . chatInputApplicationOptions . description ??= this . command . description
19
+ }
20
+
16
21
for ( const type of types ) {
17
- const idHints = this . command . options [ `${ type } ApplicationOptions` ] ?. idHints
22
+ // Set some defaults
23
+ const options = this . command . options [ `${ type } ApplicationOptions` ]
24
+ if ( ! options ) continue
25
+
26
+ options . name ??= this . command . name
27
+
28
+ // Store id hints
29
+ const { idHints } = options
18
30
if ( idHints && idHints . length > 0 ) {
19
31
const set = new Set < string > ( )
20
32
idHints . forEach ( id => set . add ( id ) )
@@ -23,6 +35,33 @@ export class ApplicationCommand {
23
35
}
24
36
}
25
37
38
+ public static async create ( options : ApplicationCommandOptions ) : Promise < ApplicationCommand > {
39
+ const instance = new ApplicationCommand ( options )
40
+ await instance . fillHints ( )
41
+ return instance
42
+ }
43
+
44
+ protected async fillHints ( ) : Promise < void > {
45
+ const clientOptions = container . client . options
46
+ const hintProvider = clientOptions . applicationCommandsHintProvider
47
+ if ( ! hintProvider ) return
48
+
49
+ for ( const type of types ) {
50
+ const hints = await hintProvider ( this . command . name , type )
51
+ if ( ! hints ) continue
52
+ const options = this . command . options [ `${ type } ApplicationOptions` ]
53
+ if ( ! options ) continue
54
+ if ( hints . guildIds ) {
55
+ options . guildIds ??= [ ]
56
+ options . guildIds . push ( ...hints . guildIds )
57
+ }
58
+ if ( hints . idHints ) {
59
+ options . idHints ??= [ ]
60
+ options . idHints . push ( ...hints . idHints )
61
+ }
62
+ }
63
+ }
64
+
26
65
public get allGuilds ( ) : string [ ] | null {
27
66
const chatInputHints = this . chatInputApplicationGuilds
28
67
const messageHints = this . messageApplicationGuilds
@@ -68,6 +107,15 @@ export class ApplicationCommand {
68
107
public get chatInputApplicationData ( ) : ChatInputApplicationCommandData | null {
69
108
const options = this . command . options . chatInputApplicationOptions
70
109
if ( ! options ) return null
110
+ if ( ! options . description ) {
111
+ container . logger . error ( 'Missing required property "description"' )
112
+ return null
113
+ }
114
+ if ( ! options . name ) {
115
+ container . logger . error ( 'Missing required property "name"' )
116
+ return null
117
+ }
118
+
71
119
return {
72
120
defaultPermission : options . defaultPermission ?? true ,
73
121
description : options . description ,
@@ -80,6 +128,10 @@ export class ApplicationCommand {
80
128
public get messageApplicationData ( ) : MessageApplicationCommandData | null {
81
129
const options = this . command . options . messageApplicationOptions
82
130
if ( ! options ) return null
131
+ if ( ! options . name ) {
132
+ container . logger . error ( 'Missing required property "name"' )
133
+ return null
134
+ }
83
135
return {
84
136
defaultPermission : options . defaultPermission ?? true ,
85
137
name : options . name ,
@@ -90,6 +142,10 @@ export class ApplicationCommand {
90
142
public get userApplicationData ( ) : UserApplicationCommandData | null {
91
143
const options = this . command . options . userApplicationOptions
92
144
if ( ! options ) return null
145
+ if ( ! options . name ) {
146
+ container . logger . error ( 'Missing required property "name"' )
147
+ return null
148
+ }
93
149
return {
94
150
defaultPermission : options . defaultPermission ?? true ,
95
151
name : options . name ,
0 commit comments