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
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ The format follows [Keep a Changelog](https://keepachangelog.com/).

### Fixed

- A blacklisted user gets the notice at most once in two minutes, not for
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.
Expand Down
29 changes: 23 additions & 6 deletions cogs/workers/modmail_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -141,12 +141,7 @@ def _ticket_member(
async def _from_user(self, message: discord.Message) -> None:
"""Handle a direct message: relay it, or offer to open a ticket."""
if await is_blacklisted_message(message):
try:
await message.author.send(view=blacklist_notice())
except discord.Forbidden:
_logger.info(
"Could not tell %s about the blacklist", message.author.id
)
await self._tell_blacklisted(message)
return

guild = self.bot.get_guild(MODMAIL_SERVER_ID)
Expand All @@ -171,6 +166,28 @@ async def _from_user(self, message: discord.Message) -> None:
return
await message.add_reaction(GREEN_CHECK)

async def _tell_blacklisted(self, message: discord.Message) -> None:
# One notice in a while: otherwise every message draws one.
allowed, _ = CoolDown.check(
interaction=message, # pyright: ignore[reportArgumentType]
user=message.author,
command="blacklisted",
)
if not allowed:
return
CoolDown.add(
interaction=message, # pyright: ignore[reportArgumentType]
time_=120,
user=message.author,
command="blacklisted",
)
try:
await message.author.send(view=blacklist_notice())
except discord.Forbidden:
_logger.info(
"Could not tell %s about the blacklist", message.author.id
)

async def _offer_ticket(self, message: discord.Message) -> None:
valid = CoolDown.check(
interaction=message, # pyright: ignore[reportArgumentType]
Expand Down
37 changes: 37 additions & 0 deletions tests/test_blacklist_notice.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
"""A blacklisted user is told, but not for every message."""

from unittest.mock import AsyncMock, MagicMock

import discord
import pytest

from cogs.workers import modmail_handler
from tests.conftest import member


async def _send(
handler: modmail_handler.ModMailHandler, user: int
) -> MagicMock:
message = MagicMock(spec=discord.Message)
message.author = member(user)
message.author.send = AsyncMock()
message.channel = MagicMock(spec=discord.DMChannel)
await handler.on_message(message)
return message.author.send


async def test_a_run_of_messages_draws_one_notice(
monkeypatch: pytest.MonkeyPatch,
) -> None:
monkeypatch.setattr(
modmail_handler, "is_blacklisted_message", AsyncMock(return_value=True)
)
monkeypatch.setattr("core.cooldown.cooldown_data", {"users": {}})
handler = modmail_handler.ModMailHandler(MagicMock())

first, second = await _send(handler, 5), await _send(handler, 5)
other = await _send(handler, 6)

first.assert_awaited_once()
second.assert_not_awaited()
other.assert_awaited_once()