import { ChatClient , ChatUser } from "twitch-chat-client/lib" ; import signale from "signale" ; import moment from "moment" ; import { Identification } from "../interfaces/identification.interface" ; import { Channel } from "../interfaces/channel.interface" ; import { Song } from "../interfaces/song.interface" ; export default class MessageComposer { param channel param user param song public SUCCESS = ( channel , user , song ) : string => { const template = channel . templates . find ( ( template ) => template . type === "SUCCESS" ) ?. template || null ; if ( ! template ) { signale . error ( `Could not find Template of type \`SUCCESS\`  for Channel \`${ channel }\`` ) ; throw new Error ( `Could not find Template of type \`SUCCESS\`  for Channel \`${ channel }\`` ) ; } return template . replace ( "%REQUESTER%" , user . userName ) . replace ( "%TITLE%" , song . title ) . replace ( "%ARTIST%" , song . artists as string ) . replace ( "%TIMECODE%" , song . timecode ) . concat ( channel . links && song . url ? ` → ${ song . url }` : "" ) ; } ; param channel param user param remaining param identification public COOLDOWN = ( channel , user , remaining , identification ? ) : string => { const song = identification ?. songs [ 0 ] ; const template = song ? channel . templates . find ( ( { type } ) => type === "COOLDOWN_WITH_ID" ) ?. template || null : channel . templates . find ( ( { type } ) => type === "COOLDOWN" ) ?. template || null ; if ( ! template ) { signale . error ( `Could not find Template of type \`${ song ? "COOLDOWN_WITH_ID" : "COOLDOWN" }\` for Channel \`${ channel }\`` ) ; throw new Error ( `Could not find Template of type \`${ song ? "COOLDOWN_WITH_ID" : "COOLDOWN" }\` for Channel \`${ channel }\`` ) ; } return song ? template . replace ( "%REQUESTER%" , user . userName ) . replace ( "%TITLE%" , song . title ) . replace ( "%ARTIST%" , song . artists as string ) . replace ( "%TIME%" , moment ( ( identification as Identification ) . date ) . fromNow ( ) ) . concat ( channel . links && song . url ? ` → ${ song . url }` : "" ) : template . replace ( "%REQUESTER%" , user . userName ) . replace ( "%REMAINING%" , remaining . toString ( ) ) ; } ; param channel param user param error public ERROR = ( channel , user , errorMessage ? ) : string => { const template = channel . templates . find ( ( template ) => template . type === "ERROR" ) ?. template || null ; if ( ! template ) { signale . error ( `Could not find Template of type \`ERROR\`  for Channel \`${ channel }\`` ) ; throw new Error ( `Could not find Template of type \`ERROR\`  for Channel \`${ channel }\`` ) ; } return template . replace ( "%REQUESTER%" , user . userName ) . replace ( "%ERROR%" , errorMessage || "<unknown error>" ) ; } ; param channel param user param message param client public send = ( channel , user , message , client ) : Promise < void > => channel . actions ? client . action ( channel . name , message ) : client . say ( channel . name , message ) ; }