Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down
13 changes: 13 additions & 0 deletions cogs/workers/modmail_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)


Expand Down
31 changes: 22 additions & 9 deletions core/views/modmail_views.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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:
Expand Down
34 changes: 34 additions & 0 deletions tests/test_ticket_screens.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down