Skip to content
This repository has been archived by the owner on Mar 13, 2023. It is now read-only.

feat: Text Utils #700

Merged
merged 14 commits into from
Nov 1, 2022
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
1 change: 1 addition & 0 deletions naff/client/utils/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@
from .misc_utils import *
from .serializer import *
from .formatting import *
from .text_utils import *
33 changes: 33 additions & 0 deletions naff/client/utils/text_utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import re
import naff.models as models

__all__ = ("mentions",)


def mentions(
text: str,
query: "str | re.Pattern[str] | models.BaseUser | models.BaseChannel | models.Role",
*,
tag_as_mention: bool = False,
) -> bool:
"""Checks whether a query is present in a text.

Args:
text: The text to search in
query: The query to search for
tag_as_mention: Should `BaseUser.tag` be checked *(only if query is an instance of BaseUser)*

Returns:
Whether the query could be found in the text
"""
if isinstance(query, str):
return query in text
elif isinstance(query, re.Pattern):
return query.match(text) is not None
elif isinstance(query, models.BaseUser):
# mentions with <@!ID> aren't detected without the replacement
return (query.mention in text.replace("@!", "@")) or (query.tag in text if tag_as_mention else False)
elif isinstance(query, (models.BaseChannel, models.Role)):
return query.mention in text
else:
return False
19 changes: 19 additions & 0 deletions naff/models/discord/message.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
from naff.client.utils.attr_converters import optional as optional_c
from naff.client.utils.attr_converters import timestamp_converter
from naff.client.utils.serializer import dict_filter_none
from naff.client.utils.text_utils import mentions
from naff.models.discord.channel import BaseChannel
from naff.models.discord.file import UPLOADABLE_TYPE
from .base import DiscordObject
Expand Down Expand Up @@ -380,6 +381,24 @@ def get_referenced_message(self) -> Optional["Message"]:
return None
return self._client.cache.get_message(self._channel_id, self._referenced_message_id)

def contains_mention(
self,
query: "str | re.Pattern[str] | models.BaseUser | models.BaseChannel | models.Role",
*,
tag_as_mention: bool = False,
) -> bool:
"""
Check whether the message contains the query or not.

Args:
query: The query to search for
tag_as_mention: Should `BaseUser.tag` be checked *(only if query is an instance of BaseUser)*

Returns:
A boolean indicating whether the query could be found or not
"""
return mentions(text=self.content or self.system_content, query=query, tag_as_mention=tag_as_mention)

@classmethod
def _process_dict(cls, data: dict, client: "Client") -> dict:
if author_data := data.pop("author", None):
Expand Down