Advertisement
Guest User

Exos Python IRC bot

a guest
Jun 14th, 2015
6,812
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.99 KB | None | 0 0
  1. import socket, string
  2.  
  3. # Set all the variables necessary to connect to Twitch IRC
  4. HOST = "irc.twitch.tv"
  5. NICK = "BOTSNICKNAME"
  6. PORT = 6667
  7. PASS = "oauth:YOUROAUTHKEYHERE"
  8. readbuffer = ""
  9. MODT = False
  10.  
  11. # Connecting to Twitch IRC by passing credentials and joining a certain channel
  12. s = socket.socket()
  13. s.connect((HOST, PORT))
  14. s.send("PASS " + PASS + "\r\n")
  15. s.send("NICK " + NICK + "\r\n")
  16. s.send("JOIN #YOURCHANNELNAME \r\n")
  17.  
  18. # Method for sending a message
  19. def Send_message(message):
  20.     s.send("PRIVMSG #YOURCHANNELNAME :" + message + "\r\n")
  21.  
  22.  
  23. while True:
  24.     readbuffer = readbuffer + s.recv(1024)
  25.     temp = string.split(readbuffer, "\n")
  26.     readbuffer = temp.pop()
  27.  
  28.     for line in temp:
  29.         # Checks whether the message is PING because its a method of Twitch to check if you're afk
  30.         if (line[0] == "PING"):
  31.             s.send("PONG %s\r\n" % line[1])
  32.         else:
  33.             # Splits the given string so we can work with it better
  34.             parts = string.split(line, ":")
  35.  
  36.             if "QUIT" not in parts[1] and "JOIN" not in parts[1] and "PART" not in parts[1]:
  37.                 try:
  38.                     # Sets the message variable to the actual message sent
  39.                     message = parts[2][:len(parts[2]) - 1]
  40.                 except:
  41.                     message = ""
  42.                 # Sets the username variable to the actual username
  43.                 usernamesplit = string.split(parts[1], "!")
  44.                 username = usernamesplit[0]
  45.                
  46.                 # Only works after twitch is done announcing stuff (MODT = Message of the day)
  47.                 if MODT:
  48.                     print username + ": " + message
  49.                    
  50.                     # You can add all your plain commands here
  51.                     if message == "Hey":
  52.                         Send_message("Welcome to my stream, " + username)
  53.  
  54.                 for l in parts:
  55.                     if "End of /NAMES list" in l:
  56.                         MODT = True
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement