diff options
Diffstat (limited to 'src/main.py')
| -rw-r--r-- | src/main.py | 41 |
1 files changed, 40 insertions, 1 deletions
diff --git a/src/main.py b/src/main.py index 1c43240..9daa8f3 100644 --- a/src/main.py +++ b/src/main.py @@ -1,8 +1,9 @@ import os +import re from slack_bolt.app.async_app import AsyncApp from slack_bolt.adapter.socket_mode.async_handler import AsyncSocketModeHandler from dotenv import load_dotenv -from .db import init_db +from .db import init_db, get_user, add_vote, get_vote_counts from .auth import app as fastapi_app from .botnet import deploy_message import threading @@ -11,6 +12,8 @@ import uvicorn load_dotenv() +VOTE_PATTERN = re.compile(r"^vote_(approve|reject)_(\d+)$") + selfbot_token = os.environ.get("SLACK_SELFBOT_OAUTH_XOXP") app_token = os.environ.get("SLACK_APP_TOKEN") selfbot_user_id = os.environ.get("SELFBOT_USER_ID") @@ -64,6 +67,42 @@ async def handle_messages(message, say): except Exception as e: print(f"something broke, good luck LMAO {e}") [email protected](VOTE_PATTERN) +async def handle_vote(ack, respond, logger, context, body, client): + await ack() + + action_id = body["actions"][0]["action_id"] + match = VOTE_PATTERN.match(action_id) + + if not match: + return + + vote_type = match.group(1) + deployment_id = int(match.group(2)) + + user_id = body["user"]["id"] + channel_id = body["channel"]["id"] + user = await get_user(user_id) + if not user: + # await respond(f"You must join the botnet first!", response_type="ephemeral") + await client.chat_postEphemeral(channel=channel_id, user=user_id, text="You must join the botnet first!") + return + + # await respond("Not implemented yet", response_type="ephemeral") + # await client.chat_postEphemeral(channel=channel_id, user=user_id, text="Not implemented yet") + + existing_vote = await get_user_vote(deployment_id, user_id) + if existing_vote: + await client.chat_postEphemeral(channel=channel_id, user=user_id, text=f"You already voted {existing_vote} on this deployment!") + + await add_vote(deployment, user_id, vote_type) + + approve_count, reject_count = await get_vote_counts(deployment_id) + + THRESHOLD = 3 # FIXME: DYNAMICALLY CALCULATE THIS!! + deployment = await get_deployment(deployment_id) + + @app.event("message") async def shut_up_slack_bolt(): # shut up that annoying debug warning that this event is unhandled |
