import type { Gatekeeper , InteractionContext , ReplyHandle , } from "@itsmapleleaf/gatekeeper" ; import { buttonComponent } from "@itsmapleleaf/gatekeeper" ; import { wait } from "../wait" ; function createCounterReply ( context ) { let count = 0 ; const reply = context . reply ( ( ) => { return [ buttonComponent ( { label : `increment (${ count })` , style : "PRIMARY" , onClick : ( ) => { count ++ ; } , } ) , buttonComponent ( { label : "done" , style : "SECONDARY" , onClick : ( ) => { reply . delete ( ) ; } , } ) , ] ; } ) ; return reply ; } export default function defineCommands ( gatekeeper ) { gatekeeper . addSlashCommand ( { name : "counter-factory" , description : "a counter on sterroids" , run ( context ) { const replies = [ ] ; let state = "active" ; const reply = context . reply ( ( ) => { const cleanup = async ( ) => { state = "cleaningUp" ; reply . refresh ( ) ; for ( const counterReply of replies ) { counterReply . delete ( ) ; } await wait ( 1000 ) ; state = "done" ; reply . refresh ( ) ; await wait ( 1000 ) ; reply . delete ( ) ; } ; if ( state === "cleaningUp" ) { return [ "cleaning up..." ] ; } if ( state === "done" ) { return [ "done" ] ; } return [ buttonComponent ( { label : "create counter" , style : "PRIMARY" , onClick : ( ) => { replies . push ( createCounterReply ( context ) ) ; } , } ) , buttonComponent ( { label : "clean up" , style : "SECONDARY" , onClick : cleanup , } ) , ] ; } ) ; } , } ) ; }