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' ;
43
5- // TYPES \\
64type RemoveMethods < T > = T extends { } ? {
75 [ P in keyof T as T [ P ] extends ( ...args : any [ ] ) => unknown ? never : P ] : RemoveMethods < T [ P ] > ;
86} : T ;
97
108type ConditionalMethod = ( args : {
119 command : Command ,
12- interaction : RemoveMethods < Discord . ChatInputCommandInteraction > ,
13- message : RemoveMethods < Discord . Message >
14- } ) => unknown
10+ interaction : RemoveMethods < ChatInputCommandInteraction > ,
11+ message : RemoveMethods < Message > ;
12+ } ) => unknown ;
1513
16- interface Conditional {
14+ interface Conditional {
1715 condition : ConditionalMethod | unknown ;
1816 onFalse ?: string ;
1917}
2018
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 } ) ;
4330 }
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+ } ;
4544}
4645
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+ }
5250
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