twitcheasy-twitch-bottwitch-authtwitch-auth-tmitwitch-chat-clienttwitch-eventsubtwitch-pubsub-clienttwitch-webhooks

ChatClient

An interface to Twitch chat.

Overview

Constructor

new ChatClient(authProvider, options)

Creates a new Twitch chat client.

Parameter Type Required Default Description
authProvider ?AuthProvider none

The AuthProvider instance to use for authentication.

options
ChatClientOptions
{}

Events

onAction((channel, user, message, msg) => { /* ... */ })

Fires when a user sends an action (/me) to a channel.

Parameter Type Description
channel string

The channel the action was sent to.

user string

The user that send the action.

message string

The action text.

msg TwitchPrivateMessage

The full message object containing all message and user information.

onAuthenticationFailure((message) => { /* ... */ })

Fires when authentication fails.

Parameter Type Description
message string

The message text.

onBan((channel, user) => { /* ... */ })

Fires when a user is permanently banned from a channel.

Parameter Type Description
channel string

The channel the user is banned from.

user string

The banned user.

onBitsBadgeUpgrade((channel, user, upgradeInfo, msg) => { /* ... */ })

Fires when a user upgrades their bits badge in a channel.

Parameter Type Description
channel string

The channel where the bits badge was upgraded.

user string

The user that has upgraded their bits badge.

upgradeInfo ChatBitsBadgeUpgradeInfo none
msg UserNotice

The full message object containing all message and user information.

onChatClear((channel) => { /* ... */ })

Fires when the chat of a channel is cleared.

Parameter Type Description
channel string

The channel whose chat is cleared.

onCommunityPayForward((channel, user, forwardInfo, msg) => { /* ... */ })

Fires when a user pays forward a subscription that was gifted to them to the community.

Parameter Type Description
channel string

The channel where the gift was forwarded.

user string

The user that forwarded the gift.

forwardInfo ChatCommunityPayForwardInfo

Additional information about the gift.

msg UserNotice

The full message object containing all message and user information.

onCommunitySub((channel, user, subInfo, msg) => { /* ... */ })

Fires when a user gifts random subscriptions to the community of a channel.

Community subs also fire multiple onSubGift events. To prevent alert spam, check Sub gift spam.

Parameter Type Description
channel string

The channel that was subscribed to.

user string

The gifting user.

subInfo ChatCommunitySubInfo

Additional information about the community subscription.

msg UserNotice

The full message object containing all message and user information.

onEmoteOnly((channel, enabled) => { /* ... */ })

Fires when emote-only mode is toggled in a channel.

Parameter Type Description
channel string

The channel where emote-only mode is being toggled.

enabled boolean

Whether emote-only mode is being enabled. If false, it's being disabled.

onFollowersOnly((channel, enabled, delay) => { /* ... */ })

Fires when followers-only mode is toggled in a channel.

Parameter Type Description
channel string

The channel where followers-only mode is being toggled.

enabled boolean

Whether followers-only mode is being enabled. If false, it's being disabled.

delay number

The time (in minutes) a user needs to follow the channel to be able to talk. Only available when enabled === true.

onGiftPaidUpgrade((channel, user, subInfo, msg) => { /* ... */ })

Fires when a user upgrades their gift subscription to a paid subscription in a channel.

Parameter Type Description
channel string

The channel where the subscription was upgraded.

user string

The user that upgraded their subscription.

subInfo ChatSubGiftUpgradeInfo

Additional information about the subscription upgrade.

msg UserNotice

The full message object containing all message and user information.

onHost((channel, target, viewers) => { /* ... */ })

Fires when a channel hosts another channel.

Parameter Type Description
channel string

The hosting channel.

target string

The channel that is being hosted.

viewers number

The number of viewers in the hosting channel.

If you're not logged in as the owner of the channel, this is undefined.

onHosted((channel, byChannel, auto, viewers) => { /* ... */ })

Fires when a channel you're logged in as its owner is being hosted by another channel.

Parameter Type Description
channel string

The channel that is being hosted.

byChannel string

The hosting channel.

auto boolean

Whether the host was triggered automatically (by Twitch's auto-host functionality).

viewers number

The number of viewers in the hosting channel.

onHostsRemaining((channel, numberOfHosts) => { /* ... */ })

Fires when Twitch tells you the number of hosts you have remaining in the next half hour for the channel for which you're logged in as owner after hosting a channel.

Parameter Type Description
channel string

The hosting channel.

numberOfHosts number

The number of hosts remaining in the next half hour.

onJoin((channel, user) => { /* ... */ })

Fires when a user joins a channel.

The join/part events are cached by the Twitch chat server and will be batched and sent every 30-60 seconds.

Please note that unless you enabled the requestMembershipEvents option, this will only react to your own joins.

Parameter Type Description
channel string

The channel that is being joined.

user string

The user that joined.

onMessage((channel, user, message, msg) => { /* ... */ })

Fires when a user sends a message to a channel.

Parameter Type Description
channel string

The channel the message was sent to.

user string

The user that send the message.

message string

The message text.

msg TwitchPrivateMessage

The full message object containing all message and user information.

onMessageFailed((channel, reason) => { /* ... */ })

Fires when sending a message fails.

Parameter Type Description
channel string

The channel that rejected the message.

reason string

The reason for the failure, e.g. you're banned (msg_banned)

onMessageRatelimit((channel, message) => { /* ... */ })

Fires when a message you tried to send gets rejected by the ratelimiter.

Parameter Type Description
channel string

The channel that was attempted to send to.

message string

The message text.

onMessageRemove((channel, messageId, msg) => { /* ... */ })

Fires when a single message is removed from a channel.

Parameter Type Description
channel string

The channel where the message was removed.

messageId string

The ID of the message that was removed.

msg ClearMsg

The full message object containing all message and user information.

This is not the message that was removed. The text of the message is available using msg.params.message though.

onNoPermission((channel, message) => { /* ... */ })

Fires when you tried to execute a command you don't have sufficient permission for.

Parameter Type Description
channel string

The channel that a command without sufficient permissions was executed on.

message string

The message text.

onPart((channel, user) => { /* ... */ })

Fires when a user leaves ("parts") a channel.

The join/part events are cached by the Twitch chat server and will be batched and sent every 30-60 seconds.

Please note that unless you enabled the requestMembershipEvents option, this will only react to your own parts.

Parameter Type Description
channel string

The channel that is being left.

user string

The user that left.

onPrimeCommunityGift((channel, user, subInfo, msg) => { /* ... */ })

Fires when a user gifts a Twitch Prime benefit to the channel.

Parameter Type Description
channel string

The channel where the benefit was gifted.

user string

The user that received the gift.

WARNING: This is a display name and thus will not work as an identifier for the API (login) in some cases.

subInfo ChatPrimeCommunityGiftInfo

Additional information about the gift.

msg UserNotice

The full message object containing all message and user information.

onPrimePaidUpgrade((channel, user, subInfo, msg) => { /* ... */ })

Fires when a user upgrades their Prime subscription to a paid subscription in a channel.

Parameter Type Description
channel string

The channel where the subscription was upgraded.

user string

The user that upgraded their subscription.

subInfo ChatSubUpgradeInfo

Additional information about the subscription upgrade.

msg UserNotice

The full message object containing all message and user information.

onR9k((channel, enabled) => { /* ... */ })

Fires when R9K mode is toggled in a channel.

Parameter Type Description
channel string

The channel where R9K mode is being toggled.

enabled boolean

Whether R9K mode is being enabled. If false, it's being disabled.

onRaid((channel, user, raidInfo, msg) => { /* ... */ })

Fires when a user raids a channel.

Parameter Type Description
channel string

The channel that was raided.

user string

The user that has raided the channel.

raidInfo ChatRaidInfo

Additional information about the raid.

msg UserNotice

The full message object containing all message and user information.

onRaidCancel((channel, msg) => { /* ... */ })

Fires when a user cancels a raid.

Parameter Type Description
channel string

The channel where the raid was cancelled.

msg UserNotice

The full message object containing all message and user information.

onResub((channel, user, subInfo, msg) => { /* ... */ })

Fires when a user resubscribes to a channel.

Parameter Type Description
channel string

The channel that was resubscribed to.

user string

The resubscribing user.

subInfo ChatSubInfo

Additional information about the resubscription.

msg UserNotice

The full message object containing all message and user information.

onRewardGift((channel, user, rewardGiftInfo, msg) => { /* ... */ })

Fires when a user gifts rewards during a special event.

Parameter Type Description
channel string

The channel where the rewards were gifted.

user string

The user that gifted the rewards.

rewardGiftInfo ChatRewardGiftInfo

Additional information about the reward gift.

msg UserNotice

The full message object containing all message and user information.

onRitual((channel, user, ritualInfo, msg) => { /* ... */ })

Fires when a user performs a "ritual" in a channel.

Parameter Type Description
channel string

The channel where the ritual was performed.

user string

The user that has performed the ritual.

ritualInfo ChatRitualInfo

Additional information about the ritual.

msg UserNotice

The full message object containing all message and user information.

onSlow((channel, enabled, delay) => { /* ... */ })

Fires when slow mode is toggled in a channel.

Parameter Type Description
channel string

The channel where slow mode is being toggled.

enabled boolean

Whether slow mode is being enabled. If false, it's being disabled.

delay number

The time (in seconds) a user has to wait between sending messages. Only set when enabling slow mode.

onStandardPayForward((channel, user, forwardInfo, msg) => { /* ... */ })

Fires when a user pays forward a subscription that was gifted to them to a specific user.

Parameter Type Description
channel string

The channel where the gift was forwarded.

user string

The user that forwarded the gift.

forwardInfo ChatStandardPayForwardInfo

Additional information about the gift.

msg UserNotice

The full message object containing all message and user information.

onSub((channel, user, subInfo, msg) => { /* ... */ })

Fires when a user subscribes to a channel.

Parameter Type Description
channel string

The channel that was subscribed to.

user string

The subscribing user.

subInfo ChatSubInfo

Additional information about the subscription.

msg UserNotice

The full message object containing all message and user information.

onSubExtend((channel, user, subInfo, msg) => { /* ... */ })

Fires when a user extends their subscription using a Sub Token.

Parameter Type Description
channel string

The channel where the subscription was extended.

user string

The user that extended their subscription.

subInfo ChatSubExtendInfo

Additional information about the subscription extension.

msg UserNotice

The full message object containing all message and user information.

onSubGift((channel, user, subInfo, msg) => { /* ... */ })

Fires when a user gifts a subscription to a channel to another user.

Community subs also fire multiple onSubGift events. To prevent alert spam, check Sub gift spam.

Parameter Type Description
channel string

The channel that was subscribed to.

user string

The user that the subscription was gifted to. The gifting user is defined in subInfo.gifter.

subInfo ChatSubGiftInfo

Additional information about the subscription.

msg UserNotice

The full message object containing all message and user information.

onSubsOnly((channel, enabled) => { /* ... */ })

Fires when sub only mode is toggled in a channel.

Parameter Type Description
channel string

The channel where sub only mode is being toggled.

enabled boolean

Whether sub only mode is being enabled. If false, it's being disabled.

onTimeout((channel, user, duration) => { /* ... */ })

Fires when a user is timed out from a channel.

Parameter Type Description
channel string

The channel the user is timed out from.

user string

The timed out user.

duration number

The duration of the timeout, in seconds.

onUnhost((channel) => { /* ... */ })

Fires when host mode is disabled in a channel.

Parameter Type Description
channel string

The channel where host mode is being disabled.

onWhisper((user, message, msg) => { /* ... */ })

Fires when receiving a whisper from another user.

Parameter Type Description
user string

The user that sent the whisper.

message string

The message text.

msg Whisper

The full message object containing all message and user information.

Properties

onAnyMessage

Type: (handler: (msg: Message) => void) => Listener

onConnect

Type: (handler: () => void) => Listener

onCtcp

Type: (handler: (target: string, user: string, command: string, params: string, msg: PrivateMessage) => void) => Listener

onCtcpReply

Type: (handler: (target: string, user: string, command: string, params: string, msg: Notice) => void) => Listener

onDisconnect

Type: (handler: (manually: boolean, reason: Error) => void) => Listener

onNickChange

Type: (handler: (oldNick: ?string, newNick: string, msg: NickChange) => void) => Listener

onNotice

Type: (handler: (target: string, user: string, message: string, msg: Notice) => void) => Listener

onRegister

Type: (handler: () => void) => Listener

currentNick

Type: string

isConnected

Type: boolean

isConnecting

Type: boolean

isRegistered

Type: boolean

port

Type: number

serverProperties

Type: ServerProperties

Methods

anonymous(options)

static

Creates a new anonymous Twitch chat client.

Parameter Type Required Default Description
options
ChatClientOptions
{}
Return type: ChatClient

action(channel, message)

async

Sends an action message (/me) to a channel.

Parameter Type Required Default Description
channel string none

The channel to send the message to.

message string none

The message to send.

Return type: void

addCapability(cap)

Parameter Type Required Default Description
cap Capability none none
Return type: void

addListener<Args>(event, listener)

Parameter Type Required Default Description
event EventBinder<Args> none none
listener EventHandler<Args> none none
Return type: Listener

addVip(channel, user)

async

Gives a user VIP status in a channel.

Parameter Type Required Default Description
channel string none

The channel to give the user VIP status in.

user string none

The user to give VIP status.

Return type: void

ban(channel, user, reason)

async

Bans a user from a channel.

Parameter Type Required Default Description
channel ?string none

The channel to ban the user from. Defaults to the channel of the connected user.

user string none

The user to ban from the channel.

reason string ""

The reason for the ban.

Return type: void

changeColor(color)

async

Changes your username color.

Parameter Type Required Default Description
color string none

The hexadecimal code (prefixed with #) or color name to use for your username.

Please note that only Twitch Turbo or Prime users can use hexadecimal codes for arbitrary colors.

If you have neither of those, you can only choose from the following color names:

Blue, BlueViolet, CadetBlue, Chocolate, Coral, DodgerBlue, Firebrick, GoldenRod, Green, HotPink, OrangeRed, Red, SeaGreen, SpringGreen, YellowGreen

Return type: void

clear(channel)

async

Clears all messages in a channel.

Parameter Type Required Default Description
channel string this._credentials.nick

The channel to ban the user from. Defaults to the channel of the connected user.

Return type: void

connect()

async
Return type: void

createMessage<T>(type, params, tags)

Parameter Type Required Default Description
type MessageConstructor<T> none none
params Partial<MessageParamValues<T>> none none
tags Record<string, string> none none
Return type: T

deleteMessage(channel, message)

async

Deletes a message from a channel.

Parameter Type Required Default Description
channel string none

The channel to delete the message from.

message string | TwitchPrivateMessage none

The message (as message ID or message object) to delete.

Return type: void

disableEmoteOnly(channel)

async

Disables emote-only mode in a channel.

Parameter Type Required Default Description
channel string none

The channel to disable emote-only mode in.

Return type: void

disableFollowersOnly(channel)

async

Disables followers-only mode in a channel.

Parameter Type Required Default Description
channel string none

The channel to disable followers-only mode in.

Return type: void

disableR9k(channel)

async

Disables r9k mode in a channel.

Parameter Type Required Default Description
channel string none

The channel to disable r9k mode in.

Return type: void

disableSlow(channel)

async

Disables slow mode in a channel.

Parameter Type Required Default Description
channel string none

The channel to disable slow mode in.

Return type: void

disableSubsOnly(channel)

async

Disables subscribers-only mode in a channel.

Parameter Type Required Default Description
channel string none

The channel to disable subscribers-only mode in.

Return type: void

enableEmoteOnly(channel)

async

Enables emote-only mode in a channel.

Parameter Type Required Default Description
channel string none

The channel to enable emote-only mode in.

Return type: void

enableFollowersOnly(channel, minFollowTime)

async

Enables followers-only mode in a channel.

Parameter Type Required Default Description
channel string none

The channel to enable followers-only mode in.

minFollowTime number 0

The time (in minutes) a user needs to be following before being able to send messages.

Return type: void

enableR9k(channel)

async

Enables r9k mode in a channel.

Parameter Type Required Default Description
channel string none

The channel to enable r9k mode in.

Return type: void

enableSlow(channel, delayBetweenMessages)

async

Enables slow mode in a channel.

Parameter Type Required Default Description
channel string none

The channel to enable slow mode in.

delayBetweenMessages number 30

The time (in seconds) a user needs to wait between messages.

Return type: void

enableSubsOnly(channel)

async

Enables subscribers-only mode in a channel.

Parameter Type Required Default Description
channel string none

The channel to enable subscribers-only mode in.

Return type: void

getCommandClass(command)

Parameter Type Required Default Description
command string none none
Return type: ?MessageConstructor

getMods(channel)

async

Retrieves a list of moderators in a channel.

Parameter Type Required Default Description
channel string none

The channel to retrieve the moderators of.

Return type: Array<string>

getVips(channel)

async

Retrieves a list of VIPs in a channel.

Parameter Type Required Default Description
channel string none

The channel to retrieve the VIPs of.

Return type: Array<string>

host(channel, target)

async

Hosts a channel on another channel.

Parameter Type Required Default Description
channel ?string none

The host source, i.e. the channel that is hosting. Defaults to the channel of the connected user.

target string none

The host target, i.e. the channel that is being hosted.

Return type: void

join(channel)

async

Joins a channel.

Parameter Type Required Default Description
channel string none

The channel to join.

Return type: void

knowsCommand(command)

Parameter Type Required Default Description
command string none none
Return type: boolean

mod(channel, user)

async

Gives a user moderator rights in a channel.

Parameter Type Required Default Description
channel string none

The channel to give the user moderator rights in.

user string none

The user to give moderator rights.

Return type: void

on<Args>(event, listener)

Parameter Type Required Default Description
event EventBinder<Args> none none
listener EventHandler<Args> none none
Return type: Listener

onNamedMessage<T>(commandName, handler, handlerName)

Parameter Type Required Default Description
commandName string none none
handler EventHandler<T> none none
handlerName string none none
Return type: string

onTypedMessage<T>(type, handler, handlerName)

Parameter Type Required Default Description
type MessageConstructor<T> none none
handler EventHandler<T> none none
handlerName string none none
Return type: string

part(channel)

Leaves a channel ("part" in IRC terms).

Parameter Type Required Default Description
channel string none

The channel to leave.

Return type: void

pingCheck()

Return type: void

purge(channel, user, reason)

async

Removes all messages of a user from a channel.

Parameter Type Required Default Description
channel string none

The channel to purge the user's messages from.

user string none

The user to purge.

reason string ""

The reason for the purge.

Return type: void

quit()

async

Disconnects from the chat server.

Return type: void

receiveLine(line)

Parameter Type Required Default Description
line string none none
Return type: void

reconnect(message)

async
Parameter Type Required Default Description
message string none none
Return type: void

registerCapability(cap)

async
Parameter Type Required Default Description
cap Capability none none
Return type: Array<ServerCapability> | Error

registerEvent<T>()

Return type: EventBinder<T>

registerMessageType(cls)

Parameter Type Required Default Description
cls MessageConstructor none none
Return type: void

removeListener(id)

Parameter Type Required Default Description
id Listener none none
Return type: void

removeListener(event, listener)

Parameter Type Required Default Description
event Function none none
listener Function none none
Return type: void

removeMessageListener(handlerName)

Parameter Type Required Default Description
handlerName string none none
Return type: void

removeVip(channel, user)

async

Takes VIP status from a user in a channel.

Parameter Type Required Default Description
channel string none

The channel to remove the user's VIP status in.

user string none

The user to take VIP status from.

Return type: void

runCommercial(channel, duration)

async

Runs a commercial break on a channel.

Parameter Type Required Default Description
channel string none

The channel to run the commercial break on.

duration CommercialLength none

The duration of the commercial break.

Return type: void

say(channel, message, attributes)

async

Sends a message to a channel.

Parameter Type Required Default Description
channel string none

The channel to send the message to.

message string none

The message to send.

attributes ChatSayMessageAttributes {}

The attributes to add to the message.

Return type: void

send(message)

Parameter Type Required Default Description
message Message none none
Return type: void

sendCtcp(target, type, message)

Parameter Type Required Default Description
target string none none
type string none none
message string none none
Return type: void

sendMessage<T>(type, params, tags)

Parameter Type Required Default Description
type MessageConstructor<T> none none
params Partial<MessageParamValues<T>> none none
tags Record<string, string> none none
Return type: void

sendMessageAndCaptureReply<T>(type, params)

async
Parameter Type Required Default Description
type MessageConstructor<T> none none
params Partial<MessageParamValues<T>> none none
Return type: Array<Message>

sendRaw(line)

Parameter Type Required Default Description
line string none none
Return type: void

timeout(channel, user, duration, reason)

async

Times out a user in a channel and removes all their messages.

Parameter Type Required Default Description
channel string none

The channel to time out the user in.

user string none

The user to time out.

duration number 60

The time (in seconds) until the user can send messages again. Defaults to 1 minute.

reason string ""
Return type: void

unhost(channel)

async

Ends any host on a channel.

This only works when in the channel that was hosted in order to provide feedback about success of the command.

If you don't need this feedback, consider using ChatClient#unhostOutside instead.

Parameter Type Required Default Description
channel string this._credentials.nick

The channel to end the host on. Defaults to the channel of the connected user.

Return type: void

unhostOutside(channel)

async

Ends any host on a channel.

This works even when not in the channel that was hosted, but provides no feedback about success of the command.

If you need feedback about success, use ChatClient#unhost (but make sure you're in the channel you are hosting).

Parameter Type Required Default Description
channel string this._credentials.nick

The channel to end the host on. Defaults to the channel of the connected user.

Return type: void

unmod(channel, user)

async

Takes moderator rights from a user in a channel.

Parameter Type Required Default Description
channel string none

The channel to remove the user's moderator rights in.

user string none

The user to take moderator rights from.

Return type: void

waitForRegistration()

async
Return type: void

whisper(user, message)

async

Sends a whisper message to another user.

Parameter Type Required Default Description
user string none

The user to send the message to.

message string none

The message to send.

Return type: void