Skip to content

Instantly share code, notes, and snippets.

@AlexCMueller
Last active May 2, 2016 03:53
Show Gist options
  • Save AlexCMueller/28c4d843bc5ddf5147525cb1793aea06 to your computer and use it in GitHub Desktop.
Save AlexCMueller/28c4d843bc5ddf5147525cb1793aea06 to your computer and use it in GitHub Desktop.
import cfg
import socket
import re
import time
import array
import thread
CHAT_MSG = re.compile(r"^:\w+!\w+@\w+\.tmi\.twitch\.tv PRIVMSG #\w+ :")
CHAT_USR = re.compile(r"\w+")
INT_PARSER = re.compile(r"\b\d{7,8}\b")
LVL_REQ_ENABLE = 0
CURRENT_REQ = ""
BROADCASTER = cfg.CHAN[1:]
idList = []
chatQueue = []
LOCAL_MAX_SIZE = cfg.MAX_QUEUE_SIZE
def chat(sock, msg):
sock.send("PRIVMSG " + cfg.CHAN + " :" + msg + "\r\n")
def msgQueue():
while True:
if chatQueue:
chat(s, chatQueue.pop())
time.sleep(1.5)
s = socket.socket()
s.connect((cfg.HOST, cfg.PORT))
s.send("PASS {}\r\n".format(cfg.PASS).encode("utf-8"))
s.send("NICK {}\r\n".format(cfg.NICK).encode("utf-8"))
s.send("JOIN {}\r\n".format(cfg.CHAN).encode("utf-8"))
chatQueue.insert(0, "GDRequestBot is now active.")
thread.start_new_thread(msgQueue, ())
while True:
response = s.recv(1024).decode("utf-8")
if response == "PING :tmi.twitch.tv\r\n":
s.send("PONG :tmi.twitch.tv\r\n".encode("utf-8"))
elif re.match(CHAT_MSG, response):
username = re.search(CHAT_USR, response).group(0)
message = re.sub(CHAT_MSG, "", response)
if re.match(r"!(level)?request\b", message):
if LVL_REQ_ENABLE:
idint = re.search(INT_PARSER, message)
if idint:
if len(idList) < LOCAL_MAX_SIZE:
if not any(username in s for s in idList):
if not any(idint.group(0) in s for s in idList):
idList.insert(0, "\"" + idint.group(0) + "\" by " + username)
chatQueue.insert(0, "Request \"" + idint.group(0) + "\" by @" + username + " added to queue.")
else:
chatQueue.insert(0, "Sorry, @" + username + ", that ID is already in the queue.")
else:
chatQueue.insert(0, "Sorry, @" + username + ", you already have an ID in the queue!")
else:
chatQueue.insert(0, "Sorry, " + username + "! The queue (Max size of "+ str(LOCAL_MAX_SIZE) +") is full. Please try again later.")
else:
chatQueue.insert(0, "@" + username + ", please give a proper 7-8 digit level id.")
else:
chatQueue.insert(0, "Sorry @" + username + ", requests are currently disabled!")
if re.match(r"!wrongid [0-9]{7,8}", message):
changesucess = 0
for i in range(len(idList)):
if username in idList[i]:
newid = re.search("[0-9]{7,8}", message).group(0)
idList[i] = newid + " by " + username
chatQueue.insert(0, "@" + username + "'s request changed to " + newid)
changesucess = 1
if not changesucess:
chatQueue.insert(0, "@" + username + ", you don't have an id in the queue!")
if message.startswith("!popreq") and username == BROADCASTER:
if len(idList) > 0:
CURRENT_REQ = idList.pop()
chatQueue.insert(0, CURRENT_REQ)
else:
CURRENT_REQ = ""
chatQueue.insert(0, "No requests at this time!")
if message.startswith("!enablerequests") and username == BROADCASTER:
LVL_REQ_ENABLE = 1
chatQueue.insert(0, "Level requests are now enabled! Request with !levelrequest or !request.")
if message.startswith("!disablerequests") and username == BROADCASTER:
LVL_REQ_ENABLE = 0
chatQueue.insert(0, "Level requests are now disabled! No further requests will be processed.")
if re.match("!setmaxqueuesize [0-9]+", message) and username == BROADCASTER:
LOCAL_MAX_SIZE = int(re.search(r"[0-9]+", response).group(0))
chatQueue.insert(0, "Max queue size has been changed to " + str(LOCAL_MAX_SIZE) + "!")
if re.search(INT_PARSER, message) and not re.match(r"!(level)?request", message) and not re.match("!wrongid", message):
if LVL_REQ_ENABLE:
chatQueue.insert(0, "@" + username + ", Please use !request or !levelrequest to request a level.")
else:
chatQueue.insert(0, "Sorry @" + username + ", requests are currently disabled!")
if message.startswith("!requestlist"):
temp = "There are currently " + str(len(idList)) + "/" + str(LOCAL_MAX_SIZE) + " levels in the queue: "
i = None
if len(idList) > 1:
maxIterationValue = len(idList)-1
for i in range(0, maxIterationValue):
temp = temp + idList[maxIterationValue - i] + ", "
temp = temp + "and " + idList[maxIterationValue -(i + 1)] + "."
chatQueue.insert(0, temp)
elif len(idList) == 1:
chatQueue.insert(0, temp + idList[0])
else:
chatQueue.insert(0, "@" + username + ", There are currently no requests in the list!")
if message.startswith("!currentrequest"):
if CURRENT_REQ != "":
chatQueue.insert(0, "@" + username + ", the request currently being played is " + CURRENT_REQ)
else:
chatQueue.insert(0, "@" + username + ", there are no requests being played at the moment!")
if message.startswith("!requesthelp"):
chatQueue.insert(0, "http://pastebin.com/wka9XtvC")
if message.startswith("!purgelist") and username == BROADCASTER:
idList[:] = []
chatQueue.insert(0, "Request list purged!")
time.sleep(0.05)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment