bot.py
import config import utils import socket import re import time import thread from time import sleep def main(): s = socket.socket() s.connect((config.HOST, config.PORT)) s.send("PASS {}\r\n".format(config.PASS).encode("utf-8")) s.send("NICK {}\r\n".format(config.NICK).encode("utf-8")) s.send("JOIN #{}\r\n".format(config.CHAN).encode("utf-8")) chat_message = re.compile(r"^:\w+!\w+@\w+\.tmi\.twitch\.tv PRIVMSG #\w+ :") utils.mess(s, "What is even up, dogs?") thread.start_new_thread(utils.fillOpList, ()) while True: utils.mess(s, "Do something awesome!") response = s.recv(1024).decode("utf-8") if response == "PING :tmi.twitch.tv\r\n": s.send("POND :tmi.twitch.tv\r\n".encode("utf-8")) else: username = re.search(r"\w+", response).group(0) message = chat_message.sub("", response) print(response) if message.strip() == "!time": utils.mess(s, "it's currently: " + time.strftime("%I:%M %p %Z on %A %B %d %Y")) if message.strip() == "!messages" and utils.isOp(username): utils.mess(s, "Do something awesome!") utils.mess(s, "Go to youtube.com/winderton and click the subscribe button there!") sleep(1) if __name__ == "__main__": main()
utils.py
import config import urllib2 import json import time import thread from time import sleep def mess(sock, message): sock.send("PRIVMSG #{} :{}\r\n".format(config.CHAN, message).encode("utf-8")) def ban(sock, user): mess(sock, ".ban {}".format(user)) def timeout(sock, user, seconds = 500): mess(sock, ".timeout {}".format(user, seconds)) def fillOpList(): while True: try: url = "http://tmi.twitch.tv/group/user/CHAN/chatters" req = urllib2.Request(url, headers={"accept": "*/*"}) res = urllib2.urlopen(req).read() if res.find("502 bad gateway") == - 1: config.oplist.clear() data = json.loads(res) for p in data["chatters"]["moderators"]: config.oplist[p] = "mod" for p in data["chatters"]["global_mods"]: config.oplist[p] = "global_mod" for p in data["chatters"]["admins"]: config.oplist[p] = "admin" for p in data["chatters"]["staff"]: config.oplist[p] = "staff" except: "Something went wrong...do nothing" sleep(5) def isOp(user): return user in config.oplist
config.py
HOST = "irc.twitch.tv" PORT = 6667 NICK = "NICK" PASS = "PASS" CHAN = "CHAN" oplist = {}
При запуске программа выводит, что подключилась к чату и виснет на строке
response = s.recv(1024).decode("utf-8")
Не могли бы вы подсказать, как это исправить?
Заранее спасибо.