From 8a7ed0b106cbb7c575331e6be8099a9295825098 Mon Sep 17 00:00:00 2001 From: Kenneth Jao Date: Tue, 28 Feb 2017 01:59:57 -0500 Subject: [PATCH] Time functionality --- breakbot/date.dat | Bin 44 -> 44 bytes breakbot/slackbot.py | 44 +++++++++++++++++++++++++++++-------------- 2 files changed, 30 insertions(+), 14 deletions(-) diff --git a/breakbot/date.dat b/breakbot/date.dat index 2e07a9a1f90ea89f9536adeedc227b57be4b762f..0d69e0471f34c02f3719136afdf29f6c6c3133f2 100644 GIT binary patch delta 20 ZcmdPVnIOy0BgX&)g^aC*OhJXrdH^Fp1DyZ> delta 20 XcmdPVnIOy000xDOt%XcMh0J;YAm{^g diff --git a/breakbot/slackbot.py b/breakbot/slackbot.py index 79e6f89..3f6f8a4 100644 --- a/breakbot/slackbot.py +++ b/breakbot/slackbot.py @@ -15,7 +15,7 @@ AT_BOT = "<@" + BOT_ID + ">" EXAMPLE_COMMAND = "do" # instantiate Slack & Twilio clients -slack_client = SlackClient("xoxb-146731148148-cOhyqUViCY4wI2gWYMcoOkpW") +slack_client = SlackClient("xoxb-146731148148-L3NkZn2dI5N645QUU34sl2mi") def handle_command(command, channel): global targetDate @@ -25,13 +25,22 @@ def handle_command(command, channel): response = "You have *" + str((targetDate.date() - dt.now().date()).days) + "* days left." elif command.startswith("time left in hell"): - timeDelta = str(targetDate - dt.now()) - days, other = timeDelta.split(",") + timeDelta = str(targetDate - dt.now()).replace(":0",":") + if "," not in timeDelta: + other = " "+timeDelta + days = "0 days" + else: + days, other = timeDelta.split(",") hours, minutes, seconds = other.split(":") response = "Only " + days + ","+hours+" hours, "+minutes+" minutes, and "+seconds.split(".")[0]+" seconds left!" + elif command.startswith("summary"): - timeDelta = str(targetDate - dt.now()) - days, other = timeDelta.split(",") + timeDelta = str(targetDate - dt.now()).replace(":0",":") + if "," not in timeDelta: + other = " "+timeDelta + days = "0 days" + else: + days, other = timeDelta.split(",") hours, minutes, seconds = other.split(":") approx = [ str((targetDate.date() - dt.now().date()).days) + " days, ", @@ -41,26 +50,34 @@ def handle_command(command, channel): approx.append(str(int(minutes)+int(approx[2].replace(" minutes, or ",""))*60)+" seconds.") first = "The current target date is *" + targetDate.strftime("%B %d, %Y") + "*!" - second = "There are " + days + ","+hours+" hours, "+minutes+" minutes, and "+seconds.split(".")[0]+" seconds left!" + second = "There are " + days + ","+hours.lstrip("0")+" hours, "+minutes.lstrip("0")+" minutes, and "+seconds.split(".")[0].lstrip("0")+" seconds left!" third = "Other representations are: " + "".join(approx); response = first+"\n"+second+"\n"+third - elif command.startswith("set new time "): - newDate = command.lower().replace("set new time ","").split(" ") + + elif command.startswith("set new time "): + newDate = command.replace("set new time ","").split(" ") + time = list(map(int, newDate[3].split(":"))) + if newDate[4] == "pm": + if time[0] != 12: + time[0] += 12 + elif newDate[4] == "am" and time[0] == 12: + time[0] -= 12 try: - newDate = dt(int(newDate[2]),int(newDate[0]),int(newDate[1])) + newDate = dt(int(newDate[2]),int(newDate[0]),int(newDate[1]),time[0],time[1]) if(newDate < dt.now()): response = "Please enter in a date after today!" else: targetDate = newDate + print(targetDate) f = open('date.dat', 'wb') pickle.dump(targetDate,f) f.close() - response = "Ok! Your new date is *" + targetDate.strftime("%B %d, %Y") + "*!" + response = "Ok! Your new date is *" + targetDate.strftime("%B %d, %Y %I:%M %p").replace(" 0"," ") + "*!" except ValueError: - response = "That's an invalid date! The format is: *Day Month Year*" + response = "That's an invalid date! The format is: *Day Month Year Hour:Minute AM/PM*" elif command.startswith("target date"): - response = "The current target date is *" + targetDate.strftime("%B %d, %Y") + "*!" + response = "The current target date is *" + targetDate.strftime("%B %d, %Y %I:%M %p").replace(" 0"," ") + "*!" elif command.startswith("flip a coin"): coin = ["Heads!", "Tails!"] response = coin[randint(0,1)] @@ -69,7 +86,7 @@ def handle_command(command, channel): "Summary: Gives a summary of all information.", "Days left in hell: Gives days left to date.", "Time left in hell: Gives countdown left to date.", - "Set new time [Day Month Year]: Sets new target date.", + "Set new time [Day Month Year Hour:Minute AM/PM]: Sets new target date.", "Target date: Gives current target date.", "Flip a coin: Gives heads or tails." ] @@ -86,7 +103,6 @@ def parse_slack_output(slack_rtm_output): for output in output_list: if output and 'text' in output and AT_BOT in output['text']: # return text after the @ mention, whitespace removed - print (output) return output['text'].split(AT_BOT)[1].strip().lower(), \ output['channel']