1- const Discord = require ( "discord.js" ) ,
2- Utils = require ( "../Modules/Utils" ) ;
1+ const Utils = require ( "../Modules/Utils" ) ;
2+ const Discord = require ( "discord.js" ) ;
3+ const chalk = require ( "chalk" ) ;
4+ const ms = require ( "ms" ) ;
35
6+ let db ;
7+ /**
8+ * @param {Discord.Client } bot
9+ * @param {Discord.Interaction } interaction
10+ */
411module . exports = async ( bot , interaction ) => {
12+ if ( ! db ) db = await Utils . database . getDatabase ( ) ;
513 const { config, lang, SlashCmds } = bot ;
614 // Slash Command Executing
715 if ( interaction . isCommand ( ) ) {
816 const command = SlashCmds . find ( ( x ) => x . commandData . SlashCommand . Data . Name . toLowerCase ( ) == interaction . commandName . toLowerCase ( ) ) ;
17+ let permissions = [ ] , ignoreCooldown = false ;
18+
919 if ( command && typeof command . runSlash == "function" ) {
20+ // Command Cooldown Check
21+ if ( command . commandData . Cooldown && ! interaction . user . bot ) {
22+ const ignoreUserCheck = bot . commands . IgnoredCooldown . Users ? bot . commands . IgnoredCooldown . Users . includes ( x => x . id == interaction . user . id
23+ || x . tag . toLowerCase ( ) == interaction . user . tag . toLowerCase ( )
24+ || x . username . toLowerCase ( ) == interaction . user . username . toLowerCase ( ) ) : false ;
25+
26+ const ignoreRoleCheck = bot . commands . IgnoredCooldown . Roles ? bot . commands . IgnoredCooldown . Roles . includes ( x =>
27+ interaction . member . roles . cache . some ( y => y . id == x
28+ || y . name . toLowerCase ( ) == x . toLowerCase ( ) ) ) : false ;
29+
30+ if ( ignoreUserCheck || ignoreRoleCheck ) ignoreCooldown = true ;
31+
32+ const cooldown = ms ( command . commandData . Cooldown ) ;
33+ const isOnCooldown = db . prepare ( `SELECT * FROM cooldowns WHERE user=? AND command=?` ) . get ( interaction . user . id , command . name ) ;
34+
35+
36+ if ( ignoreCooldown == false ) {
37+ if ( isOnCooldown && isOnCooldown . time - Date . now ( ) < 0 ) {
38+ Utils . database . cooldowns . reset ( command . name , interaction . user . id , cooldown )
39+ } else if ( isOnCooldown ) {
40+ return interaction . reply ( Utils . setupMessage ( {
41+ configPath : lang . Presets . OnCooldown ,
42+ variables : [
43+ ...Utils . userVariables ( interaction . member , 'user' ) ,
44+ { searchFor : / { c o o l d o w n } / g, replaceWith : ms ( isOnCooldown . time - Date . now ( ) , { long : true } ) } ,
45+ { searchFor : / { c o m m a n d } / g, replaceWith : command . name }
46+ ]
47+ } ) )
48+ } else if ( ! isOnCooldown ) {
49+ Utils . database . cooldowns . set ( command . name , interaction . user . id , cooldown )
50+ db . prepare ( `INSERT INTO cooldowns (user, command, time)VALUES(?,?,?)` ) . run ( interaction . user . id , command . name , ( Date . now ( ) + cooldown ) ) ;
51+ }
52+ }
53+ }
54+
55+ // Command Channel Check
1056 if ( command . commandData . AllowedChannels ) {
1157 if ( typeof command . commandData . AllowedChannels == "string" )
1258 command . commandData . AllowedChannels = [ command . commandData . AllowedChannels ] ;
@@ -28,18 +74,60 @@ module.exports = async (bot, interaction) => {
2874 ] ,
2975 } , true ) )
3076 }
77+ } ;
78+
79+ // Command Permission Check
80+ if ( command . commandData . Permission ) {
81+ if ( typeof command . commandData . Permission == "string" )
82+ command . commandData . Permission = [ command . commandData . Permission ] ;
83+ else if ( ! command . commandData . Permission [ 0 ] ) command . commandData . Permission = [ "@everyone" ] ;
84+
85+ if ( command . commandData . Permission . includes ( "@everyone" ) || command . commandData . Permission . includes ( "everyone" ) )
86+ permissions . push ( true ) ;
87+ else command . commandData . Permission . forEach ( permission => {
88+ const roleExists = Utils . findRole ( permission , interaction . guild , false ) ;
89+ const userExists = Utils . parseUser ( permission , interaction . guild ) ;
90+
91+ if ( ! roleExists && ! userExists )
92+ Utils . logWarning ( `${ chalk . bold ( permission ) } is not a valid ${ chalk . bold ( 'role/user' ) } permission in command ${ chalk . bold ( command . name ) } ` )
93+
94+ if ( userExists && userExists . id === interaction . user . id ) permissions . push ( true ) ;
95+ else if ( Utils . hasRole ( interaction . member , permission , false ) ) permissions . push ( true ) ;
96+ } )
97+ } ;
98+
99+ if ( permissions . includes ( true ) ) {
100+ let options = interaction . options && interaction . options . _hoistedOptions ? Utils . parseSlashArgs ( interaction . options . _hoistedOptions ) : { } ;
101+ await command . runSlash ( bot , interaction , options , {
102+ commandUsed : interaction . commandName ,
103+ commandData : command
104+ } ) ;
105+ } else {
106+ interaction . reply ( Utils . setupMessage ( {
107+ configPath : lang . Presets . NoPermission ,
108+ variables : [
109+ ...Utils . userVariables ( interaction . member ) ,
110+ {
111+ searchFor : / { p e r m s } / g, replaceWith : permissions [ 0 ] ? command . commandData . Permission . map ( ( x ) => {
112+ if ( ! ! Utils . findRole ( x , interaction . guild , false ) ) {
113+ let role = Utils . findRole ( x , interaction . guild , true ) ;
114+ if ( role ) return Utils . builder . roleMention ( role . id ) ;
115+ }
116+ if ( ! ! Utils . parseUser ( x , interaction . guild ) ) {
117+ let user = Utils . parseUser ( x , interaction . guild , true ) ;
118+ if ( user ) return Utils . builder . userMention ( user . id ) ;
119+ }
120+ } ) . join ( ", " ) : "Invalid Permissions configured." ,
121+ } ,
122+ ] ,
123+ } ) ) ;
31124 }
32- let options = interaction . options && interaction . options . _hoistedOptions ? Utils . parseSlashArgs ( interaction . options . _hoistedOptions ) : { } ;
33- let commandUsed = interaction . commandName , commandData = command ;
34- await command . runSlash ( bot , interaction , options , { commandUsed, commandData } ) ;
125+
35126 } else {
36127 let cmd = interaction . guild . commands . cache . find ( ( x ) =>
37128 x . name . toLowerCase ( ) == interaction . commandName . toLowerCase ( ) ) ;
38129 if ( cmd ) cmd . delete ( ) ;
39- interaction . reply ( {
40- content : "This command no longer exists." ,
41- ephemeral : true ,
42- } ) ;
130+ interaction . reply ( { content : "This command no longer exists." , ephemeral : true } ) ;
43131 }
44132 } else if ( interaction . isButton ( ) ) {
45133 bot . emit ( "interactionCreate-Button" , interaction ) ;
0 commit comments