Add clean_content property and tests (#56)

This commit is contained in:
Slipstream 2025-06-11 14:26:55 -06:00 committed by GitHub
parent 64dec9b3f5
commit 35eb459c36
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 32 additions and 0 deletions

View File

@ -6,6 +6,7 @@ Data models for Discord objects.
import asyncio import asyncio
import json import json
import re
from dataclasses import dataclass from dataclasses import dataclass
from typing import Any, AsyncIterator, Dict, List, Optional, TYPE_CHECKING, Union, cast 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_roles: List[str] = data.get("mention_roles", [])
# self.mention_everyone: bool = data.get("mention_everyone", False) # 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: async def pin(self) -> None:
"""|coro| """|coro|

View File

@ -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"