diff --git a/CHANGELOG.md b/CHANGELOG.md index 02ad197..5fd0696 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -16,7 +16,7 @@ The format follows [Keep a Changelog](https://keepachangelog.com/). every direct message. - 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. + channel that pinged nobody. A channel that already holds a message stays. - A lost database connection is retried on the same pool. Before, each failure opened another pool of ten connections. diff --git a/core/views/modmail_views.py b/core/views/modmail_views.py index 1d31c3f..558025f 100644 --- a/core/views/modmail_views.py +++ b/core/views/modmail_views.py @@ -181,9 +181,11 @@ async def _open_ticket( _logger.exception( "Discord refused the ticket header of %s", user.id ) - # Without it nobody is pinged: start over next time. + # Without it nobody is pinged: start over next time, + # unless a message has reached the channel meanwhile. with contextlib.suppress(discord.HTTPException): - await channel.delete() + if not [m async for m in channel.history(limit=1)]: + await channel.delete() await self.initial_message.channel.send( view=refused_notice(error) ) diff --git a/tests/test_ticket_screens.py b/tests/test_ticket_screens.py index 1b37fce..2107cf0 100644 --- a/tests/test_ticket_screens.py +++ b/tests/test_ticket_screens.py @@ -26,8 +26,21 @@ TicketOptionsView, ticket_header, ) -from tests.conftest import USER_ID, member, said, text_of, ticket_channel -from tests.helpers import World, button, forbidden, refused, within_limits +from tests.conftest import ( + USER_ID, + member, + said, + text_of, + ticket_channel, +) +from tests.helpers import ( + World, + button, + forbidden, + history_message, + refused, + within_limits, +) from tests.ui_helpers import interaction TYPES = { @@ -485,6 +498,21 @@ async def test_a_refused_header_leaves_no_silent_ticket( assert flow.initial.add_reaction.await_count == 0 +async def test_a_new_ticket_that_got_a_message_is_not_removed( + monkeypatch: pytest.MonkeyPatch, +) -> None: + # A second direct message can arrive before the header is refused. + flow = _Flow(monkeypatch, "Confirm") + flow.channel.send.side_effect = refused() + flow.channel.delete = AsyncMock() + flow.channel.history.items = [history_message("already here")] + + await flow.run() + + flow.channel.delete.assert_not_awaited() + assert "not delivered" in said(flow.initial.channel.send) + + async def test_staff_hear_that_only_part_of_a_reply_arrived( monkeypatch: pytest.MonkeyPatch, ) -> None: