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

feat: filter more kinds of non-emoji unicode characters #720

Merged
merged 1 commit into from
Nov 13, 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
6 changes: 4 additions & 2 deletions naff/models/discord/emoji.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import re
import string
import unicodedata
from typing import TYPE_CHECKING, Any, Dict, List, Optional, Union

import attrs
Expand All @@ -22,7 +23,7 @@
__all__ = ("PartialEmoji", "CustomEmoji", "process_emoji_req_format", "process_emoji")

emoji_regex = re.compile(r"<?(a)?:(\w*):(\d*)>?")
unicode_emoji_reg = re.compile(r"[^\w\s,]")
unicode_emoji_reg = re.compile(r"[^\w\s,’‘“”…–—•◦‣⁃⁎⁏⁒⁓⁺⁻⁼⁽⁾ⁿ₊₋₌₍₎]")


@attrs.define(eq=False, order=False, hash=False, kw_only=False)
Expand Down Expand Up @@ -87,7 +88,8 @@ def from_str(cls, emoji_str: str, *, language: str = "alias") -> Optional["Parti

# the regex will match certain special characters, so this acts as a final failsafe
if match not in string.printable:
return cls(name=match)
if unicodedata.category(match) == "So":
return cls(name=match)
return None

def __str__(self) -> str:
Expand Down
31 changes: 31 additions & 0 deletions tests/test_emoji.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,3 +107,34 @@ def test_numerical_emoji() -> None:
def test_false_positives() -> None:
for _e in string.printable:
assert PartialEmoji.from_str(_e) is None

unicode_general_punctuation = [
"’",
"‘",
"“",
"”",
"…",
"–",
"—",
"•",
"◦",
"‣",
"⁃",
"⁎",
"⁏",
"⁒",
"⁓",
"⁺",
"⁻",
"⁼",
"⁽",
"⁾",
"ⁿ",
"₊",
"₋",
"₌",
"₍",
"₎",
]
for _e in unicode_general_punctuation:
assert PartialEmoji.from_str(_e) is None