Options
All
  • Public
  • Public/Protected
  • All
Menu

Class Chat

Interact with Twitch chat.

Connecting

const token = 'cfabdegwdoklmawdzdo98xt2fo512y'
const username = 'ronni'
const { chat } = new TwitchJs({ token, username })

chat.connect().then(globalUserState => {
  // Do stuff ...
})

Note: Connecting with a token and a username is optional.

Once connected, chat.userState will contain global user state information.

Joining a channel

const channel = '#dallas'

chat.join(channel).then(channelState => {
  // Do stuff with channelState...
})

After joining a channel, chat.channels[channel] will contain channel state information.

Listening for events

// Listen to all events
chat.on('*', message => {
  // Do stuff with message ...
})

// Listen to private messages
chat.on('PRIVMSG', privateMessage => {
  // Do stuff with privateMessage ...
})

Events are nested; for example:

// Listen to subscriptions only
chat.on('USERNOTICE/SUBSCRIPTION', userStateMessage => {
  // Do stuff with userStateMessage ...
})

// Listen to all user notices
chat.on('USERNOTICE', userStateMessage => {
  // Do stuff with userStateMessage ...
})

For added convenience, TwitchJS also exposes event constants.

const { chat } = new TwitchJs({ token, username })

// Listen to all user notices
chat.on(chat.events.USER_NOTICE, userStateMessage => {
  // Do stuff with userStateMessage ...
})

Sending messages

To send messages, [Chat] must be initialized with a username and a token with chat_login scope.

All messages sent to Twitch are automatically rate-limited according to Twitch Developer documentation.

Speak in channel

const channel = '#dallas'

chat
  .say(channel, 'Kappa Keepo Kappa')
  // Optionally ...
  .then(() => {
    // ... do stuff on success ...
  })

Send command to channel

All chat commands are currently supported and exposed as camel-case methods. For example:

const channel = '#dallas'

// Enable followers-only for 1 week
chat.followersOnly(channel, '1w')

// Ban ronni
chat.ban(channel, 'ronni')

Note: Promise-resolves for each commands are planned.

Joining multiple channels

const channels = ['#dallas', '#ronni']

Promise.all(channels.map(channel => chat.join(channel))).then(channelStates => {
  // Listen to all messages from #dallas only
  chat.on('#dallas', message => {
    // Do stuff with message ...
  })

  // Listen to private messages from #dallas and #ronni
  chat.on('PRIVMSG', privateMessage => {
    // Do stuff with privateMessage ...
  })

  // Listen to private messages from #dallas only
  chat.on('PRIVMSG/#dallas', privateMessage => {
    // Do stuff with privateMessage ...
  })

  // Listen to all private messages from #ronni only
  chat.on('PRIVMSG/#ronni', privateMessage => {
    // Do stuff with privateMessage ...
  })
})

Broadcasting to all channels

chat
  .broadcast('Kappa Keepo Kappa')
  // Optionally ...
  .then(userStateMessages => {
    // ... do stuff with userStateMessages on success ...
  })

Hierarchy

Index

Type aliases

Static ArgumentMap

ArgumentMap<T>: {}

Type parameters

  • T: object

Type declaration

Static EventArgs

EventArgs<T, K>: Parameters<EventListener<T, K>>

Type parameters

Static EventListener

EventListener<T, K>: T extends string | symbol ? (...args: any[]) => void : (...args: ArgumentMap<Exclude<T, string | symbol>>[Extract<K, keyof T>]) => void

Type parameters

Static EventNames

EventNames<T>: T extends string | symbol ? T : keyof T

Type parameters

Static ValidEventTypes

ValidEventTypes: string | symbol | object

object should be in either of the following forms:

interface EventTypes {
  'event-with-parameters': any[]
  'event-with-example-handler': (...args: any[]) => void
}

Constructors

constructor

  • Chat constructor.

    Parameters

    Returns Chat

Properties

Private _channelState

_channelState: ChannelStates

Private Optional _client

_client: Client

Private _connectionAttempts

_connectionAttempts: number = 0

Private Optional _connectionInProgress

_connectionInProgress: CancelablePromise<any>

Private Optional _disconnectionInProgress

_disconnectionInProgress: Promise<void>

Private Optional _globalUserState

_globalUserState: GlobalUserStateTags

Private _internalEmitter

_internalEmitter: EventEmitter<string | symbol, any> = new EventEmitter()

Private _isAuthenticated

_isAuthenticated: boolean = false

Private _log

_log: Logger

Private _options

_options: ChatOptions

Private _readyState

_readyState: ChatReadyStates = ChatReadyStates.WAITING

Private Optional _reconnectionInProgress

_reconnectionInProgress: CancelablePromise<any>

Static Commands

Commands: Commands = Commands

Static EventEmitter

EventEmitter: EventEmitterStatic

Static Events

Events: {} = Events

Type declaration

Static prefixed

prefixed: string | boolean

Methods

Private _clearChannelState

  • _clearChannelState(): void
  • Returns void

Private _emit

  • _emit(eventName: string, message: Messages): void
  • Parameters

    Returns void

Private _getChannelState

  • _getChannelState(channel: string): ChannelState | undefined
  • Parameters

    • channel: string

    Returns ChannelState | undefined

Private _getChannels

  • _getChannels(): string[]
  • Returns string[]

Private _handleClientAuthenticationFailure

  • _handleClientAuthenticationFailure(originError: BaseMessage): Promise<void>
  • Parameters

    Returns Promise<void>

Private _handleClientMessage

  • Parameters

    Returns void

Private _handleConnect

  • _handleConnect(): void
  • Returns void

Private _handleDisconnect

  • _handleDisconnect(): void
  • Returns void

Private _handleJoinsAfterConnect

  • _handleJoinsAfterConnect(): Promise<void>
  • Returns Promise<void>

Private _handleReconnect

  • _handleReconnect(): void
  • Returns void

Private _parseMessageForEmitter

  • Parameters

    Returns [string, Messages]

Private _removeChannelState

  • _removeChannelState(channel: string): void
  • Parameters

    • channel: string

    Returns void

Private _setChannelState

  • _setChannelState(channel: string, state: ChannelState): void
  • Parameters

    Returns void

addListener

  • addListener<T>(event: T, fn: EventEmitter.EventListener<EventTypes, T>, context?: Context): this
  • Type parameters

    Parameters

    Returns this

ban

  • This command will allow you to permanently ban a user from the chat room.

    Parameters

    • channel: string
    • username: string

    Returns Promise<NoticeMessages>

block

  • block(channel: string, username: string): Promise<void>
  • This command will allow you to block all messages from a specific user in chat and whispers if you do not wish to see their comments.

    Parameters

    • channel: string
    • username: string

    Returns Promise<void>

broadcast

  • broadcast(message: string): Promise<Promise<void>[]>
  • Broadcast message to all connected channels.

    Parameters

    • message: string

    Returns Promise<Promise<void>[]>

clear

  • This command will allow the Broadcaster and chat moderators to completely wipe the previous chat history.

    Parameters

    • channel: string

    Returns Promise<NoticeMessages>

color

  • Allows you to change the color of your username.

    Parameters

    • channel: string
    • color: string

    Returns Promise<NoticeMessages>

commercial

  • commercial(channel: string, length: 30 | 60 | 90 | 120 | 150 | 180): Promise<NoticeMessages>
  • An Affiliate and Partner command that runs a commercial for all of your viewers.

    Parameters

    • channel: string
    • length: 30 | 60 | 90 | 120 | 150 | 180

    Returns Promise<NoticeMessages>

connect

  • connect(): Promise<void>
  • Connect to Twitch.

    Returns Promise<void>

delete

  • delete(channel: string, targetMessageId: string): Promise<void>
  • Single message removal on a channel.

    Parameters

    • channel: string
    • targetMessageId: string

    Returns Promise<void>

disconnect

  • disconnect(): Promise<void>
  • Disconnected from Twitch.

    Returns Promise<void>

emit

  • emit<T>(event: T, ...args: EventEmitter.EventArgs<EventTypes, T>): boolean
  • Calls each of the listeners registered for a given event.

    Type parameters

    Parameters

    • event: T
    • Rest ...args: EventEmitter.EventArgs<EventTypes, T>

    Returns boolean

emoteOnly

  • This command allows you to set your room so only messages that are 100% emotes are allowed.

    Parameters

    • channel: string

    Returns Promise<NoticeMessages>

emoteOnlyOff

  • This command allows you to disable emote only mode if you previously enabled it.

    Parameters

    • channel: string

    Returns Promise<NoticeMessages>

eventNames

  • eventNames(): Array<EventEmitter.EventNames<EventTypes>>
  • Return an array listing the events for which the emitter has registered listeners.

    Returns Array<EventEmitter.EventNames<EventTypes>>

followersOnly

  • followersOnly(channel: string, period: string): Promise<NoticeMessages>
  • This command allows you or your mods to restrict chat to all or some of your followers, based on how long they’ve followed.

    Parameters

    • channel: string
    • period: string

      Follow time from 0 minutes (all followers) to 3 months.

    Returns Promise<NoticeMessages>

followersOnlyOff

  • This command will disable followers only mode if it was previously enabled on the channel.

    Parameters

    • channel: string

    Returns Promise<NoticeMessages>

help

  • Parameters

    • channel: string

    Returns Promise<NoticeMessages>

host

  • This command will allow you to host another channel on yours.

    Parameters

    • channel: string
    • hostChannel: string

    Returns Promise<NoticeMessages>

join

  • Join a channel.

    example
    Joining #dallas const channel = '#dallas'

    chat.join(channel).then(channelState => { // Do stuff with channelState... })

    example
    Joining multiple channels const channels = ['#dallas', '#ronni']

    Promise.all(channels.map(channel => chat.join(channel))) .then(channelStates => { // Listen to all PRIVMSG chat.on('PRIVMSG', privateMessage => { // Do stuff with privateMessage ... })

    // Listen to PRIVMSG from #dallas ONLY
    chat.on('PRIVMSG/#dallas', privateMessage => {
      // Do stuff with privateMessage ...
    })
    // Listen to all PRIVMSG from #ronni ONLY
    chat.on('PRIVMSG/#ronni', privateMessage => {
      // Do stuff with privateMessage ...
    })

    })

    Parameters

    • channel: string

    Returns Promise<{ roomState: RoomStateTags; userState: undefined | UserStateTags }>

listenerCount

  • listenerCount(event: EventEmitter.EventNames<EventTypes>): number
  • Return the number of listeners listening to a given event.

    Parameters

    Returns number

listeners

  • listeners<T>(event: T): Array<EventEmitter.EventListener<EventTypes, T>>
  • Return the listeners registered for a given event.

    Type parameters

    Parameters

    • event: T

    Returns Array<EventEmitter.EventListener<EventTypes, T>>

marker

  • marker(channel: string, description: string): Promise<void>
  • Adds a stream marker (with an optional description, max 140 characters) at the current timestamp. You can use markers in the Highlighter for easier editing.

    Parameters

    • channel: string
    • description: string

    Returns Promise<void>

me

  • me(channel: string, text: string): Promise<void>
  • This command will color your text based on your chat name color.

    Parameters

    • channel: string
    • text: string

    Returns Promise<void>

mod

  • This command will allow you to promote a user to a channel moderator.

    Parameters

    • channel: string
    • username: string

    Returns Promise<NoticeMessages>

mods

  • This command will display a list of all chat moderators for that specific channel.

    Parameters

    • channel: string

    Returns Promise<NoticeMessages>

off

  • off<T>(event: T, fn?: EventEmitter.EventListener<EventTypes, T>, context?: Context, once?: undefined | false | true): this
  • Type parameters

    Parameters

    • event: T
    • Optional fn: EventEmitter.EventListener<EventTypes, T>
    • Optional context: Context
    • Optional once: undefined | false | true

    Returns this

on

  • on<T>(event: T, fn: EventEmitter.EventListener<EventTypes, T>, context?: Context): this
  • Add a listener for a given event.

    Type parameters

    Parameters

    Returns this

once

  • once<T>(event: T, fn: EventEmitter.EventListener<EventTypes, T>, context?: Context): this
  • Add a one-time listener for a given event.

    Type parameters

    Parameters

    Returns this

part

  • part(channel: string): Promise<void>
  • Depart from a channel.

    Parameters

    • channel: string

    Returns Promise<void>

r9K

  • deprecated

    Parameters

    • channel: string

    Returns Promise<NoticeMessages>

r9KOff

  • deprecated

    Parameters

    • channel: string

    Returns Promise<NoticeMessages>

raid

  • raid(channel: string, raidChannel: string): Promise<void>
  • This command will send the viewer to another live channel.

    Parameters

    • channel: string
    • raidChannel: string

    Returns Promise<void>

reconnect

  • reconnect(options?: Partial<ChatOptions>): CancelablePromise<any>
  • Reconnect to Twitch, providing new options to the client.

    Parameters

    Returns CancelablePromise<any>

removeAllListeners

  • removeAllListeners(event?: EventEmitter.EventNames<EventTypes>): this
  • Remove all listeners, or those of the specified event.

    Parameters

    • Optional event: EventEmitter.EventNames<EventTypes>

    Returns this

removeListener

  • removeListener<T>(event: T, fn?: EventEmitter.EventListener<EventTypes, T>, context?: Context, once?: undefined | false | true): this
  • Remove the listeners of a given event.

    Type parameters

    Parameters

    • event: T
    • Optional fn: EventEmitter.EventListener<EventTypes, T>
    • Optional context: Context
    • Optional once: undefined | false | true

    Returns this

say

  • say(channel: string, message: string, options?: { priority?: undefined | number }): Promise<void>
  • Send a message to a channel.

    Parameters

    • channel: string
    • message: string
    • Default value options: { priority?: undefined | number } = {}
      • Optional priority?: undefined | number

    Returns Promise<void>

send

  • send(message: string, options?: Partial<{ isModerator: boolean; priority: number }>): Promise<void>
  • Send a raw message to Twitch.

    Parameters

    • message: string
    • Optional options: Partial<{ isModerator: boolean; priority: number }>

    Returns Promise<void>

slow

  • This command allows you to set a limit on how often users in the chat room are allowed to send messages (rate limiting).

    Parameters

    • channel: string
    • seconds: string

    Returns Promise<NoticeMessages>

slowOff

  • This command allows you to disable slow mode if you had previously set it.

    Parameters

    • channel: string

    Returns Promise<NoticeMessages>

subscribers

  • This command allows you to set your room so only users subscribed to you can talk in the chat room. If you don't have the subscription feature it will only allow the Broadcaster and the channel moderators to talk in the chat room.

    Parameters

    • channel: string

    Returns Promise<NoticeMessages>

subscribersOff

  • This command allows you to disable subscribers only chat room if you previously enabled it.

    Parameters

    • channel: string

    Returns Promise<NoticeMessages>

timeout

  • timeout(channel: string, username: string, timeout?: undefined | number): Promise<NoticeMessages>
  • This command allows you to temporarily ban someone from the chat room for 10 minutes by default. This will be indicated to yourself and the temporarily banned subject in chat on a successful temporary ban. A new timeout command will overwrite an old one.

    Parameters

    • channel: string
    • username: string
    • Optional timeout: undefined | number

    Returns Promise<NoticeMessages>

unban

  • This command will allow you to lift a permanent ban on a user from the chat room. You can also use this command to end a ban early; this also applies to timeouts.

    Parameters

    • channel: string
    • username: string

    Returns Promise<NoticeMessages>

unblock

  • unblock(channel: string, username: string): Promise<void>
  • This command will allow you to remove users from your block list that you previously added.

    Parameters

    • channel: string
    • username: string

    Returns Promise<void>

unhost

  • Using this command will revert the embedding from hosting a channel and return it to its normal state.

    Parameters

    • channel: string

    Returns Promise<NoticeMessages>

unmod

  • This command will allow you to demote an existing moderator back to viewer status (removing their moderator abilities).

    Parameters

    • channel: string
    • username: string

    Returns Promise<NoticeMessages>

unraid

  • This command will cancel the raid.

    Parameters

    • channel: string

    Returns Promise<NoticeMessages>

unvip

  • unvip(channel: string, username: string): Promise<void>
  • This command will grant VIP status to a user.

    Parameters

    • channel: string
    • username: string

    Returns Promise<void>

updateOptions

  • Updates the client options after instantiation. To update token or username, use reconnect().

    Parameters

    Returns void

vip

  • vip(channel: string, username: string): Promise<void>
  • This command will grant VIP status to a user.

    Parameters

    • channel: string
    • username: string

    Returns Promise<void>

vips

  • vips(channel: string): Promise<void>
  • This command will display a list of VIPs for that specific channel.

    Parameters

    • channel: string

    Returns Promise<void>

whisper

  • whisper(username: string, message: string): Promise<void>
  • This command sends a private message to another user on Twitch.

    Parameters

    • username: string
    • message: string

    Returns Promise<void>

Object literals

Static CompoundEvents

CompoundEvents: object

[Events.NOTICE]

[Events.NOTICE]: NoticeCompounds = NoticeCompounds

[Events.PRIVATE_MESSAGE]

[Events.PRIVATE_MESSAGE]: PrivateMessageCompounds = PrivateMessageCompounds

[Events.USER_NOTICE]

[Events.USER_NOTICE]: UserNoticeCompounds = UserNoticeCompounds

Generated using TypeDoc