import { Command } from "./commands/command" ; import { Listener } from "./listeners/listener" ; import { Looker } from "./looker" ; import { LookQueryRunner } from "./repliers/look_query_runner" ; import { QueryRunner } from "./repliers/query_runner" ; import { ReplyContext } from "./reply_context" ; import { Service } from "./services/service" ; export class Commander { private service : Service ; private commands : Command [ ] ; constructor ( service , opts ) { this . service = service ; this . commands = opts . commands . map ( ( c ) => new c ( ) ) ; console . log ( `Starting ${ service . constructor . name }` ) ; this . service . begin ( { listeners : opts . listeners , messageHandler : ( context ) => { return this . handleMessage ( context ) ; } , urlHandler : ( context , url ) => { return this . handleUrlExpansion ( context , url ) ; } , } ) ; } private handleMessage ( context ) { const message = context . sourceMessage ; message . text = message . text . split ( "“" ) . join ( '"' ) ; message . text = message . text . split ( "”" ) . join ( '"' ) ; for ( const command of this . commands ) { if ( command . attempt ( context ) ) { break ; } } context . completeSlashCommand ( ) ; } private handleUrlExpansion ( context , url ) { for ( const looker of Looker . all ) { if ( url . lastIndexOf ( looker . url , 0 ) === 0 ) { context . looker = looker ; this . annotateLook ( context , url ) ; this . annotateShareUrl ( context , url ) ; } } } private annotateLook ( context , url ) { const matches = url . match ( /\/looks\/([0-9]+)$/ ) ; if ( matches ) { console . log ( `Expanding Look URL ${ url }` ) ; const runner = new LookQueryRunner ( context , matches [ 1 ] ) ; runner . start ( ) ; } } private annotateShareUrl ( context , url ) { const matches = url . match ( /\/x\/([A-Za-z0-9]+)$/ ) ; if ( matches ) { console . log ( `Expanding Share URL ${ url }` ) ; const runner = new QueryRunner ( context , { slug : matches [ 1 ] } ) ; runner . start ( ) ; } } }