import { CommandInteraction , NewsChannel , TextChannel , ThreadChannel } from 'discord.js' ; import { RateLimiter } from 'discord.js-rate-limiter' ; import { EventHandler } from '.' ; import { Command , CommandDeferType } from '../commands' ; import { EventData } from '../models/internal-models' ; import { Lang , Logger } from '../services' ; import { CommandUtils , MessageUtils } from '../utils' ; let Config = require ( '../../config/config.json' ) ; let Logs = require ( '../../lang/logs.json' ) ; export class CommandHandler implements EventHandler { private rateLimiter = new RateLimiter ( Config . rateLimiting . commands . amount , Config . rateLimiting . commands . interval * 1000 ) ; constructor ( public commands ) { } public async process ( intr ) { if ( intr . user . id === intr . client . user ?. id || intr . user . bot ) { return ; } let limited = this . rateLimiter . take ( intr . user . id ) ; if ( limited ) { return ; } let command = this . commands . find ( command => command . metadata . name === intr . commandName ) ; if ( ! command ) { Logger . error ( Logs . error . commandNotFound . replaceAll ( '{INTERACTION_ID}' , intr . id ) . replaceAll ( '{COMMAND_NAME}' , intr . commandName ) ) ; return ; } switch ( command . deferType ) { case CommandDeferType . PUBLIC : { await MessageUtils . deferReply ( intr , false ) ; break ; } case CommandDeferType . HIDDEN : { await MessageUtils . deferReply ( intr , true ) ; break ; } } let data = new EventData ( ) ; try { let passesChecks = await CommandUtils . runChecks ( command , intr , data ) ; if ( passesChecks ) { await command . execute ( intr , data ) ; } } catch ( error ) { await this . sendError ( intr , data ) ; Logger . error ( intr . channel instanceof TextChannel || intr . channel instanceof NewsChannel || intr . channel instanceof ThreadChannel ? Logs . error . commandGuild . replaceAll ( '{INTERACTION_ID}' , intr . id ) . replaceAll ( '{COMMAND_NAME}' , command . metadata . name ) . replaceAll ( '{USER_TAG}' , intr . user . tag ) . replaceAll ( '{USER_ID}' , intr . user . id ) . replaceAll ( '{CHANNEL_NAME}' , intr . channel . name ) . replaceAll ( '{CHANNEL_ID}' , intr . channel . id ) . replaceAll ( '{GUILD_NAME}' , intr . guild ?. name ) . replaceAll ( '{GUILD_ID}' , intr . guild ?. id ) : Logs . error . commandOther . replaceAll ( '{INTERACTION_ID}' , intr . id ) . replaceAll ( '{COMMAND_NAME}' , command . metadata . name ) . replaceAll ( '{USER_TAG}' , intr . user . tag ) . replaceAll ( '{USER_ID}' , intr . user . id ) , error ) ; } } private async sendError ( intr , data ) { try { await MessageUtils . sendIntr ( intr , Lang . getEmbed ( 'errorEmbeds.command' , data . lang ( ) , { ERROR_CODE : intr . id , } ) ) ; } catch { } } }