import { InteractionResponseType , InteractionType , } from 'slash-commands/dist/src/structures' ; import { handlers } from '..' ; import { createOriginal } from './endpoints' ; function neverHappens ( ) { throw new Error ( 'Invalid state, `handle_command.ts`' ) ; } export async function handleCommand ( interaction , event ) { if ( interaction . type !== InteractionType . APPLICATION_COMMAND ) return neverHappens ( ) ; const commandId = interaction . data . id ; if ( commandId in handlers ) { const command = handlers [ commandId ] ; const resp = new Promise ( ( resolve ) => { const context = { ... interaction , context : { originalResp : resolve , haveResponded : false , } , } ; setTimeout ( ( ) => { if ( ! context . context . haveResponded ) { context . context . haveResponded = true ; resolve ( { type : InteractionResponseType . DEFERRED_CHANNEL_MESSAGE_WITH_SOURCE , } ) ; } } , 2500 ) ; event . waitUntil ( ( async ( ) => { const res = await command ( context ) ; if ( res && ! context . context . haveResponded ) { await createOriginal ( context , res ) ; } else if ( res ) { await createOriginal ( context , res ) ; } } ) ( ) ) ; } ) ; return await resp ; } else { return { type : InteractionResponseType . CHANNEL_MESSAGE_WITH_SOURCE , data : { content : 'Command not set up in `slash-worker`.' , } , } ; } }