import { awaitResponse } from "../await/await-response" ; import { SingleConnection } from "../client/connection" ; import { MessageError } from "../client/errors" ; import { NoticeMessage } from "../message/twitch-types/notice" ; import { validateChannelName } from "../validation/channel" ; import { sendPrivmsg } from "./privmsg" ; export class WhisperError extends MessageError { public targetUsername : string ; public failedMessage : string ; public constructor ( targetUsername , failedMessage , message ? , cause ? ) { super ( message , cause ) ; this . targetUsername = targetUsername ; this . failedMessage = failedMessage ; } } const badNoticeIDs = [ "whisper_banned" , "whisper_banned_recipient" , "whisper_invalid_args" , "whisper_invalid_login" , "whisper_invalid_self" , "whisper_limit_per_min" , "whisper_limit_per_sec" , "whisper_restricted" , "whisper_restricted_recipient" , ] ; export async function whisper ( conn , username , message ) { validateChannelName ( username ) ; sendPrivmsg ( conn , conn . configuration . username , `/w ${ username } ${ message }` ) ; return awaitResponse ( conn , { failure : ( msg ) => msg instanceof NoticeMessage && msg . channelName === conn . configuration . username && badNoticeIDs . includes ( msg . messageID ! ) , noResponseAction : "success" , timeout : 1000 , errorType : ( msg , cause ) => new WhisperError ( username , message , msg , cause ) , errorMessage : `Failed to whisper [${ username }]: ${ message }` , } ) as Promise < void > ; }