Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import socket, string, config, time, re, thread, sys, os, os.path, urllib, json
- RATE = 20/30
- #readbuffer = ""
- Version = "1.0"
- send = ""
- MODT = False
- # Connecting to Twitch IRC by passing credentials and joining a certain channel
- s = socket.socket()
- s.connect((config.HOST, config.PORT))
- s.send("PASS " + config.PASS + "\r\n")
- s.send("NICK " + config.USER + "\r\n")
- s.send("JOIN #" + config.CHAN + "\r\n")
- print("Sending data to IRC server on "+config.HOST+":"+str(config.PORT))
- # Methods
- def Send_message(message):
- s.send("PRIVMSG #"+config.CHAN+" :" + message + "\r\n")
- print config.USER + ": " + message
- def getJson(url):
- resp = urllib.urlopen(url)
- data = json.loads(resp.read())
- return data
- def isMod(user):
- resp = getJson("http://tmi.twitch.tv/group/user/"+config.CHAN+"/chatters")
- if resp == None:
- print("Invalid responce from JSON! (isMod() on http://tmi.twitch.tv/group/user/"+config.CHAN+"/chatters)")
- return False
- else:
- if user in resp["chatters"]["moderators"]:
- return True
- else:
- return False
- def onCommand(sender, args):
- for cmd in config.CMDS:
- if args[0].lower() == "!" + cmd[0].lower():
- Send_message(cmd[1])
- if args[0].lower() == "!help" or args[0].lower() == "!commands":
- ayy = "LittleBigBot command list:"
- for cmd in config.CMDS:
- ayy = ayy +" | !"+cmd[0]+" > "+cmd[2]
- Send_message(ayy)
- if args[0].lower() == "!set_crs":
- if not len(args) < 3:
- for cmd in config.CMDS:
- if cmd[0] == args[1]:
- cmd[1] = args[2]
- Send_message("Set the "+cmd[0]+"'s responce to "+args[2]+".")
- else:
- Send_message("Usage: !set_crs <command> <responce>")
- if args[0].lower() == "!add_cmd":
- if not len(args) < 3:
- for cmd in config.CMDS:
- if cmd[0] == args[1]:
- Send_message("That is already a command!")
- return
- test111 = "Hello"
- config.CMDS.insert(len(config.CMDS)+1, [args[1], args[2], args[3]])
- Send_message("!"+args[1]+" added!")
- else:
- Send_message("Usage: !add_cmd <command> <responce> <description>")
- if args[0].lower() == "!rem_cmd":
- if not len(args) < 3:
- found = False
- for cmd in config.CMDS:
- if cmd[0] == args[1]:
- found = True
- config.CMDS.remove([cmd[0], cmd[1], cmd[2]])
- Send_message("Removed the '"+cmd[0]+"' command!")
- if found == False:
- Send_message("There was no command found by that name!")
- #test command to check the mod status of a user.
- if args[0].lower() == "!myrank":
- Send_message(str(isMod(sender)))
- print("twitchThread started, attempting to aquire responce from servers.")
- while True:
- try:
- readbuffer = s.recv(1024).decode("utf-8")
- temp = string.split(readbuffer, "\n")
- readbuffer = temp.pop()
- for line in temp:
- # Checks whether the message is PING because its a method of Twitch to check if you're afk
- if (line[0] == "PING"):
- s.send("PONG %s\r\n" % line[1])
- else:
- # Splits the given string so we can work with it better
- parts = string.split(line, ":")
- if "QUIT" not in parts[1] and "JOIN" not in parts[1] and "PART" not in parts[1]:
- try:
- # Sets the message variable to the actual message sent
- message = parts[2][:len(parts[2]) - 1]
- except:
- message = ""
- # Sets the username variable to the actual username
- usernamesplit = string.split(parts[1], "!")
- username = usernamesplit[0]
- # Only works after twitch is done announcing stuff (MODT = Message of the day)
- if MODT:
- print(username + ": " + message)
- if not message == "":
- args = message.split()
- try:
- onCommand(username, args)
- for pat in config.NWBW:
- for a in args:
- if re.match(pat, a):
- Send_message("Watch your language, " + username + "!")
- Send_message("/timeout " + username + " 1")
- break
- except IndexError:
- print "Exception: IndexError."
- else:
- print line
- for l in parts:
- if "End of /NAMES list" in l:
- MODT = True
- 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")
- print("littlebigbot_ (v." + Version + ") connected and loaded.")
- except socket.error:
- print("Socket Died")
- except socket.timeout:
- print("Shocket Timeout")
- #time.sleep(1 / RATE)
- def killed_bot():
- Send_message("littlebigbot_ (v." + Version + ") stopped.")
- print("littlebigbot_ stopped. (v." + Version + ")")
- time.sleep(2)
- sys.exit()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement