From d1c2aaf4cbf5c169790946458136242fd3c49d06 Mon Sep 17 00:00:00 2001 From: twonfi Date: Thu, 28 Aug 2025 18:05:04 -0700 Subject: [PATCH] Delete own messages and remove reaction instead https://github.com/hackclub/nephthys/pull/50#pullrequestreview-3166968776 --- nephthys/actions/resolve.py | 35 ++++++++++++++++++++++------------- nephthys/macros/thread.py | 33 +++++++++++++++++---------------- 2 files changed, 39 insertions(+), 29 deletions(-) diff --git a/nephthys/actions/resolve.py b/nephthys/actions/resolve.py index 65b6be5..11b7782 100644 --- a/nephthys/actions/resolve.py +++ b/nephthys/actions/resolve.py @@ -9,7 +9,14 @@ from nephthys.utils.permissions import can_resolve from prisma.enums import TicketStatus -async def resolve(ts: str, resolver: str, client: AsyncWebClient, stale: bool = False): +async def resolve( + ts: str, + resolver: str, + client: AsyncWebClient, + stale: bool = False, + add_reaction: bool = True, + send_resolved_message: bool = True, +): resolving_user = await env.db.user.find_unique(where={"slackId": resolver}) if not resolving_user: await send_heartbeat( @@ -55,19 +62,21 @@ async def resolve(ts: str, resolver: str, client: AsyncWebClient, stale: bool = ) return - await client.chat_postMessage( - channel=env.slack_help_channel, - text=env.transcript.ticket_resolve.format(user_id=resolver) - if not stale - else env.transcript.ticket_resolve_stale.format(user_id=resolver), - thread_ts=ts, - ) + if send_resolved_message: + await client.chat_postMessage( + channel=env.slack_help_channel, + text=env.transcript.ticket_resolve.format(user_id=resolver) + if not stale + else env.transcript.ticket_resolve_stale.format(user_id=resolver), + thread_ts=ts, + ) - await client.reactions_add( - channel=env.slack_help_channel, - name="white_check_mark", - timestamp=ts, - ) + if add_reaction: + await client.reactions_add( + channel=env.slack_help_channel, + name="white_check_mark", + timestamp=ts, + ) await client.reactions_remove( channel=env.slack_help_channel, diff --git a/nephthys/macros/thread.py b/nephthys/macros/thread.py index 2835686..4e559cc 100644 --- a/nephthys/macros/thread.py +++ b/nephthys/macros/thread.py @@ -8,30 +8,31 @@ class Thread(Macro): async def run(self, ticket, helper, **kwargs): """ - A simple macro telling people to use threads + A macro that deletes the Nephthys message and removes the + reaction to avoid duplicate thread clutter """ sender = await env.db.user.find_first(where={"id": ticket.openedById}) if not sender: return - user_info = await env.slack_client.users_info(user=sender.slackId) - name = ( - user_info["user"]["profile"].get("display_name") - or user_info["user"]["profile"].get("real_name") - or user_info["user"]["name"] - ) - await env.slack_client.chat_postMessage( - text=f"hey, {name}! would you mind asking your questions in the same thread? :rac_info: it helps us keep track of questions easier!\n\n(to use them, hover over the message and click :speech_balloon:)", + + # Delete the first (FAQ) message sent by the bot + bot_info = await env.slack_client.auth_test() + bot_user_id = bot_info.get("user_id") + bot_messages = await env.slack_client.conversations_replies( channel=env.slack_help_channel, - thread_ts=ticket.msgTs, - ) - # Thread emoji (standard practice on Slack) - await env.slack_client.reactions_add( - channel=env.slack_help_channel, - name="thread", - timestamp=ticket.msgTs, + ts=ticket.msgTs, ) + for msg in bot_messages["messages"]: + if msg["user"] == bot_user_id: + await env.slack_client.chat_delete( + channel=env.slack_help_channel, + ts=msg["ts"], + ) + await resolve( ts=ticket.msgTs, resolver=helper.slackId, client=env.slack_client, + add_reaction=False, + send_resolved_message=False, )