@@ -6,6 +6,8 @@ import { Commands } from '../managers/CommandManager';
6
6
import { Locale , LocaleString } from '../util/common' ;
7
7
import { Logger } from '../util/logger/Logger' ;
8
8
import { commandAndOptionNameRegexp } from '../util/regexes' ;
9
+ import { PermissionResolvable , Permissions } from 'discord.js' ;
10
+ import { Util } from '../util/Util' ;
9
11
10
12
export enum CommandType {
11
13
/**
@@ -26,17 +28,19 @@ export enum CommandType {
26
28
'CONTEXT_MESSAGE' = 3 ,
27
29
}
28
30
29
- export type CommandInhibitor = ( ctx : CommandContext ) => boolean | any ;
30
- export type CommandInhibitors = Array <
31
- { run : CommandInhibitor } | CommandInhibitor
32
- > ;
31
+ export type CommandInhibitor =
32
+ | ( ( ctx : CommandContext ) => boolean | any )
33
+ | { run : CommandInhibitor } ;
34
+ export type CommandInhibitors = Array < CommandInhibitor > ;
33
35
34
36
export interface CommandOptions {
35
37
name : string ;
36
38
nameLocalizations ?: Record < LocaleString , string > ;
37
39
description ?: string ;
38
40
descriptionLocalizations ?: Record < LocaleString , string > ;
39
41
type : Array < CommandType | keyof typeof CommandType > ;
42
+ defaultMemberPermissions ?: PermissionResolvable ;
43
+ dmPermission ?: boolean ;
40
44
arguments ?: Array < Argument | ArgumentOptions > ;
41
45
inhibitors ?: CommandInhibitors ;
42
46
guildId ?: string ;
@@ -84,8 +88,10 @@ const validationSchema = z
84
88
)
85
89
. array ( )
86
90
. nonempty ( ) ,
91
+ defaultMemberPermissions : z . any ( ) . optional ( ) ,
92
+ dmPermission : z . boolean ( ) . optional ( ) ,
87
93
arguments : z . any ( ) . array ( ) . optional ( ) ,
88
- inhibitors : z . any ( ) . array ( ) . optional ( ) ,
94
+ inhibitors : z . any ( ) . array ( ) . optional ( ) . default ( [ ] ) ,
89
95
guildId : z . string ( ) . optional ( ) ,
90
96
cooldown : z . string ( ) . optional ( ) ,
91
97
autoDefer : z
@@ -109,6 +115,8 @@ export class Command {
109
115
public description ?: string ;
110
116
public descriptionLocalizations ?: Record < LocaleString , string > ;
111
117
public type : Array < CommandType | keyof typeof CommandType > ;
118
+ public defaultMemberPermissions ?: PermissionResolvable ;
119
+ public dmPermission ?: boolean ;
112
120
public arguments ?: Array < Argument > ;
113
121
public inhibitors : CommandInhibitors ;
114
122
public guildId ?: string ;
@@ -136,6 +144,11 @@ export class Command {
136
144
options . descriptionLocalizations ||
137
145
Command . defaults ?. descriptionLocalizations ;
138
146
this . type = options . type || Command . defaults ?. type ;
147
+ this . defaultMemberPermissions =
148
+ options . defaultMemberPermissions ||
149
+ Command . defaults ?. defaultMemberPermissions ;
150
+ this . dmPermission =
151
+ options . dmPermission || Command . defaults ?. dmPermission ;
139
152
this . arguments = options . arguments ?. map ( argument => {
140
153
if ( argument instanceof Argument ) return argument ;
141
154
else return new Argument ( argument ) ;
@@ -174,26 +187,9 @@ export class Command {
174
187
if ( ! this . inhibitors ) return true ;
175
188
176
189
for await ( const inhibitor of this . inhibitors ) {
177
- let result ;
178
- if ( typeof inhibitor === 'function' ) {
179
- result = await Promise . resolve ( inhibitor ( ctx ) ) . catch ( error => {
180
- Logger . error (
181
- typeof error . code !== 'undefined' ? error . code : '' ,
182
- error . message ,
183
- ) ;
184
- if ( error . stack ) Logger . trace ( error . stack ) ;
185
- } ) ;
186
- } else if ( typeof inhibitor . run === 'function' ) {
187
- result = await Promise . resolve ( inhibitor . run ( ctx ) ) . catch ( error => {
188
- Logger . error (
189
- typeof error . code !== 'undefined' ? error . code : '' ,
190
- error . message ,
191
- ) ;
192
- if ( error . stack ) Logger . trace ( error . stack ) ;
193
- } ) ;
194
- }
195
- if ( result !== true ) return false ;
190
+ if ( ( await Util . runInhibitor ( ctx , inhibitor ) ) !== true ) return false ;
196
191
}
192
+
197
193
return true ;
198
194
}
199
195
@@ -218,6 +214,12 @@ export class Command {
218
214
name_localizations : this . nameLocalizations ,
219
215
description : this . description ,
220
216
description_localizations : this . descriptionLocalizations ,
217
+ dm_permission : this . dmPermission ,
218
+ default_member_permissions : this . defaultMemberPermissions
219
+ ? new Permissions (
220
+ this . defaultMemberPermissions ,
221
+ ) . bitfield . toString ( )
222
+ : null ,
221
223
options : this . arguments ?. map ( argument => argument . toJSON ( ) ) ,
222
224
type : type ,
223
225
} ;
0 commit comments