TwitchChat Node
The TwitchChat
node simplifies reading and writing messages to a specific Twitch channel's chat room. It uses Twitch EventSub for receiving messages and the Twitch API for sending them. It also integrates with TwitchMediaLoader
to potentially utilize badge and emote data preloading.
Prerequisites
- Add the Node: Add a
TwitchChat
node to your scene. It's commonly placed as a child of a configuredTwitchService
node, which helps automatically wire up dependencies. - Dependencies: Ensure instances of
TwitchEventsub
,TwitchAPI
, andTwitchMediaLoader
(optional) are available and assigned in the Inspector. IfTwitchChat
is a child ofTwitchService
, these might be assigned automatically or easily accessible. When each of the nodes exists only once in the scene, it will wire up automatically. - Configuration: Select the
TwitchChat
node and configure its properties in the Inspector, especiallyBroadcaster User
.
Configuration (Inspector Properties)
These properties configure the behavior of the TwitchChat
node:
Eventsub
(TwitchEventsub
): Required. TheTwitchEventsub
instance used to listen for incoming chat message events.Media Loader
(TwitchMediaLoader
): Optional. TheTwitchMediaLoader
instance used to preload badge and emote data for the channel.Api
(TwitchAPI
): Required. TheTwitchAPI
instance used for sending chat messages.Broadcaster User
(TwitchUser
): Required. ATwitchUser
resource representing the channel whose chat you want to monitor and send messages to. Subscription will fail if this is not set.Sender User
(TwitchUser
): Optional. ATwitchUser
resource representing the user who will send messages. If leftnull
, the currently authenticated user (owner of the access token used byTwitchAPI
) will be used as the sender.Subscribe On Ready
(bool
): Iftrue
(default), the node automatically attempts to callsubscribe()
when the scene starts (outside the editor). Iffalse
, you must callsubscribe()
manually.
Signals
message_received(message: TwitchChatMessage)
- Emitted when a new chat message arrives for the configured
broadcaster_user
's channel. - The
message
argument is aTwitchChatMessage
object containing detailed information about the message, including the content, sender, badges, emotes, message ID, etc.
- Emitted when a new chat message arrives for the configured
Methods
subscribe() -> void
- Manually initiates the subscription process for chat messages for the configured
Broadcaster User
. - This involves:
- Preloading channel badges and emotes using
TwitchMediaLoader
. - Determining the
sender_user
via an API call if it wasn't explicitly set. - Creating and registering a
channel.chat.message
EventSub subscription via the assignedTwitchEventsub
node.
- Preloading channel badges and emotes using
- You typically call this if
subscribe_on_ready
isfalse
. - Note: This function internally uses
await
for API calls so you canawait
until everything is set up correctly.
- Manually initiates the subscription process for chat messages for the configured
send_message(message: String, reply_parent_message_id: String = "") -> Array[TwitchSendChatMessage.ResponseData]
- Sends a chat message to the configured
broadcaster_user
's channel. message
: The text content of the message to send.reply_parent_message_id
: Optional. The ID of the message you want to reply to. If provided, the sent message will appear as a reply in the chat interface.- Uses the assigned
TwitchAPI
instance. The message is sent as thesender_user
(or the authenticated user ifsender_user
is null). - Returns an Array containing
TwitchSendChatMessage.ResponseData
objects. Indicates if the message was sent successfully or provides a reason if it was dropped (e.g., by AutoMod). - Note: This function internally uses
await
for API calls so you canawait
until the message was sent.
- Sends a chat message to the configured
Usage Example
gdscript
extends Node
@onready var twitch_chat: TwitchChat = %TwitchChat
func _ready():
twitch_chat.message_received.connect(_on_chat_message_received)
# If subscribe_on_ready is false, you might need to call this:
# twitch_chat.subscribe()
# Callback function for new messages
func _on_chat_message_received(chat_message: TwitchChatMessage):
print("[%s] %s: %s" % [chat_message.broadcaster_user_name, chat_message.chatter_user_name, chat_message.message.text])
# Example: Reply "Hello!" to any message containing "hi"
if "hi" in chat_message.message.text.to_lower():
var response_data: Array[TwitchSendChatMessage.ResponseData] = await twitch_chat.send_message("Hello!", chat_message.message_id)
if not response_data.is_empty() and response_data[0].is_sent:
print("Replied successfully!")
else:
printerr("Failed to send reply. Reason: ", response_data[0].drop_reason if not response_data.is_empty() else "Unknown")
# Example: Send a message manually (e.g., triggered by a button press)
func _send_test_message():
var response_data: Array[TwitchSendChatMessage.ResponseData] = await twitch_chat.send_message("This is a test message from Godot!")
if not response_data.is_empty() and response_data[0].is_sent:
print("Test message sent successfully!")
else:
printerr("Failed to send test message. Reason: ", response_data[0].drop_reason if not response_data.is_empty() else "Unknown")