Add clean_content property and tests (#56)
This commit is contained in:
parent
64dec9b3f5
commit
35eb459c36
@ -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|
|
||||||
|
|
||||||
|
23
tests/test_message_clean_content.py
Normal file
23
tests/test_message_clean_content.py
Normal 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"
|
Loading…
x
Reference in New Issue
Block a user