├── config.py ├── bot.py └── utils.py /config.py: -------------------------------------------------------------------------------- 1 | HOST = "irc.twitch.tv" 2 | PORT = 6667 3 | NICK = "SimpleTwitchBot" 4 | PASS = "" 5 | CHAN = "winderton" 6 | RATE = (20/30) 7 | 8 | 9 | oplist = {} 10 | -------------------------------------------------------------------------------- /bot.py: -------------------------------------------------------------------------------- 1 | import config 2 | import utils 3 | import socket 4 | import re 5 | import time 6 | import thread 7 | from time import sleep 8 | 9 | 10 | 11 | def main(): 12 | s = socket.socket() 13 | s.connect((config.HOST, config.PORT)) 14 | s.send("PASS {}\r\n".format(config.PASS).encode("utf-8")) 15 | s.send("NICK {}\r\n".format(config.NICK).encode("utf-8")) 16 | s.send("JOIN #{}\r\n".format(config.CHAN).encode("utf-8")) 17 | 18 | chat_message = re.compile(r"^:\w+!\w+@\w+\.tmi\.twitch\.tv PRIVMSG #\w+ :") 19 | utils.mess(s, "What is even up, dogs?") 20 | 21 | thread.start_new_thread(utils.fillOpList, ()) 22 | while True: 23 | response = s.recv(1024).decode("utf-8") 24 | if response == "PING :tmi.twitch.tv\r\n": 25 | s.send("POND :tmi.twitch.tv\r\n".encode("utf-8")) 26 | else: 27 | username = re.search(r"\w+", response).group(0) 28 | message = chat_message.sub("", response) 29 | print(response) 30 | 31 | 32 | if message.strip() == "!time": 33 | utils.mess(s, "it's currently: " + time.strftime("%I:%M %p %Z on %A %B %d %Y")) 34 | if message.strip() == "!messages" and utils.isOp(username): 35 | utils.mess(s, "Do something awesome!") 36 | utils.mess(s, "Go to youtube.com/winderton and click the subscribe button there!") 37 | sleep(1) 38 | 39 | 40 | 41 | 42 | 43 | if __name__ == "__main__": 44 | main() -------------------------------------------------------------------------------- /utils.py: -------------------------------------------------------------------------------- 1 | import config 2 | import urllib2 3 | import json 4 | import time 5 | import thread 6 | from time import sleep 7 | 8 | 9 | 10 | def mess(sock, message): 11 | sock.send("PRIVMSG #{} :{}\r\n".format(config.CHAN, message)) 12 | 13 | 14 | 15 | def ban(sock, user): 16 | mess(sock, ".ban {}".format(user)) 17 | 18 | 19 | 20 | def timeout(sock, user, seconds = 500): 21 | mess(sock, ".timeout {}".format(user, seconds)) 22 | 23 | 24 | 25 | #req = request 26 | #res = response 27 | def fillOpList(): 28 | while True: 29 | try: 30 | url = "http://tmi.twitch.tv/group/user/winderton/chatters" 31 | req = urllib2.Request(url, headers={"accept": "*/*"}) 32 | res = urllib2.urlopen(req).read() 33 | if res.find("502 bad gateway") == - 1: 34 | config.oplist.clear() 35 | data = json.loads(res) 36 | for p in data["chatters"]["moderators"]: 37 | config.oplist[p] = "mod" 38 | for p in data["chatters"]["global_mods"]: 39 | config.oplist[p] = "global_mod" 40 | for p in data["chatters"]["admins"]: 41 | config.oplist[p] = "admin" 42 | for p in data["chatters"]["staff"]: 43 | config.oplist[p] = "staff" 44 | except: 45 | "Something went wrong...do nothing" 46 | sleep(5) 47 | 48 | 49 | 50 | def isOp(user): 51 | return user in config.oplist 52 | 53 | --------------------------------------------------------------------------------