2022-07-31 17:08:00 +05:00
const { InteractionType } = require ( "discord.js" ) ;
2022-08-04 21:02:45 +05:00
const BaseEvent = require ( "../base/BaseEvent" ) ;
2022-07-23 17:14:42 +05:00
class CommandHandler extends BaseEvent {
constructor ( ) {
super ( {
name : "interactionCreate" ,
2022-12-15 21:02:38 +05:00
once : false ,
2022-07-23 17:14:42 +05:00
} ) ;
}
/ * *
*
* @ param { import ( "../base/JaBa" ) } client
* @ param { import ( "discord.js" ) . Interaction } interaction
* /
async execute ( client , interaction ) {
const command = client . commands . get ( interaction . commandName ) ;
const data = [ ] ;
const userData = await client . findOrCreateUser ( {
2022-12-15 21:02:38 +05:00
id : interaction . user . id ,
2022-07-23 17:14:42 +05:00
} ) ;
data . userData = userData ;
if ( interaction . inGuild ( ) ) {
const guildData = await client . findOrCreateGuild ( {
2022-12-15 21:02:38 +05:00
id : interaction . guildId ,
2022-07-23 17:14:42 +05:00
} ) ;
2022-07-26 19:46:32 +05:00
interaction . guild . data = data . guildData = guildData ;
2022-07-23 17:14:42 +05:00
const memberData = await client . findOrCreateMember ( {
id : interaction . member . id ,
2022-12-15 21:02:38 +05:00
guildId : interaction . guildId ,
2022-07-23 17:14:42 +05:00
} ) ;
data . memberData = memberData ;
}
2023-03-24 00:09:26 +05:00
if ( interaction . isAutocomplete ( ) ) {
return await command . autocompleteRun ( client , interaction ) ;
}
if ( interaction . type !== InteractionType . ApplicationCommand && ! interaction . isCommand ( ) ) return ;
2023-03-26 18:31:19 +05:00
if ( command . guildOnly && ! interaction . inGuild ( ) ) return interaction . error ( "misc:GUILD_ONLY" , null , { ephemeral : true } ) ;
if ( command . ownerOnly && interaction . user . id !== client . config . owner . id ) return interaction . error ( "misc:OWNER_ONLY" , null , { ephemeral : true } ) ;
2023-03-24 00:09:26 +05:00
2022-07-23 17:14:42 +05:00
if ( ! userData . achievements . firstCommand . achieved ) {
2023-05-25 22:49:27 +05:00
const args = {
2022-10-14 20:40:37 +05:00
content : interaction . user . toString ( ) ,
2022-07-23 17:14:42 +05:00
files : [ {
name : "achievement_unlocked2.png" ,
2022-12-15 21:02:38 +05:00
attachment : "./assets/img/achievements/achievement_unlocked2.png" ,
} ] ,
2023-05-25 22:49:27 +05:00
} ;
userData . achievements . firstCommand . progress . now = 1 ;
userData . achievements . firstCommand . achieved = true ;
userData . markModified ( "achievements.firstCommand" ) ;
await userData . save ( ) ;
interaction . channel . isDMBased ( ) ? interaction . user . send ( args ) : await interaction . channel . send ( args ) ;
2022-07-23 17:14:42 +05:00
}
2023-06-07 23:59:38 +05:00
client . logger . log ( ` User ${ interaction . user . discriminator === "0" ? interaction . user . username : interaction . user . tag } used ${ command . command . name } in ${ interaction . guild ? interaction . guild . name : "DM" } with arguments: ${ interaction . options . data . length > 0 ? interaction . options . data . map ( arg => { return ` ${ arg . name } : ${ arg . value } ` ; } ).join(", ") : "no args"} ` , "cmd" ) ;
2022-11-16 14:37:16 +05:00
2022-07-23 17:14:42 +05:00
return command . execute ( client , interaction , data ) ;
}
}
module . exports = CommandHandler ;