def handle_my_plans(params: HandlerParams): if (not params.storage.contains(params.storage.PLANS_ENTRY) or len(params.storage.get(params.storage.PLANS_ENTRY)) == 0): common.send_reply( params.client, params.message, "There are no active lunch plans right now! Why not add one using the make-plan command?", ) return plans = params.storage.get(params.storage.PLANS_ENTRY) user = User.get_sender(params.message) common.send_reply( params.client, params.message, "Here are the lunches you've RSVP'd to:\n{}".format("\n".join([ common.render_plan(plan) for _, plan in plans.items() if user in plan.rsvps ])), )
def handle_un_rsvp(params: HandlerParams): if len(params.args) < 2 or len(params.args) > 3: common.send_reply( params.client, params.message, "Oops! The un-rsvp command requires more information. Type help for formatting instructions.", ) return time = None if len(params.args) == 3: time = common.parse_time(params.args[2]) if (not params.storage.contains(params.storage.PLANS_ENTRY) or len(params.storage.get(params.storage.PLANS_ENTRY)) == 0): common.send_reply( params.client, params.message, "There are no lunch plans to remove your RSVP from! Why not add one using the make-plan command?", ) return plans = params.storage.get(params.storage.PLANS_ENTRY) matching_plans = common.get_matching_plans(params.args[1], params.storage, time=time) if len(matching_plans) == 0: common.send_reply( params.client, params.message, "That lunch_id doesn't exist! Type show-plans to see each lunch_id and its associated lunch plan.", ) return if len(matching_plans) > 1: common.send_reply( params.client, params.message, "There are multiple lunches with that lunch_id. Please reissue the command with the time of the lunch" " you're interested in:\n{}".format( "\n".join([ common.render_plan_short(plan) for plan in matching_plans ]), ), ) return user = User.get_sender(params.message) selected_plan = matching_plans[0] if user not in selected_plan.rsvps: common.send_reply( params.client, params.message, "Oops! It looks like you haven't RSVP'd to this lunch_id!", ) return selected_plan.rsvps.remove(user) plans[selected_plan.uuid] = selected_plan params.storage.put(params.storage.PLANS_ENTRY, plans) common.send_reply( params.client, params.message, "You've successful un-RSVP'd to lunch at {}.".format( selected_plan.restaurant), )
def handle_make_plan(params: HandlerParams): if len(params.args) != 3: common.send_reply( params.client, params.message, "Oops! The make-plan command requires more information. Type help for formatting instructions.", ) return try: plan_time = common.parse_time(params.args[2]) except ValueError: common.send_reply( params.client, params.message, "Sorry, the time you entered could not be used because it is not a valid date format.", ) return now = common.get_now() if plan_time < now: common.send_reply( params.client, params.message, "Sorry, you can't plan for a lunch before the current time.", ) return if not params.storage.contains(params.storage.PLANS_ENTRY): params.storage.put(params.storage.PLANS_ENTRY, {}) plans = params.storage.get(params.storage.PLANS_ENTRY) for _, plan in plans.items(): if plan.restaurant == params.args[1] and plan.time == plan_time: common.send_reply( params.client, params.message, "Sorry, there is already a plan with that name and time. How about you RSVP instead?", ) return user = User.get_sender(params.message) plan = Plan(params.args[1], plan_time, [user]) plans = params.storage.get(params.storage.PLANS_ENTRY) plans[plan.uuid] = plan params.storage.put(params.storage.PLANS_ENTRY, plans) common.send_reply( params.client, params.message, "I have added your plan! Enjoy lunch, {}!".format(user.full_name), ) params.cron.add_event( (plan.time - timedelta(minutes=15)).timestamp(), events.AlertLeavingGenerator(plan), plan.uuid, ) params.cron.add_event( plan.time.timestamp(), events.DeletePlanGenerator(plan), )