Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import socket
- import string
- import os
- import time
- import sys
- import threading
- import itertools
- from urllib.request import urlopen
- import json
- from user import User
- clear = lambda: os.system("cls")
- USERS = []
- def makeUser(name, place, kills):
- newUser = User(name,place,kills)
- return newUser
- def loadJson():
- with open("users.json") as json_data:
- data = json.load(json_data)
- if len(data["users"]) > 0:
- USERS = []
- for user in data["users"]:
- newUser = makeUser(user["username"], user["place"], user["kills"])
- USERS.append(newUser)
- def saveJson():
- data = {}
- data["users"] = []
- for x in USERS:
- data["users"].append({
- "username": x.get_name(),
- "place": x.get_place(),
- "kills": x.get_kills()
- })
- with open("users.json", "w") as outfile:
- json.dump(data, outfile)
- if True:
- #Twitch credentials
- HOST = "irc.twitch.tv"
- NICK = "tagptroll1"
- PORT = 6667
- PASS = "NiceTry"
- #Send credentials
- s = socket.socket()
- s.connect((HOST, PORT))
- s.send(bytes("PASS " + PASS + "\r\n", "UTF-8"))
- s.send(bytes("NICK " + NICK + "\r\n", "UTF-8"))
- s.send(bytes("JOIN #" + NICK + " \r\n", "UTF-8"))
- def send_message(message):
- s.send(bytes("PRIVMSG #" + NICK + " :#" + message + "\r\n", "UTF-8"))
- def animate():
- for c in itertools.cycle(['|', '/', '-', '\\']):
- if done:
- break
- sys.stdout.write('\rloading ' + c)
- sys.stdout.flush()
- time.sleep(0.1)
- #Functions
- def update_mods():
- global done
- clear()
- done = False
- t = threading.Thread(target=animate)
- t.start()
- #fetch moderators
- response = urlopen('https://tmi.twitch.tv/group/user/' + NICK + '/chatters')
- readable = response.read().decode('utf-8')
- chatlist = json.loads(readable)
- chatters = chatlist['chatters']
- moderators = chatters['moderators']
- moderators.append("tagptroll1")
- done = True
- clear()
- send_message("done")
- print("\rDone! MODS LOADED: " + str(moderators)+"\nChat:\n", end="")
- def get_end_letters(x):
- if int(x) % 10 == 1 and int(x) != 11:
- x = "st"
- elif int(x) % 10 == 2 and int(x) != 12:
- x = "nd"
- elif int(x) % 10 == 3 and int(x) != 13:
- x = "rd"
- else:
- x = "th"
- return str(x)
- def isNumber(s):
- try:
- int(s)
- return True
- except ValueError:
- return False
- def user_in(user):
- for x in USERS:
- print(str(x.get_name()))
- if user.get_name() == x.get_name():
- return True
- else:
- return False
- def find_user(user):
- for x in USERS:
- if user.get_name() == x.get_name():
- return x
- else:
- None
- def addUser(username, x, y):
- newUser = User(username, x, y)
- if not(user_in(newUser)):
- USERS.append(newUser)
- send_message("Entered~")
- else:
- newUser = find_user(newUser)
- send_message("{}, you have already entered with {}{} place and {} kills".format(newUser.get_name(),
- newUser.get_place(),
- get_end_letters(newUser.get_kills()),
- newUser.get_kills()))
- def firsti():
- clear()
- update_mods()
- loadJson()
- firsti()
- #Commands
- def parseToCommand(messageList, username):
- command = messageList[0]
- length = len(messageList)
- try:
- s1 = messageList[1]
- s2 = messageList[2]
- except IndexError:
- None
- else:
- None
- if command == "!bet" and length == 3:
- try:
- isNumber(s1) and isNumber(s2)
- except IndexError:
- None
- else:
- for x in USERS:
- print(str(x.get_name()))
- addUser(username, s1, s2)
- saveJson()
- elif command == "!updatemods":
- update_mods()
- elif command == "!savebets":
- saveJson()
- elif command == "!loadbets":
- loadJson()
- while True:
- line = str(s.recv(1024))
- if "End of /NAMES list" in line:
- break
- # -- Chat reading --
- while True:
- for line in str(s.recv(1024)).split('\\r\\n'):
- parts = line.split(':')
- if len(parts) < 3:
- continue
- if "QUIT" not in parts[1] and "JOIN" not in parts[1] and "PART" not in parts[1]:
- message = parts[2][:len(parts[2])]
- usernamesplit = parts[1].split("!")
- username = usernamesplit[0]
- print(username + ": " + message)
- messageList = message.split(" ")
- parseToCommand(messageList, username)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement