From 35eb459c36820ea856c5fe126086353bff5e561e Mon Sep 17 00:00:00 2001 From: Slipstream Date: Wed, 11 Jun 2025 14:26:55 -0600 Subject: [PATCH] Add clean_content property and tests (#56) --- disagreement/models.py | 9 +++++++++ tests/test_message_clean_content.py | 23 +++++++++++++++++++++++ 2 files changed, 32 insertions(+) create mode 100644 tests/test_message_clean_content.py diff --git a/disagreement/models.py b/disagreement/models.py index f525d68..b724249 100644 --- a/disagreement/models.py +++ b/disagreement/models.py @@ -6,6 +6,7 @@ Data models for Discord objects. import asyncio import json +import re from dataclasses import dataclass from typing import Any, AsyncIterator, Dict, List, Optional, TYPE_CHECKING, Union, cast @@ -116,6 +117,14 @@ class Message: # self.mention_roles: List[str] = data.get("mention_roles", []) # self.mention_everyone: bool = data.get("mention_everyone", False) + @property + def clean_content(self) -> str: + """Returns message content without user, role, or channel mentions.""" + + pattern = re.compile(r"<@!?\d+>|<#\d+>|<@&\d+>") + cleaned = pattern.sub("", self.content) + return " ".join(cleaned.split()) + async def pin(self) -> None: """|coro| diff --git a/tests/test_message_clean_content.py b/tests/test_message_clean_content.py new file mode 100644 index 0000000..83b1b91 --- /dev/null +++ b/tests/test_message_clean_content.py @@ -0,0 +1,23 @@ +import types +from disagreement.models import Message + + +def make_message(content: str) -> Message: + data = { + "id": "1", + "channel_id": "c", + "author": {"id": "2", "username": "u", "discriminator": "0001"}, + "content": content, + "timestamp": "t", + } + return Message(data, client_instance=types.SimpleNamespace()) + + +def test_clean_content_removes_mentions(): + msg = make_message("Hello <@123> <#456> <@&789> world") + assert msg.clean_content == "Hello world" + + +def test_clean_content_no_mentions(): + msg = make_message("Just text") + assert msg.clean_content == "Just text"