From d36875d1f9c05bc7292b11c930b6db68ccdfdd9b Mon Sep 17 00:00:00 2001 From: RejectModders <142326052+RejectModders@users.noreply.github.com> Date: Sun, 29 Nov 2026 20:41:28 -0600 Subject: [PATCH] fix(tickets): report replies and ticket headers Discord refuses A refused staff reply was only logged, so staff believed it had arrived. A refused header left a channel that pinged nobody; it is now removed and the user is told. --- CHANGELOG.md | 3 +++ cogs/workers/modmail_handler.py | 13 +++++++++++++ core/views/modmail_views.py | 31 +++++++++++++++++++++--------- tests/test_ticket_screens.py | 34 +++++++++++++++++++++++++++++++++ 4 files changed, 72 insertions(+), 9 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 5c95f80..2108241 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,6 +10,9 @@ The format follows [Keep a Changelog](https://keepachangelog.com/). ### Fixed +- Staff are told when Discord refuses a reply. A new ticket whose first + message is refused is removed and the user is told, instead of leaving a + channel that pinged nobody. - A lost database connection is retried on the same pool. Before, each failure opened another pool of ten connections. diff --git a/cogs/workers/modmail_handler.py b/cogs/workers/modmail_handler.py index d8fd5be..865e187 100644 --- a/cogs/workers/modmail_handler.py +++ b/cogs/workers/modmail_handler.py @@ -254,6 +254,19 @@ async def _from_staff( ) ) return + except discord.HTTPException: + _logger.exception("Discord refused a message to %s", user_id) + await channel.send( + view=notice( + "Message Not Delivered", + ( + "Discord refused your message, so it was not sent to " + "the user." + ), + error=True, + ) + ) + return await message.add_reaction(GREEN_CHECK) diff --git a/core/views/modmail_views.py b/core/views/modmail_views.py index 4f35800..1d31c3f 100644 --- a/core/views/modmail_views.py +++ b/core/views/modmail_views.py @@ -1,5 +1,6 @@ """The screens a ticket is opened, annotated and closed with.""" +import contextlib import logging from datetime import datetime from typing import final, override @@ -166,15 +167,27 @@ async def _open_ticket( # Closing waits for this, so the first message makes the transcript async with delivery_lock(channel.id): if header is not None: - await channel.send( - view=header, - allowed_mentions=discord.AllowedMentions( - everyone=False, - users=False, - replied_user=False, - roles=[discord.Object(MODMAIL_STAFF_PING)], - ), - ) + try: + await channel.send( + view=header, + allowed_mentions=discord.AllowedMentions( + everyone=False, + users=False, + replied_user=False, + roles=[discord.Object(MODMAIL_STAFF_PING)], + ), + ) + except discord.HTTPException as error: + _logger.exception( + "Discord refused the ticket header of %s", user.id + ) + # Without it nobody is pinged: start over next time. + with contextlib.suppress(discord.HTTPException): + await channel.delete() + await self.initial_message.channel.send( + view=refused_notice(error) + ) + return try: await send_relayed(channel, self.initial_message) except (discord.HTTPException, PartialDeliveryError) as error: diff --git a/tests/test_ticket_screens.py b/tests/test_ticket_screens.py index a7e43cd..1b37fce 100644 --- a/tests/test_ticket_screens.py +++ b/tests/test_ticket_screens.py @@ -451,6 +451,40 @@ async def test_a_refused_first_message_is_reported( assert flow.initial.add_reaction.await_count == 0 +async def test_staff_hear_that_discord_refused_their_reply( + monkeypatch: pytest.MonkeyPatch, +) -> None: + channel = ticket_channel() + world = World(channel, monkeypatch) + world.relay.side_effect = refused() + person = member(USER_ID) + channel.guild.get_member.side_effect = lambda _: person + message = world.staff_message() + + await world.handler.on_message(message) + + text = said(channel.send) + assert "Message Not Delivered" in text + assert "was not sent to the user" in text + assert message.add_reaction.await_count == 0 + + +async def test_a_refused_header_leaves_no_silent_ticket( + monkeypatch: pytest.MonkeyPatch, +) -> None: + # A channel without its header pings nobody. + flow = _Flow(monkeypatch, "Confirm") + flow.channel.send.side_effect = refused() + flow.channel.delete = AsyncMock() + + await flow.run() + + flow.channel.delete.assert_awaited_once() + assert flow.relay.await_count == 0 + assert "not delivered" in said(flow.initial.channel.send) + assert flow.initial.add_reaction.await_count == 0 + + async def test_staff_hear_that_only_part_of_a_reply_arrived( monkeypatch: pytest.MonkeyPatch, ) -> None: