aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--bot/cogs/moderation/incidents.py32
-rw-r--r--tests/bot/cogs/moderation/test_incidents.py26
2 files changed, 57 insertions, 1 deletions
diff --git a/bot/cogs/moderation/incidents.py b/bot/cogs/moderation/incidents.py
index 580a258fe..ca591fc6e 100644
--- a/bot/cogs/moderation/incidents.py
+++ b/bot/cogs/moderation/incidents.py
@@ -1,13 +1,14 @@
import asyncio
import logging
import typing as t
+from datetime import datetime
from enum import Enum
import discord
from discord.ext.commands import Cog
from bot.bot import Bot
-from bot.constants import Channels, Emojis, Roles, Webhooks
+from bot.constants import Channels, Colours, Emojis, Roles, Webhooks
from bot.utils.messages import sub_clyde
log = logging.getLogger(__name__)
@@ -41,6 +42,35 @@ ALLOWED_ROLES: t.Set[int] = {Roles.moderators, Roles.admins, Roles.owners}
ALL_SIGNALS: t.Set[str] = {signal.value for signal in Signal}
+def make_embed(incident: discord.Message, outcome: Signal, actioned_by: discord.Member) -> discord.Embed:
+ """
+ Create an embed representation of `incident` for the #incidents-archive channel.
+
+ The name & discriminator of `actioned_by` and `outcome` will be presented in the
+ embed footer. Additionally, the embed is coloured based on `outcome`.
+
+ The author of `incident` is not shown in the embed. It is assumed that this piece
+ of information will be relayed in other ways, e.g. webhook username.
+
+ As mentions in embeds do not ping, we do not need to use `incident.clean_content`.
+ """
+ if outcome is Signal.ACTIONED:
+ colour = Colours.soft_green
+ footer = f"Actioned by {actioned_by}"
+ else:
+ colour = Colours.soft_red
+ footer = f"Rejected by {actioned_by}"
+
+ embed = discord.Embed(
+ description=incident.content,
+ timestamp=datetime.utcnow(),
+ colour=colour,
+ )
+ embed.set_footer(text=footer, icon_url=actioned_by.avatar_url)
+
+ return embed
+
+
def is_incident(message: discord.Message) -> bool:
"""True if `message` qualifies as an incident, False otherwise."""
conditions = (
diff --git a/tests/bot/cogs/moderation/test_incidents.py b/tests/bot/cogs/moderation/test_incidents.py
index c2e32fe6b..4731a786d 100644
--- a/tests/bot/cogs/moderation/test_incidents.py
+++ b/tests/bot/cogs/moderation/test_incidents.py
@@ -9,6 +9,7 @@ import aiohttp
import discord
from bot.cogs.moderation import Incidents, incidents
+from bot.constants import Colours
from tests.helpers import (
MockAsyncWebhook,
MockBot,
@@ -68,6 +69,31 @@ mock_404 = discord.NotFound(
)
+class TestMakeEmbed(unittest.TestCase):
+ """Collection of tests for the `make_embed` helper function."""
+
+ def test_make_embed_actioned(self):
+ """Embed is coloured green and footer contains 'Actioned' when `outcome=Signal.ACTIONED`."""
+ embed = incidents.make_embed(MockMessage(), incidents.Signal.ACTIONED, MockMember())
+
+ self.assertEqual(embed.colour.value, Colours.soft_green)
+ self.assertIn("Actioned", embed.footer.text)
+
+ def test_make_embed_not_actioned(self):
+ """Embed is coloured red and footer contains 'Rejected' when `outcome=Signal.NOT_ACTIONED`."""
+ embed = incidents.make_embed(MockMessage(), incidents.Signal.NOT_ACTIONED, MockMember())
+
+ self.assertEqual(embed.colour.value, Colours.soft_red)
+ self.assertIn("Rejected", embed.footer.text)
+
+ def test_make_embed_content(self):
+ """Incident content appears as embed description."""
+ incident = MockMessage(content="this is an incident")
+ embed = incidents.make_embed(incident, incidents.Signal.ACTIONED, MockMember())
+
+ self.assertEqual(incident.content, embed.description)
+
+
@patch("bot.constants.Channels.incidents", 123)
class TestIsIncident(unittest.TestCase):
"""