From f4dc6e06a59081c9ccb6e196ac8947287749a2d1 Mon Sep 17 00:00:00 2001 From: Jacob Henry Date: Thu, 24 Nov 2016 19:49:30 +0000 Subject: [PATCH] Made all commands case insensitive Added truescrollutil, for converting actual brother numbers to scrolls, and vice versa --- TrueScrollUtil.py | 72 +++++++++++++++++++++++++++++++++++++++++++++++ WaitonBot.py | 7 +++++ 2 files changed, 79 insertions(+) create mode 100644 TrueScrollUtil.py diff --git a/TrueScrollUtil.py b/TrueScrollUtil.py new file mode 100644 index 0000000..3a7209b --- /dev/null +++ b/TrueScrollUtil.py @@ -0,0 +1,72 @@ +""" +This file handles conversion of users scrolls into numbers +""" + +import re +from SlackUtil import reply + +p = re.compile("truescroll\\s*(\d*)$") + +def handleTrueScrollMsg(slack, msg): + text = msg['text'] + response = None + + m = p.match(text) + if not m: + response = "Could not parse your query. Please invoke as \"truescroll \"" + else: + num = m.group(1) + brotherScroll = getBrotherScroll(num) + trueScroll = getTrueScroll(num) + + response = "The brother with scroll {0} is in fact the {1} brother to sign\n" + response += "The {2} brother to sign will have scroll {3}\n" + + response = response.format(num, trueScroll, num, brotherScroll) + + reply(slack, msg, response, username = "scrollbot") + + +brotherNums = [str(x) for x in range(10) if (not x == 3)] +trueNums = [str(x) for x in range(10)] + +def getTrueScroll(brotherScroll): + return convertBase(brotherScroll, brotherNums, trueNums) + + +def getBrotherScroll(trueScroll): + return convertBase(trueScroll, trueNums, brotherNums) + + +def convertBase(numStr, srcBaseNums, targBaseNums): + #Returns int value + def numberFromBase(ns, numerals): + base = len(numerals) + basePower = 1 + total = 0 + + #Go by character. + for c in ns[::-1]: + try: + digitVal = numerals.index(c) + total += digitVal * basePower + except ValueError: + total += 0 + + basePower = basePower * base + + return total + + #Returns string array, each elt is the corresponding numeral + def numberToBase(n, numerals): + if n==0: + return [0] + digits = [] + while n: + digVal = int(n % len(numerals)) + digits.append(numerals[digVal]) + n /= len(numerals) + n = int(n) + return "".join(digits[::-1]) + + return numberToBase(numberFromBase(numStr, srcBaseNums), targBaseNums) diff --git a/WaitonBot.py b/WaitonBot.py index 3546694..2f21fdc 100755 --- a/WaitonBot.py +++ b/WaitonBot.py @@ -4,6 +4,7 @@ from SlackUtil import * from WaitonUtil import handleWaitonMsg #For waitons from ScrollUtil import handleScrollMsg #For scrolls +from TrueScrollUtil import handleTrueScrollMsg #For true scrolls import re @@ -44,6 +45,7 @@ def handleProfilelessScum(slack, msg, user, for_user=None): waiton_pattern = re.compile("^waiton") scroll_pattern = re.compile("^scroll") +true_scroll_pattern = re.compile("^truescroll") housejob_pattern = re.compile("^(house)?job") def main(): @@ -69,6 +71,7 @@ def main(): #Handle Message text = msg['text'].lower() + msg['text'] = text if not isValidProfile(user):#invalid profile print("Received profileless") handleProfilelessScum(slack, msg, user) @@ -85,6 +88,10 @@ def main(): print("Received scroll from " + user['user']['name']) handleScrollMsg(slack, msg) + elif true_scroll_pattern.match(text): + print("Received true scroll from " + user['user']['name']) + handleTrueScrollMsg(slack, msg) + elif housejob_pattern.match(text): print("Received housejob from " + user['user']['name']) reply(slack, msg, "I cannot do that (yet)", username="sadbot")