import { awaitResponse } from "../await/await-response" ; import { SingleConnection } from "../client/connection" ; import { MessageError } from "../client/errors" ; import { NoticeMessage } from "../message/twitch-types/notice" ; import { UserstateMessage } from "../message/twitch-types/userstate" ; import { sendPrivmsg } from "./privmsg" ; export function removeCommands ( message ) { if ( message . startsWith ( "." ) || message . startsWith ( "/" ) ) { return `/ ${ message }` ; } else { return message ; } } export class SayError extends MessageError { public failedChannelName : string ; public messageText : string ; public action : boolean ; public constructor ( failedChannelName , failedMessage , action , message ? , cause ? ) { super ( message , cause ) ; this . failedChannelName = failedChannelName ; this . messageText = failedMessage ; this . action = action ; } } const badNoticeIDs = [ "msg_banned" , "msg_bad_characters" , "msg_channel_blocked" , "msg_channel_suspended" , "msg_duplicate" , "msg_emoteonly" , "msg_facebook" , "msg_followersonly" , "msg_followersonly_followed" , "msg_followersonly_zero" , "msg_r9k" , "msg_ratelimit" , "msg_rejected" , "msg_rejected_mandatory" , "msg_room_not_found" , "msg_slowmode" , "msg_subsonly" , "msg_suspended" , "msg_timedout" , "msg_verified_email" , ] ; export async function say ( conn , channelName , messageText , action = false ) { let command ; let errorMessage ; let errorType ; if ( action ) { command = `/me ${ messageText }` ; errorMessage = `Failed to say [#${ channelName }]: /me ${ messageText }` ; errorType = ( msg , cause ) => new SayError ( channelName , messageText , true , msg , cause ) ; } else { command = removeCommands ( messageText ) ; errorMessage = `Failed to say [#${ channelName }]: ${ messageText }` ; errorType = ( msg , cause ) => new SayError ( channelName , messageText , false , msg , cause ) ; } sendPrivmsg ( conn , channelName , command ) ; return awaitResponse ( conn , { success : ( msg ) => msg instanceof UserstateMessage && msg . channelName === channelName , failure : ( msg ) => msg instanceof NoticeMessage && msg . channelName === channelName && badNoticeIDs . includes ( msg . messageID ! ) , errorType , errorMessage , } ) as Promise < UserstateMessage > ; } export async function me ( conn , channelName , message ) { return say ( conn , channelName , message , true ) ; }