Advertisement
Guest User

Main code for my bot.

a guest
Nov 27th, 2015
273
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.62 KB | None | 0 0
  1. import socket, string, config, time, re, thread, sys, os, os.path, urllib, json
  2.  
  3. RATE = 20/30
  4. #readbuffer = ""
  5. Version = "1.0"
  6. send = ""
  7. MODT = False
  8.  
  9. # Connecting to Twitch IRC by passing credentials and joining a certain channel
  10. s = socket.socket()
  11. s.connect((config.HOST, config.PORT))
  12. s.send("PASS " + config.PASS + "\r\n")
  13. s.send("NICK " + config.USER + "\r\n")
  14. s.send("JOIN #" + config.CHAN + "\r\n")
  15.  
  16. print("Sending data to IRC server on "+config.HOST+":"+str(config.PORT))
  17.  
  18. # Methods
  19. def Send_message(message):
  20.     s.send("PRIVMSG #"+config.CHAN+" :" + message + "\r\n")
  21.     print config.USER + ": " + message
  22.  
  23. def getJson(url):
  24.     resp = urllib.urlopen(url)
  25.     data = json.loads(resp.read())
  26.     return data
  27.  
  28. def isMod(user):
  29.     resp = getJson("http://tmi.twitch.tv/group/user/"+config.CHAN+"/chatters")
  30.     if resp == None:
  31.         print("Invalid responce from JSON! (isMod() on http://tmi.twitch.tv/group/user/"+config.CHAN+"/chatters)")
  32.         return False
  33.     else:
  34.         if user in resp["chatters"]["moderators"]:
  35.             return True
  36.         else:
  37.             return False
  38.  
  39.  
  40. def onCommand(sender, args):
  41.     for cmd in config.CMDS:
  42.         if args[0].lower() == "!" + cmd[0].lower():
  43.             Send_message(cmd[1])
  44.  
  45.     if args[0].lower() == "!help" or args[0].lower() == "!commands":
  46.         ayy = "LittleBigBot command list:"
  47.         for cmd in config.CMDS:
  48.             ayy = ayy +" | !"+cmd[0]+" > "+cmd[2]
  49.         Send_message(ayy)
  50.  
  51.     if args[0].lower() == "!set_crs":
  52.         if not len(args) < 3:
  53.             for cmd in config.CMDS:
  54.                 if cmd[0] == args[1]:
  55.                     cmd[1] = args[2]
  56.                     Send_message("Set the "+cmd[0]+"'s responce to "+args[2]+".")
  57.         else:
  58.             Send_message("Usage: !set_crs <command> <responce>")
  59.  
  60.     if args[0].lower() == "!add_cmd":
  61.         if not len(args) < 3:
  62.             for cmd in config.CMDS:
  63.                 if cmd[0] == args[1]:
  64.                     Send_message("That is already a command!")
  65.                     return
  66.             test111 = "Hello"
  67.             config.CMDS.insert(len(config.CMDS)+1, [args[1], args[2], args[3]])
  68.             Send_message("!"+args[1]+" added!")
  69.         else:
  70.             Send_message("Usage: !add_cmd <command> <responce> <description>")
  71.  
  72.     if args[0].lower() == "!rem_cmd":
  73.         if not len(args) < 3:
  74.             found = False
  75.             for cmd in config.CMDS:
  76.                 if cmd[0] == args[1]:
  77.                     found = True
  78.                     config.CMDS.remove([cmd[0], cmd[1], cmd[2]])
  79.                     Send_message("Removed the '"+cmd[0]+"' command!")
  80.             if found == False:
  81.                 Send_message("There was no command found by that name!")
  82.  
  83.     #test command to check the mod status of a user.
  84.     if args[0].lower() == "!myrank":
  85.         Send_message(str(isMod(sender)))
  86.  
  87. print("twitchThread started, attempting to aquire responce from servers.")
  88. while True:
  89.     try:
  90.         readbuffer = s.recv(1024).decode("utf-8")
  91.         temp = string.split(readbuffer, "\n")
  92.         readbuffer = temp.pop()
  93.         for line in temp:
  94.         # Checks whether the message is PING because its a method of Twitch to check if you're afk
  95.             if (line[0] == "PING"):
  96.                 s.send("PONG %s\r\n" % line[1])
  97.             else:
  98.                 # Splits the given string so we can work with it better
  99.                 parts = string.split(line, ":")
  100.  
  101.                 if "QUIT" not in parts[1] and "JOIN" not in parts[1] and "PART" not in parts[1]:
  102.                     try:
  103.                     # Sets the message variable to the actual message sent
  104.                         message = parts[2][:len(parts[2]) - 1]
  105.                     except:
  106.                         message = ""
  107.                     # Sets the username variable to the actual username
  108.                     usernamesplit = string.split(parts[1], "!")
  109.                     username = usernamesplit[0]
  110.                     # Only works after twitch is done announcing stuff (MODT = Message of the day)
  111.                     if MODT:
  112.                         print(username + ": " + message)
  113.                         if not message == "":
  114.                             args = message.split()
  115.                             try:
  116.                                 onCommand(username, args)
  117.                                 for pat in config.NWBW:
  118.                                     for a in args:
  119.                                         if re.match(pat, a):
  120.                                             Send_message("Watch your language, " + username + "!")
  121.                                             Send_message("/timeout " + username + " 1")
  122.                                             break
  123.                             except IndexError:
  124.                                 print "Exception: IndexError."
  125.                     else:
  126.                         print line
  127.                     for l in parts:
  128.                         if "End of /NAMES list" in l:
  129.                             MODT = True
  130.                             Send_message("Hey how's it goin? I'm littlebigbot_, at your service. You can use !commands or !help for a list of things I can do. Enjoy! KappaPride")
  131.                             print("littlebigbot_ (v." + Version + ") connected and loaded.")
  132.     except socket.error:
  133.         print("Socket Died")
  134.     except socket.timeout:
  135.         print("Shocket Timeout")
  136.         #time.sleep(1 / RATE)
  137.  
  138. def killed_bot():
  139.     Send_message("littlebigbot_ (v." + Version + ") stopped.")
  140.     print("littlebigbot_ stopped. (v." + Version + ")")
  141.     time.sleep(2)
  142.     sys.exit()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement