From 1b5f5fe117c987ddb6cb446efea31b179f9a5c79 Mon Sep 17 00:00:00 2001 From: Carl Worth Date: Thu, 22 Oct 2020 03:54:27 -0700 Subject: [PATCH] Teach '/puzzle' how to do its magic in a puzzle channel By looking up the corresponding hunt in the database, we can now invoke a '/puzzle' command in the channel of any existing puzzle in the hunt. --- turbot/events.py | 2 +- turbot/interaction.py | 41 ++++++++++++++++++++++++++++++++++------- 2 files changed, 35 insertions(+), 8 deletions(-) diff --git a/turbot/events.py b/turbot/events.py index 931e9c4..9c70e2e 100644 --- a/turbot/events.py +++ b/turbot/events.py @@ -191,7 +191,7 @@ def puzzle_channel_created(turb, puzzle_channel_name, puzzle_channel_id): hunts_table = turb.db.Table('hunts') response = hunts_table.scan( FilterExpression='hunt_id = :hunt_id', - ExpressionAttributeValues={':hunt_id': hunt_id}, + ExpressionAttributeValues={':hunt_id': hunt_id} ) if 'Items' in response: diff --git a/turbot/interaction.py b/turbot/interaction.py index b106196..4e626f3 100644 --- a/turbot/interaction.py +++ b/turbot/interaction.py @@ -205,6 +205,36 @@ def rot(turb, body, args): commands["/rot"] = rot +def find_hunt_for_channel(turb, channel_id, channel_name): + """Given a channel ID/name find the id/name of the hunt for this channel + + This works whether the original channel is a primary hunt channel, + or if it is one of the channels of a puzzle belonging to the hunt. + + Returns a tuple of (hunt_name, hunt_id) or (None, None).""" + + hunts_table = turb.db.Table("hunts") + response = hunts_table.get_item(Key={'channel_id': channel_id}) + + if 'Item' in response: + item = response['Item'] + return (item['hunt_id'], item['name']) + + # So we're not a hunt channel, let's look to see if we are a + # puzzle channel with a hunt-id prefix. + hunt_id = channel_name.split('-')[0] + + response = hunts_table.scan( + FilterExpression='hunt_id = :hunt_id', + ExpressionAttributeValues={':hunt_id': hunt_id} + ) + + if 'Items' in response: + item = response['Items'][0] + return (item['hunt_id'], item['name']) + + return (None, None) + def puzzle(turb, body, args): """Implementation of the /puzzle command @@ -212,17 +242,14 @@ def puzzle(turb, body, args): a modal dialog for user input instead).""" channel_id = body['channel_id'][0] + channel_name = body['channel_name'][0] trigger_id = body['trigger_id'][0] - hunts_table = turb.db.Table("hunts") - response = hunts_table.get_item(Key={'channel_id': channel_id}) + (hunt_id, hunt_name) = find_hunt_for_channel(turb, channel_id, channel_name) - if 'Item' in response: - hunt_name = response['Item']['name'] - hunt_id = response['Item']['hunt_id'] - else: + if not hunt_id: return bot_reply("Sorry, this channel doesn't appear to " - + "be a hunt channel") + + "be a hunt or puzzle channel") view = { "type": "modal", -- 2.43.0