Class TwitchChat

java.lang.Object
com.github.twitch4j.chat.TwitchChat
All Implemented Interfaces:
ITwitchChat, AutoCloseable

public class TwitchChat extends Object implements ITwitchChat
  • Field Details

    • REQUIRED_THREAD_COUNT

      public static final int REQUIRED_THREAD_COUNT
      See Also:
    • TWITCH_WEB_SOCKET_SERVER

      public static final String TWITCH_WEB_SOCKET_SERVER
      Twitch's official WebSocket Server
      See Also:
    • FDGT_TEST_SOCKET_SERVER

      public static final String FDGT_TEST_SOCKET_SERVER
      ThirdParty WebSocket Server for Testing
      See Also:
    • sendCredentialToThirdPartyHost

      protected final boolean sendCredentialToThirdPartyHost
      Whether the OAuth2Credential password should be sent when the baseUrl does not match the official twitch websocket server, thus bypassing a security check in the library.
    • currentChannels

      protected final Set<String> currentChannels
      Current Channels
    • channelIdToChannelName

      protected final Map<String,String> channelIdToChannelName
      Cache: ChannelId to ChannelName
    • channelNameToChannelId

      protected final Map<String,String> channelNameToChannelId
      Cache: ChannelName to ChannelId
    • ircMessageBucket

      protected final io.github.bucket4j.Bucket ircMessageBucket
      IRC Message Bucket
    • ircWhisperBucket

      protected final io.github.bucket4j.Bucket ircWhisperBucket
      IRC Whisper Bucket
    • ircJoinBucket

      protected final io.github.bucket4j.Bucket ircJoinBucket
      IRC Join Bucket
    • ircAuthBucket

      protected final io.github.bucket4j.Bucket ircAuthBucket
      IRC Auth Bucket
    • perChannelRateLimit

      protected final io.github.bucket4j.Bandwidth perChannelRateLimit
      IRC Per-Channel Message Limit
    • ircCommandQueue

      protected final BlockingQueue<String> ircCommandQueue
      IRC Command Queue
    • queueThread

      protected final ScheduledFuture<?> queueThread
      IRC Command Queue Thread
    • stopQueueThread

      protected volatile boolean stopQueueThread
      Command Queue Thread stop flag
    • botOwnerIds

      protected final Collection<String> botOwnerIds
      Bot Owner IDs
    • commandPrefixes

      protected final List<String> commandPrefixes
      IRC Command Handlers
    • taskExecutor

      protected final ScheduledExecutorService taskExecutor
      Thread Pool Executor
    • chatQueueTimeout

      protected final long chatQueueTimeout
      Time to wait for an item on the chat queue before continuing to next iteration If set too high your thread will be late check to shutdown
    • autoJoinOwnChannel

      protected final boolean autoJoinOwnChannel
      Whether one's own channel should automatically be joined
    • removeChannelOnJoinFailure

      protected final boolean removeChannelOnJoinFailure
      Whether join failures should result in removal from current channels
    • enableMembershipEvents

      protected final boolean enableMembershipEvents
      Whether JOIN/PART events should be enabled
    • maxJoinRetries

      protected final int maxJoinRetries
      The maximum number of attempts to make for joining each channel
    • chatJoinTimeout

      protected final long chatJoinTimeout
      Minimum milliseconds to wait after a join attempt
    • joinAttemptsByChannelName

      protected final io.github.xanthic.cache.api.Cache<String,Integer> joinAttemptsByChannelName
      Cache of recent number of join attempts for each channel
    • bucketByChannelName

      protected final io.github.xanthic.cache.api.Cache<String,io.github.bucket4j.Bucket> bucketByChannelName
      Cache of per-channel message buckets
    • identityProvider

      protected final TwitchIdentityProvider identityProvider
      Twitch Identity Provider
    • validateOnConnect

      protected final boolean validateOnConnect
      Whether OAuth token status should be checked on reconnect
  • Constructor Details

    • TwitchChat

      public TwitchChat(WebsocketConnection websocketConnection, com.github.philippheuer.events4j.core.EventManager eventManager, com.github.philippheuer.credentialmanager.CredentialManager credentialManager, com.github.philippheuer.credentialmanager.domain.OAuth2Credential chatCredential, String baseUrl, boolean sendCredentialToThirdPartyHost, Collection<String> commandPrefixes, Integer chatQueueSize, io.github.bucket4j.Bucket ircMessageBucket, io.github.bucket4j.Bucket ircWhisperBucket, io.github.bucket4j.Bucket ircJoinBucket, io.github.bucket4j.Bucket ircAuthBucket, ScheduledThreadPoolExecutor taskExecutor, long chatQueueTimeout, ProxyConfig proxyConfig, boolean autoJoinOwnChannel, boolean enableMembershipEvents, Collection<String> botOwnerIds, boolean removeChannelOnJoinFailure, int maxJoinRetries, long chatJoinTimeout, int wsPingPeriod, IBackoffStrategy connectionBackoffStrategy, io.github.bucket4j.Bandwidth perChannelRateLimit, boolean validateOnConnect, int wsCloseDelay, BiPredicate<TwitchChat,String> outboundCommandFilter)
      Constructor
      Parameters:
      websocketConnection - WebsocketConnection
      eventManager - EventManager
      credentialManager - CredentialManager
      chatCredential - Chat Credential
      baseUrl - The websocket url for the chat client to connect to
      sendCredentialToThirdPartyHost - Whether the password should be sent when the baseUrl is not official
      commandPrefixes - Command Prefixes
      chatQueueSize - Chat Queue Size
      ircMessageBucket - Bucket for chat
      ircWhisperBucket - Bucket for whispers
      ircJoinBucket - Bucket for joins
      ircAuthBucket - Bucket for auths
      taskExecutor - ScheduledThreadPoolExecutor
      chatQueueTimeout - Timeout to wait for events in Chat Queue
      proxyConfig - Proxy Configuration
      autoJoinOwnChannel - Whether one's own channel should automatically be joined
      enableMembershipEvents - Whether JOIN/PART events should be enabled
      botOwnerIds - Bot Owner IDs
      removeChannelOnJoinFailure - Whether channels should be removed after a join failure
      maxJoinRetries - Maximum join retries per channel
      chatJoinTimeout - Minimum milliseconds to wait after a join attempt
      wsPingPeriod - WebSocket Ping Period
      connectionBackoffStrategy - WebSocket Connection Backoff Strategy
      perChannelRateLimit - Per channel message limit
      validateOnConnect - Whether token should be validated on connect
      wsCloseDelay - Websocket Close Delay
      outboundCommandFilter - Outbound Command Filter
  • Method Details

    • onConnected

      protected void onConnected()
    • onTextMessage

      protected void onTextMessage(String text)
    • onDisconnecting

      protected void onDisconnecting()
    • connect

      public void connect()
      Connecting to IRC-WS
    • disconnect

      public void disconnect()
      Disconnecting from IRC-WS
    • reconnect

      public void reconnect()
      Reconnecting to IRC-WS
    • sendCommand

      protected void sendCommand(String command, String... args)
      Send IRC Command
      Parameters:
      command - IRC Command
      args - command arguments
    • sendRaw

      public boolean sendRaw(String command)
      Send raw irc command

      Note: perChannelRateLimit does not apply when directly using this method

      Parameters:
      command - raw irc command
    • joinChannel

      public void joinChannel(String channelName)
      Joining the channel
      Specified by:
      joinChannel in interface ITwitchChat
      Parameters:
      channelName - channel name
    • issueJoin

      protected void issueJoin(String channelName, int attempts)
    • leaveChannel

      public boolean leaveChannel(String channelName)
      leaving the channel
      Specified by:
      leaveChannel in interface ITwitchChat
      Parameters:
      channelName - channel name
      Returns:
      whether the channel was previously joined
    • sendMessage

      public boolean sendMessage(String channel, String message, Map<String,Object> tags)
      Description copied from interface: ITwitchChat
      Sends a message to the channel while including the specified message tags.
      Specified by:
      sendMessage in interface ITwitchChat
      Parameters:
      channel - the name of the channel to send the message to.
      message - the message to be sent.
      tags - the message tags.
      Returns:
      whether the message was added to the queue
    • sendPrivateMessage

      @Deprecated public void sendPrivateMessage(String targetUser, String message)
      Deprecated.
      Twitch will decommission this method on February 18, 2023; migrate to TwitchHelix#sendWhisper
      Sends a user a private message
      Parameters:
      targetUser - username
      message - message
    • close

      public void close()
      Close
      Specified by:
      close in interface AutoCloseable
      Specified by:
      close in interface ITwitchChat
    • isChannelJoined

      public boolean isChannelJoined(String channelName)
      Description copied from interface: ITwitchChat
      Check if Chat is currently in a channel
      Specified by:
      isChannelJoined in interface ITwitchChat
      Parameters:
      channelName - channel to check (without # prefix)
      Returns:
      boolean
    • getCurrentChannels

      @Deprecated public List<String> getCurrentChannels()
      Deprecated.
      use getChannels() instead
      Returns a set of all currently joined channels (without # prefix)
      Returns:
      a set of channel names
    • getChannels

      public Set<String> getChannels()
      Description copied from interface: ITwitchChat
      Returns a set of all currently joined channels (without # prefix)
      Specified by:
      getChannels in interface ITwitchChat
      Returns:
      a set of channel names
    • getChannelIdToChannelName

      public Map<String,String> getChannelIdToChannelName()
      Specified by:
      getChannelIdToChannelName in interface ITwitchChat
      Returns:
      the cached map used for channel id to name mapping
    • getChannelNameToChannelId

      public Map<String,String> getChannelNameToChannelId()
      Specified by:
      getChannelNameToChannelId in interface ITwitchChat
      Returns:
      the cached map used for channel name to id mapping
    • getLatency

      public long getLatency()
      Specified by:
      getLatency in interface ITwitchChat
      Returns:
      the most recently measured round-trip latency for the socket(s) in milliseconds, or -1 if unknown
    • getState

      public WebsocketConnectionState getState()
      Returns:
      the connection state
    • getConnectionState

      @Deprecated public TMIConnectionState getConnectionState()
      Deprecated.
      use getState() instead
      Returns:
      the connection state
    • getEventManager

      public com.github.philippheuer.events4j.core.EventManager getEventManager()
      EventManager
      Specified by:
      getEventManager in interface ITwitchChat
    • getCredentialManager

      public com.github.philippheuer.credentialmanager.CredentialManager getCredentialManager()
      CredentialManager