The latency is basically because I fed the output of the game back through my streaming software. If the game was HTML5 and overlayed directly on Twitch then there would be negligible latency. So it’s not the chatbot solution per se that caused an issue. On my computer (i.e. getting rid of the stream delay) there was no noticeable latency at all (entering command in Twitch chat → chat logged by bot → chat log scraped by my game).
I haven’t gotten far enough into the Twitch API to be able to assess it. I found it very overwhelming to even find the footing to start anywhere. For someone comfortable working with APIs it might be easier, but I just couldn’t wrap my head around converting the given examples to Lua/Defold.
Here’s my script for scraping usernames and messages from the chat log:
local filename = [[C:\Users\[yourusername]\AppData\Roaming\Streamlabs\Streamlabs Chatbot\Services\Twitch\Logs\BotChatLog.txt]]
local line_number = 0
local num_lines_per_call = 10
--define pattern to identify username
local username_start_str = "display-name="
local username_start_str_len = string.len(username_start_str)
local username_end_str = ";"
local username_end_str_len = string.len(username_end_str)
--define pattern to identify chat message
local message_start_str = "PRIVMSG #yourtwitchusernamehere:"
local message_start_str_len = string.len(message_start_str)
--ignore chats from previous stream (i.e. start at end of chat log)
local command = io.input(filename, "r")
line_number = command:seek("end")
--catch and consume new lines
timer.delay(0.1, true, function()
local command = io.input(filename, "r")
command:seek("set", line_number)
if command then
for i=1,num_lines_per_call do
local command_contents = io.read("*l")
line_number = command:seek()
if not command_contents then
break
end
local username_start = string.find(command_contents, username_start_str, 1, true)
local username_end = string.find(command_contents, username_end_str, username_start, true)
--no username found check
if not username_end or not username_start then
break
end
local username = string.sub(command_contents, username_start+username_start_str_len, username_end-username_end_str_len)
local message_start = string.find(command_contents, message_start_str, 1, true)
--no message found check
if not message_start then
break
end
local message = string.sub(command_contents, message_start+message_start_str_len)
print(username .. ": " .. message)
--pass identified username and message to function so it can be interpreted by the game
commands.process_command(username, message)
end
end
end)