import { Message , MessageType } from './message' ; import { Translator } from '../translator' ; import * as program from 'commander' ; const wildcardRegex = /<\*>/g ; const wordRegex = /<WORD>/g ; const numberRegex = /<NUMBER>/g ; const imageRegex = /<IMAGE>/g ; const cardsRegex = /<CARDS>/g ; const regexRegex = /<\((.+?)\)>/g ; export class Response { responseType : MessageType ; private textMatchChecker : RegExp ; original : string ; constructor ( responseData ) { this . original = responseData . trim ( ) ; switch ( this . original ) { case '<IMAGE>' : this . responseType = MessageType . Image ; break ; case '<CARDS>' : this . responseType = MessageType . Card ; break ; default : this . responseType = MessageType . Text ; this . textMatchChecker = new RegExp ( Response . transformTags ( this . original ) ) ; } } matches ( message ) { if ( ! message . messageTypes . includes ( this . responseType ) ) { return false ; } else if ( this . responseType === MessageType . Text ) { return message . text . trim ( ) . match ( this . textMatchChecker ) !== null ; } else { return true ; } } static transformTags ( text ) { regexRegex . lastIndex = 0 ; const taggedText = text . replace ( imageRegex , '' ) . replace ( cardsRegex , '' ) . replace ( wildcardRegex , '<([\\s\\S]*?)>' ) . replace ( wordRegex , '<([^ ]+?)>' ) . replace ( numberRegex , '<([0-9\.,-]+)>' ) ; const regexes = [ ] ; let match ; while ( ( match = regexRegex . exec ( taggedText ) ) != null ) { regexes . push ( `(${ match [ 1 ] })` ) ; } const escapedText = Response . escapeRegex ( taggedText ) ; let outText = escapedText ; regexes . forEach ( ( regexString ) => { const escapedVersion = '<' + Response . escapeRegex ( regexString ) + '>' ; outText = outText . replace ( escapedVersion , regexString ) ; } ) ; return `^${ outText }$` ; } static escapeRegex ( text ) { return text . replace ( /[-\/\\^$*+?.()|[\]{}]/g , '\\$&' ) ; } }