Advertisement
Guest User

Untitled

a guest
Oct 27th, 2016
165
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.68 KB | None | 0 0
  1. # bot.py
  2.  
  3. import socket
  4. import re
  5. import time
  6. import string
  7.  
  8. CHAT_MSG = re.compile(r"^:\w+!\w+@\w+\.tmi\.twitch\.tv PRIVMSG #\w+ :")
  9.  
  10. HOST = "irc.twitch.tv"              # the Twitch IRC server
  11. PORT = 6667                         # always use port 6667!
  12. NICK = "t_oo"            # your Twitch username, lowercase
  13. PASS = "oauth:ks8p4gtpdtaqot88zfew5hw59c81rx" # your Twitch OAuth token
  14. CHAN = "ishugi"                   # the channel you want to join
  15. RATE = (20/30) # messages per second
  16.  
  17.  
  18. s = socket.socket()
  19. s.connect((HOST, PORT))
  20. s.send("PASS {}\r\n".format(PASS).encode("utf-8"))
  21. s.send("NICK {}\r\n".format(NICK).encode("utf-8"))
  22. s.send("JOIN {}\r\n".format(CHAN).encode("utf-8"))
  23.  
  24. def chat(sock, msg):
  25.     sock.send("PRIVMSG #{} :{}".format(cfg.CHAN, msg))
  26.  
  27. def initial(s):
  28.     readbuffer = ""
  29.     Loading = True
  30.     while Loading:
  31.         readbuffer = readbuffer + (s.recv(1024)).decode("utf-8")
  32.         temp = readbuffer.splitlines()
  33.         readbuffer = temp.pop()
  34.         for line in temp:
  35.             print(line)
  36.             Loading = loadingComplete(line)
  37.     ##sendMessage(s, "Connection successful. VoHiYo")
  38.     print("connected")
  39.  
  40. def loadingComplete(line):
  41.     if(">" in line):
  42.         return False
  43.     else:
  44.         return True
  45.        
  46. def sendMessage(s, message):
  47.     messageTemp = "PRIVMSG #" + CHAN + " :" + message
  48.     s.send((messageTemp + "\r\n").encode("utf-8"))
  49.     print("Sent: " + messageTemp)
  50.    
  51. def getMessage(line):
  52.     print("getMessage")
  53.     separate = line.split(":", 2)
  54.     message = separate[2]
  55.     return message
  56.    
  57. def getUser(line):
  58.     print("getUser")
  59.     separate = line.split(":", 2)
  60.     user = separate[1].split("!", 1)[0]
  61.     return user
  62.    
  63. initial(s)
  64.        
  65. readbuffer = ""
  66. while True:
  67.     '''readbuffer = readbuffer + (s.recv(1024).decode("utf-8"))
  68.    temp = readbuffer.splitlines()
  69.    print(temp)
  70.    readbuffer = temp.pop()
  71.    print(readbuffer)
  72.    for line in temp:
  73.        print(line)
  74.        print(line)
  75.        if "PING :tmi.twitch.tv" in line:
  76.            s.send("PONG :tmi.twitch.tv\r\n".encode("utf-8"))
  77.             print("PONG")      
  78.        user = getUser(line)
  79.        message = getMessage(line)
  80.        print (user + " typed :" + message)
  81.        if "hello justin" in message:
  82.            s.send("Hey there fam")
  83.            break'''
  84.     response = s.recv(1024).decode("utf-8")
  85.     print(response)
  86.     if response == "PING :tmi.twitch.tv\r\n":
  87.         s.send("PONG :tmi.twitch.tv\r\n".encode("utf-8"))
  88.         print("Pong")
  89.     else:
  90.         username = re.search(r"\w+", line).group(0)  # return the entire match
  91.         message = CHAT_MSG.sub("", line)
  92.         print(username + ": " + message)
  93.         time.sleep(1 / cfg.RATE)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement