Twitch Integration

Twitch Integration

Twitch integration is built into the Lumberyard engine with ChatPlay, JoinIn, and the developer APIs. The current Lua Scripting reference, linked below, is not as detailed as the other aspects of the Twitch, and thus methods parameters and language can be confusing. For example, the reference guide uses the parameter channel for some method inputs, but does not explain what a channel is, and where people familiar with Twitch may believe it is the channel name, it is not related. The tutorials don’t overview Lua Scripting for Twitch, but rather Flow Graph, a visual scripting system that is being deprecated. The real strength with the Twitch integration in the current version is from the visual scripting, Flow Graph, which makes connecting to Twitch and other Twitch operations smooth. To contain all scripting in one language, preferably not a visual scripting like Flow Graph, developers can use the Flow Graph scripting system as a test of functionality. By slowly removing layers/nodes of the visual scripting and implementing them into Lua, the Lua script can be unit tested.

To develop for Twitch, developers need to connect the game to the Twitch application, through the Twitch developer dashboard, which will provide a clientID. To use ChatPlay the chatPlay_ClientID must be set to the clientID value provided in the game.cfg, see register reference.

Another discrepancy with Lumberyard’s Lua Twitch documentation is with regards to setting a keyword which is recognized in the chat. In Flow Graph, documentation and engine source code, the phrasing used for recognizing a word in the Twitch chat is keyword. Within the engine code and Flow Graph, the phrasing for methods is ‘RegisterKeyword’ however the Lua script is lacking this register. The Lua reference has a notification callback for keyword match and other functions calling out to a set keyword. The miscommunication in the reference, is the method function ‘AddOption’ is the way to add a keyword. Once the keyword is added, it isn’t set to be recognized by default, it is added in a disable state; which, is why the options either need to be enabled individual or all options can be enabled simultaneously.

Twitch Bots

While Lumberyard provides their ChatPlay feature to integrate Twitch with the engine, Twitch also provides their own IRC for anyone to be able to connect to channels and chat rooms. Bots are a useful way of automating certain tasks that a broadcaster might find important and helpful, such as chat moderation, immediate feedback loop, and custom chat commands. Some commonly available Twitch bots are Nightbot, Phantombot, and Moobot.

Twitch bots are nothing but pieces of code that you, as the broadcaster, can start during streaming (or even when you are offline for connecting to chat rooms). Bots can be written in any language. In most cases, socket programming is used to connect to the IRC channel. IRC is set up as a TCP connection. All messages that are sent or received are done so in the bytes format. All bytes are appended with “\r\n” to denote the end of the message.

Authentication

IRC works very similar to how ChatPlay is set up. To connect your bot to your channel, or any other channel, you’ll need to get an oauth token linked to the Twitch account that you want to link the bot with. To connect to a channel through IRC, the bot needs to be authenticated. This can be done through sending the following messages:

  1. NICK <your bot’s Twitch account username>
  2. PASS <the oauth token>
  3. JOIN #<name of the channel you want to connect to>

Ping

IRC clients need to keep their connection alive. Every 5 minutes or so, the Twitch servers send a ping to test whether the connection is still active or have been terminated. Bots need to listen for this ping and reply with a “PONG :tmi.twitch.tv”.

Sending/Receiving Messages

Twtich IRC uses “PRIVMSG” to send and receive messages. This is true for communication over a chat channel, a chat room, or even whispers. Some example commands:

  1. Sending a message to a channel:
    > PRIVMSG #<channel> :<message>
  2. Sending a message to a chat room:
    > PRIVMSG #chatrooms:<channel ID>:<room UUID> :<message>
  3. Sending a whisper:
    > PRIVMSG #<channel> :/w <recepient> <message>

Moderation and Other Chat Commands

Broadcasters can grant “mod” permissions to specific users. Every broadcaster is by default a moderator of its own channel. When a bot with a moderator status joins a chat, it can regulate the chat in the channel to a certain extent. These actions include:

  1. Slow mode (/slow or /slowoff):
  2. R9K mode (/r9kbeta or /r9kbetaoff)
  3. Emote mode (/emoteonly or /emoteonlyoff)
  4. Ban user (/ban or /unban)
  5. Clear Chat (/clear)

Capabilities

Twitch allows requesting for capabilities that allow a broadcaster or a moderator of the chat to get more information about the viewers.

  1. Membership (CAP REQ :twitch.tv/membership)
    Get information about when someone joins or departs from a channel, gains or loses moderator status, or list the existing chatters in a channel.
  2. Tags (CAP REQ :twitch.tv/tags)
    Get user state related tag information with several commands.
  3. Commands (CAP REQ :twitch.tv/commands)
    Enables several Twitch-specific commands.

Due to caching, events are not sent to a channel immediately; instead, they are batched up and sent every 10 seconds.

Limits

Normal Twitch accounts are associated with some limits that they need to follow. You are, however, allowed to register your bot with Twitch. Known or verified bot have a slightly more liberal set of limits. Currently, bots can be registered using this form. For the official set of limits, check out the Twitch docs.

Resources

Sidebar