Class TwitchChat

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

public class TwitchChat
extends java.lang.Object
implements ITwitchChat
  • Field Summary

    Fields
    Modifier and Type Field Description
    protected boolean autoJoinOwnChannel
    Whether one's own channel should automatically be joined
    protected com.github.twitch4j.common.util.ExponentialBackoffStrategy backoff
    Helper class to compute delays between connection retries.
    protected java.lang.String baseUrl
    The websocket url for the chat client to connect to.
    protected java.util.Collection<java.lang.String> botOwnerIds
    Bot Owner IDs
    protected java.util.Map<java.lang.String,​java.lang.String> channelIdToChannelName
    Cache: ChannelId to ChannelName
    protected java.util.Map<java.lang.String,​java.lang.String> channelNameToChannelId
    Cache: ChannelName to ChannelId
    protected 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
    protected java.util.List<java.lang.String> commandPrefixes
    IRC Command Handlers
    protected java.util.Set<java.lang.String> currentChannels
    Current Channels
    protected boolean enableMembershipEvents
    Whether JOIN/PART events should be enabled
    static java.lang.String FDGT_TEST_SOCKET_SERVER
    ThirdParty WebSocket Server for Testing
    protected java.util.concurrent.BlockingQueue<java.lang.String> ircCommandQueue
    IRC Command Queue
    protected io.github.bucket4j.Bucket ircJoinBucket
    IRC Join Bucket
    protected io.github.bucket4j.Bucket ircMessageBucket
    IRC Message Bucket
    protected io.github.bucket4j.Bucket ircWhisperBucket
    IRC Whisper Bucket
    protected java.util.concurrent.ScheduledFuture<?> queueThread
    IRC Command Queue Thread
    static int REQUIRED_THREAD_COUNT  
    protected 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.
    protected boolean stopQueueThread
    Command Queue Thread stop flag
    protected java.util.concurrent.ScheduledExecutorService taskExecutor
    Thread Pool Executor
    static java.lang.String TWITCH_WEB_SOCKET_SERVER
    Twitch's official WebSocket Server
    protected com.neovisionaries.ws.client.WebSocketFactory webSocketFactory
    WebSocket Factory
  • Constructor Summary

    Constructors
    Constructor Description
    TwitchChat​(com.github.philippheuer.events4j.core.EventManager eventManager, com.github.philippheuer.credentialmanager.CredentialManager credentialManager, com.github.philippheuer.credentialmanager.domain.OAuth2Credential chatCredential, java.lang.String baseUrl, boolean sendCredentialToThirdPartyHost, java.util.List<java.lang.String> commandPrefixes, java.lang.Integer chatQueueSize, io.github.bucket4j.Bucket ircMessageBucket, io.github.bucket4j.Bucket ircWhisperBucket, io.github.bucket4j.Bucket ircJoinBucket, java.util.concurrent.ScheduledThreadPoolExecutor taskExecutor, long chatQueueTimeout, com.github.twitch4j.common.config.ProxyConfig proxyConfig, boolean autoJoinOwnChannel, boolean enableMembershipEvents, java.util.Collection<java.lang.String> botOwnerIds)
    Constructor
  • Method Summary

    Modifier and Type Method Description
    void close()
    Close
    void connect()
    Connecting to IRC-WS
    void disconnect()
    Disconnecting from IRC-WS
    java.util.Map<java.lang.String,​java.lang.String> getChannelIdToChannelName()  
    java.util.Map<java.lang.String,​java.lang.String> getChannelNameToChannelId()  
    java.util.Set<java.lang.String> getChannels()
    Returns a set of all currently joined channels (without # prefix)
    TMIConnectionState getConnectionState()
    The connection state Default: (TMIConnectionState.DISCONNECTED)
    com.github.philippheuer.credentialmanager.CredentialManager getCredentialManager()
    CredentialManager
    java.util.List<java.lang.String> getCurrentChannels()
    Deprecated.
    use getChannels() instead
    com.github.philippheuer.events4j.core.EventManager getEventManager()
    EventManager
    boolean isChannelJoined​(java.lang.String channelName)
    Check if Chat is currently in a channel
    void joinChannel​(java.lang.String channelName)
    Joining the channel
    boolean leaveChannel​(java.lang.String channelName)
    leaving the channel
    void reconnect()
    Reconnecting to IRC-WS
    protected void sendCommand​(java.lang.String command, java.lang.String... args)
    Send IRC Command
    boolean sendMessage​(java.lang.String channel, java.lang.String message, java.util.Map<java.lang.String,​java.lang.Object> tags)
    Sends a message to the channel while including the specified message tags.
    void sendPrivateMessage​(java.lang.String targetUser, java.lang.String message)
    Sends a user a private message
    boolean sendRaw​(java.lang.String command)
    Send raw irc command

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

    • REQUIRED_THREAD_COUNT

      public static final int REQUIRED_THREAD_COUNT
      See Also:
      Constant Field Values
    • TWITCH_WEB_SOCKET_SERVER

      public static final java.lang.String TWITCH_WEB_SOCKET_SERVER
      Twitch's official WebSocket Server
      See Also:
      Constant Field Values
    • FDGT_TEST_SOCKET_SERVER

      public static final java.lang.String FDGT_TEST_SOCKET_SERVER
      ThirdParty WebSocket Server for Testing
      See Also:
      Constant Field Values
    • baseUrl

      protected final java.lang.String baseUrl
      The websocket url for the chat client to connect to.
    • 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 java.util.Set<java.lang.String> currentChannels
      Current Channels
    • channelIdToChannelName

      protected final java.util.Map<java.lang.String,​java.lang.String> channelIdToChannelName
      Cache: ChannelId to ChannelName
    • channelNameToChannelId

      protected final java.util.Map<java.lang.String,​java.lang.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
    • ircCommandQueue

      protected final java.util.concurrent.BlockingQueue<java.lang.String> ircCommandQueue
      IRC Command Queue
    • queueThread

      protected final java.util.concurrent.ScheduledFuture<?> queueThread
      IRC Command Queue Thread
    • stopQueueThread

      protected volatile boolean stopQueueThread
      Command Queue Thread stop flag
    • botOwnerIds

      protected final java.util.Collection<java.lang.String> botOwnerIds
      Bot Owner IDs
    • commandPrefixes

      protected final java.util.List<java.lang.String> commandPrefixes
      IRC Command Handlers
    • taskExecutor

      protected final java.util.concurrent.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
    • webSocketFactory

      protected final com.neovisionaries.ws.client.WebSocketFactory webSocketFactory
      WebSocket Factory
    • autoJoinOwnChannel

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

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

      protected final com.github.twitch4j.common.util.ExponentialBackoffStrategy backoff
      Helper class to compute delays between connection retries.
      See Also:
      Official suggestion
  • Constructor Details

    • TwitchChat

      public TwitchChat​(com.github.philippheuer.events4j.core.EventManager eventManager, com.github.philippheuer.credentialmanager.CredentialManager credentialManager, com.github.philippheuer.credentialmanager.domain.OAuth2Credential chatCredential, java.lang.String baseUrl, boolean sendCredentialToThirdPartyHost, java.util.List<java.lang.String> commandPrefixes, java.lang.Integer chatQueueSize, io.github.bucket4j.Bucket ircMessageBucket, io.github.bucket4j.Bucket ircWhisperBucket, io.github.bucket4j.Bucket ircJoinBucket, java.util.concurrent.ScheduledThreadPoolExecutor taskExecutor, long chatQueueTimeout, com.github.twitch4j.common.config.ProxyConfig proxyConfig, boolean autoJoinOwnChannel, boolean enableMembershipEvents, java.util.Collection<java.lang.String> botOwnerIds)
      Constructor
      Parameters:
      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
      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
  • Method Details

    • 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​(java.lang.String command, java.lang.String... args)
      Send IRC Command
      Parameters:
      command - IRC Command
      args - command arguments
    • sendRaw

      public boolean sendRaw​(java.lang.String command)
      Send raw irc command
      Parameters:
      command - raw irc command
    • joinChannel

      public void joinChannel​(java.lang.String channelName)
      Joining the channel
      Specified by:
      joinChannel in interface ITwitchChat
      Parameters:
      channelName - channel name
    • leaveChannel

      public boolean leaveChannel​(java.lang.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​(java.lang.String channel, java.lang.String message, @Unofficial java.util.Map<java.lang.String,​java.lang.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 (unofficial).
      Returns:
      whether the message was added to the queue
    • sendPrivateMessage

      public void sendPrivateMessage​(java.lang.String targetUser, java.lang.String message)
      Sends a user a private message
      Parameters:
      targetUser - username
      message - message
    • close

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

      public boolean isChannelJoined​(java.lang.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 java.util.List<java.lang.String> getCurrentChannels()
      Deprecated.
      use getChannels() instead
      Returns a set of all currently joined channels (without # prefix)
      Returns:
      a set of channel names
    • getChannels

      public java.util.Set<java.lang.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 java.util.Map<java.lang.String,​java.lang.String> getChannelIdToChannelName()
      Specified by:
      getChannelIdToChannelName in interface ITwitchChat
      Returns:
      the cached map used for channel id to name mapping
    • getChannelNameToChannelId

      public java.util.Map<java.lang.String,​java.lang.String> getChannelNameToChannelId()
      Specified by:
      getChannelNameToChannelId in interface ITwitchChat
      Returns:
      the cached map used for channel name to id mapping
    • 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
    • getConnectionState

      public TMIConnectionState getConnectionState()
      The connection state Default: (TMIConnectionState.DISCONNECTED)