import camelCase from 'lodash/camelCase' ; import gt from 'lodash/gt' ; import toLower from 'lodash/toLower' ; import { BaseTags , ClearChatTags , GlobalUserStateTags , ChatEvents , Commands , RoomStateTags , UserStateTags , MessageParameters , } from '../../../twitch' ; import * as constants from '../../constants' ; import * as helpers from './helpers' ; export const clearChat = ( tags ) : ClearChatTags => ( { ... tags , banReason : helpers . generalString ( tags . banReason ) , banDuration : helpers . generalNumber ( tags . banDuration ) , } ) ; export const privateMessageCheerEvent = ( tags ) => { return gt ( tags . bits , 0 ) ? { event : ChatEvents . CHEER , bits : parseInt ( tags . bits , 10 ) } : { event : Commands . PRIVATE_MESSAGE } ; } ; export const roomState = ( roomStateTags ) : RoomStateTags => Object . entries ( roomStateTags ) . reduce ( ( tags , [ tag , value ] ) => { switch ( tag ) { case 'followersOnly' : return { ... tags , [ tag ] : helpers . followersOnly ( value ) } ; case 'broadcasterLang' : return { ... tags , [ tag ] : helpers . generalString ( value ) } ; case 'slow' : return { ... tags , [ tag ] : helpers . generalNumber ( value ) } ; case 'emoteOnly' : case 'r9k' : case 'subsOnly' : return { ... tags , [ tag ] : helpers . generalBoolean ( value ) } ; default : return { ... tags , [ tag ] : value } ; } } , { } ) ; export const userNoticeMessageParameters = ( tags ) => Object . entries ( tags ) . reduce ( ( parameters , [ tag , value ] ) => { const [ , param ] = constants . MESSAGE_PARAMETER_PREFIX_RE . exec ( tag ) || [ ] ; switch ( param ) { case 'Months' : case 'MassGiftCount' : case 'PromoGiftTotal' : case 'SenderCount' : case 'ViewerCount' : return { ... parameters , [ camelCase ( param ) ] : helpers . generalNumber ( value ) , } ; case undefined : return parameters ; default : return { ... parameters , [ camelCase ( param ) ] : helpers . generalString ( value ) , } ; } } , { } as MessageParameters ) ; export const userState = ( tags ) : UserStateTags => ( { ... tags , badges : helpers . badges ( tags . badges ) , bits : helpers . generalNumber ( tags . bits ) , color : tags . color , displayName : tags . displayName , emotes : helpers . emotes ( tags . emotes ) , emoteSets : helpers . emoteSets ( tags . emoteSets ) , userType : helpers . userType ( tags . userType ) , username : tags . displayName ? toLower ( tags . displayName ) : tags . username , isModerator : tags . mod === '1' , } ) ; export const globalUserState = ( tags ) : GlobalUserStateTags => ( { ... tags , ... userState ( tags ) , } ) ; export const privateMessage = userState ; export const userNotice = userState ;