Advertisement
Guest User

twitch_chat.lua

a guest
Apr 13th, 2017
139
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.40 KB | None | 0 0
  1. -- Twitch chat display
  2. local serverhost = "irc.chat.twitch.tv" -- Server to connect to
  3. local serverport = 6667 -- Server port
  4. local serveruser = "username" -- Server username
  5. local serverpass = "oauth:xxxxxxxxxxxxxxxxxxxxxx" -- Twitch oauth token
  6. local serverchan = "#channel"
  7.  
  8. print("Starting...")
  9.  
  10. function split(data, pat)
  11.   local ret = {}
  12.   for i in string.gmatch(data,pat) do
  13.     table.insert(ret,i)
  14.   end
  15.   return ret
  16. end
  17.  
  18. print("Starting connection...")
  19.  
  20. local event = require("event")
  21. local net = require("internet")
  22. local con = net.open(serverhost, serverport)
  23.  
  24. print("Testing connection...")
  25.  
  26. if(con) then
  27.   print("Connected!")
  28.   local line,png,linesplit,msgfrom = ""
  29.   local running = true
  30.   con:write("PASS " .. serverpass .. "\r\n")
  31.   con:write("NICK " .. serveruser .. "\r\n")
  32.   con:write("JOIN " .. serverchan .. "\r\n")
  33.   print("Joined " .. serverchan)
  34.   function exitProgram()
  35.     running = false
  36.   end
  37.   while(running) do
  38.     event.listen("interrupted", exitProgram)
  39.     line = con:read()
  40.     -- print(line)
  41.     linesplit = split(line, "[^:]+")
  42.     if linesplit[1] == "PING" or linesplit[1] == "PING " then
  43.       png = split(line,"[^:]+")
  44.       con:write("PONG :" .. png[#png] .. "\r\n")
  45.     elseif string.find(linesplit[1], "PRIVMSG #") ~= nil then
  46.       msgfrom = split(linesplit[1], "[^!]+")
  47.       msgfrom = msgfrom[1]
  48.       print(msgfrom .. ": " .. linesplit[2])
  49.     end
  50.   end
  51. else
  52.   print("Connection failed.")
  53. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement