1
- // IMPORTS \\
2
- import { CommandPlugin , CommandType , Command } from "@spark.ts/handler" ;
3
- import * as Discord from "discord.js" ;
1
+ import { CommandPlugin , CommandType , Command } from '@spark.ts/handler' ;
2
+ import { ChatInputCommandInteraction , Message } from 'discord.js' ;
4
3
5
- // TYPES \\
6
4
type RemoveMethods < T > = T extends { } ? {
7
5
[ P in keyof T as T [ P ] extends ( ...args : any [ ] ) => unknown ? never : P ] : RemoveMethods < T [ P ] > ;
8
6
} : T ;
9
7
10
8
type ConditionalMethod = ( args : {
11
9
command : Command ,
12
- interaction : RemoveMethods < Discord . ChatInputCommandInteraction > ,
13
- message : RemoveMethods < Discord . Message >
14
- } ) => unknown
10
+ interaction : RemoveMethods < ChatInputCommandInteraction > ,
11
+ message : RemoveMethods < Message > ;
12
+ } ) => unknown ;
15
13
16
- interface Conditional {
14
+ interface Conditional {
17
15
condition : ConditionalMethod | unknown ;
18
16
onFalse ?: string ;
19
17
}
20
18
21
- // EXPORTS \\
22
- export function Guard ( ...conditionals : Conditional [ ] ) : CommandPlugin < CommandType . Both > {
23
- return {
24
- name : "guard" ,
25
- description : "Checks through a list of defined conditions before running a command." ,
26
- run ( { command, controller, message, interaction} ) {
27
- for ( const conditional of conditionals ) {
28
- let condition = conditional . condition ;
29
- if ( typeof conditional . condition === "function" ) {
30
- condition = conditional . condition ( { command, message, interaction} ) ;
31
- }
32
-
33
- if ( ! condition ) {
34
- if ( "onFalse" in conditional ) {
35
- if ( command . type === CommandType . Slash ) interaction ?. reply ( conditional . onFalse ! ) ;
36
- else if ( command . type === CommandType . Text ) message ?. reply ( conditional . onFalse ! ) ;
37
- }
38
- return controller . stop ( ) ;
39
- }
40
- }
41
-
42
- return controller . next ( ) ;
19
+ export function Guard ( ...conditionals : Conditional [ ] ) : CommandPlugin < CommandType . Both > {
20
+ return {
21
+ name : 'guard' ,
22
+ description : 'Checks through a list of defined conditions before running a command.' ,
23
+ run ( {
24
+ command, controller, message, interaction,
25
+ } ) {
26
+ for ( const conditional of conditionals ) {
27
+ let { condition } = conditional ;
28
+ if ( typeof conditional . condition === 'function' ) {
29
+ condition = conditional . condition ( { command, message, interaction } ) ;
43
30
}
44
- }
31
+
32
+ if ( ! condition ) {
33
+ if ( 'onFalse' in conditional ) {
34
+ if ( command . type === CommandType . Slash ) interaction ?. reply ( conditional . onFalse ! ) ;
35
+ else if ( command . type === CommandType . Text ) message ?. reply ( conditional . onFalse ! ) ;
36
+ }
37
+ return controller . stop ( ) ;
38
+ }
39
+ }
40
+
41
+ return controller . next ( ) ;
42
+ } ,
43
+ } ;
45
44
}
46
45
47
- // HELPERS \\
48
- export abstract class Helpers {
49
- static InGuild ( guilds : string [ ] ) : ConditionalMethod {
50
- return ( { command, message, interaction} ) => guilds . includes ( ( command . type === "slash" ? interaction : message ) . guildId ! ) ;
51
- }
46
+ export abstract class Helpers {
47
+ static InGuild ( guilds : string [ ] ) : ConditionalMethod {
48
+ return ( { command, message, interaction } ) => guilds . includes ( ( command . type === 'slash' ? interaction : message ) . guildId ! ) ;
49
+ }
52
50
53
- static InChannel ( channels : string [ ] ) : ConditionalMethod {
54
- return ( { command, message, interaction} ) => channels . includes ( ( command . type === " slash" ? interaction : message ) . channelId ! ) ;
55
- }
56
- }
51
+ static InChannel ( channels : string [ ] ) : ConditionalMethod {
52
+ return ( { command, message, interaction } ) => channels . includes ( ( command . type === ' slash' ? interaction : message ) . channelId ! ) ;
53
+ }
54
+ }
0 commit comments