Advertisement
Guest User

[PYTHON] TWITCH BOT

a guest
Apr 11th, 2017
417
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.89 KB | None | 0 0
  1. # coding: by ParduY and cp112
  2. import socket, string, random, threading
  3. from time import gmtime, strftime
  4.  
  5. #for connecting to twitch
  6. HOST = "irc.twitch.tv"
  7. NICK = "YOURNICK"
  8. PORT = 6667
  9. PASS = "OAUTHKEY"
  10.  
  11. #we need this
  12. readbuffer = ""
  13. MODT = False
  14. mood = 0
  15.  
  16. #smiles
  17. angry = " >( "
  18. bigsmile = " :D "  
  19. bored = " :z "
  20. confused = " o_O "
  21. cool = "  B)  "
  22. heart = " <3 "
  23. sad = " :( "
  24. smile = " :) "
  25. tongue = " :P "
  26. surprised = " :o "
  27. undecided = " :\ "
  28. wink = " ;p "
  29. winking =" ;) "
  30.  
  31. #connecting to twitch
  32. s = socket.socket()
  33. s.connect((HOST, PORT))
  34. s.send("PASS " + PASS + "\r\n")
  35. s.send("NICK " + NICK + "\r\n")
  36. s.send("JOIN #king_herdik\r\n")
  37.  
  38. #sending message
  39. def Send_message(message):
  40.     s.send("PRIVMSG #YOURNICK :" + message + "\r\n")
  41.  
  42. # set timer (only once)
  43. def time():
  44.     Send_message("Commands: !time, etc")
  45.  
  46. #timer
  47. t = threading.Timer(10.0, time)
  48. t.start()    
  49.  
  50. while True:
  51.     readbuffer = readbuffer + s.recv(1024)
  52.     temp = string.split(readbuffer, "\n")
  53.     readbuffer = temp.pop()
  54.  
  55.     for line in temp:
  56.         #checks whether the message is PING because Twitch checks you (afk or not)
  57.         if (line[0] == "PING"):
  58.             s.send("PONG %s\r\n" % line[1])
  59.         else:
  60.             #splits the given string
  61.             parts = string.split(line, ":")
  62.  
  63.             if "QUIT" not in parts[1] and "JOIN" not in parts[1] and "PART" not in parts[1]:
  64.                 try:
  65.                     #sets the message variable to the actual message sent
  66.                     message = parts[2][:len(parts[2]) - 1]
  67.                 except:
  68.                     message = ""
  69.                 #sets the username variable to the actual username
  70.                 usernamesplit = string.split(parts[1], "!")
  71.                 username = usernamesplit[0]
  72.                
  73.                 # works after twitch is done announcing stuff
  74.                 # (MODT = Message of the day)
  75.                 if MODT:
  76.                     print username + ": " + message
  77.                     #here you can add your own commands
  78.                     if message == "Hello":
  79.                         Send_message("Hello " + username + cool)
  80.                     if message == "How are you?":
  81.                         mood = random.randint(3,6)
  82.                         if mood == 3: Send_message("Nice, " + username  + smile)                        
  83.                         elif mood == 4: Send_message("Fine, " + username  + tongue)
  84.                         elif mood == 5: Send_message("Not bad, " + username  + bored)
  85.                         elif mood == 6: Send_message("OK, " + username   + undecided)
  86.                     if message == "!time":
  87.                         Send_message(strftime("Now" + "%a, %d %b %Y %H:%M:%S", gmtime())+ "," + username)
  88.                
  89.                 for l in parts:
  90.                     if "End of /NAMES list" in l:
  91.                         MODT = True
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement