Advertisement
Guest User

bot.py

a guest
Sep 18th, 2016
187
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.17 KB | None | 0 0
  1. # ----------------------------------------------------------------------------------------------------------------------
  2.  
  3. """
  4.    --- Bot Send-Chat Functionality ---
  5.  
  6.    Recommended: Do not touch.
  7.  
  8. """
  9.  
  10. def send(sock, msg):
  11.     if len(message) > 0:
  12.         sock.sendall((msg + "\n").encode("utf-8"))
  13.  
  14.  
  15. def chat(msg):
  16.     print(cfg.NICK + ": " + msg)
  17.     send(s, ('PRIVMSG {0} :{1}'.format(cfg.CHAN, msg)))
  18.  
  19.  
  20.  
  21. # ----------------------------------------------------------------------------------------------------------------------
  22.  
  23.  
  24. """
  25.    --- Chat Commands ---
  26.  
  27.    These are commands that you can have the bot respond to (i.e. !Purge to purge chat)
  28.  
  29. """
  30.  
  31.  
  32.  
  33.  
  34. # ----------------------------------------------------------------------------------------------------------------------
  35.  
  36. """
  37.   ---  IRC Connection Setup ---
  38.  
  39.   Recommended: Do not touch.
  40.  
  41. """
  42.  
  43.  
  44. import cfg
  45. import socket
  46. import time
  47. import re
  48.  
  49. CHAT_MSG = re.compile(r"^:\w+!\w+@\w+\.tmi\.twitch\.tv PRIVMSG #\w+ :")
  50.  
  51. s = socket.socket()
  52. s.connect((cfg.HOST, cfg.PORT))
  53. s.send("PASS {}\r\n".format(cfg.PASS).encode("utf-8"))
  54. s.send("NICK {}\r\n".format(cfg.NICK).encode("utf-8"))
  55. s.send("JOIN {}\r\n".format(cfg.CHAN).encode("utf-8"))
  56.  
  57.  
  58.  
  59. # ----------------------------------------------------------------------------------------------------------------------
  60.  
  61. #BigotBot.py
  62.  
  63. while True:                                                     #Main Loop
  64.     response = s.recv(1024).decode("utf-8")
  65.     if response == "PING :tmi.twitch.tv\r\n":                   #If Ping, return Pong
  66.         s.send("PONG :tmi.twitch.tv\r\n".encode("utf-8"))
  67.         print("Pong Successful")
  68.     else:                                                       #Else, Decode User Message
  69.         username = re.search(r"\w+", response).group(0)         #Gets User
  70.         message = CHAT_MSG.sub("", response)                    #Gets Message
  71.         print (username + ": " + message)                       #Prints User Message
  72.         if message.find("!hello") != -1:                        #Simple Test command to see if Reading Chat Input
  73.             chat ("Hello! I'm speaking!\r\n")
  74.     time.sleep(1 / cfg.RATE)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement