]> git.cworth.org Git - turbot/commitdiff
Teach '/puzzle' how to do its magic in a puzzle channel
authorCarl Worth <cworth@cworth.org>
Thu, 22 Oct 2020 10:54:27 +0000 (03:54 -0700)
committerCarl Worth <cworth@cworth.org>
Thu, 22 Oct 2020 10:59:39 +0000 (03:59 -0700)
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
turbot/interaction.py

index 931e9c453b271db34f786a6fd2ca60b9cde52bb9..9c70e2e2c8cd0b695b15eee1a6281799320bc0d7 100644 (file)
@@ -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:
index b106196d28cd0fc1106905278af40ea01aea102a..4e626f39fc0c7adc4ec2a3be32c63d9aec632216 100644 (file)
@@ -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",