diff options
| -rw-r--r-- | bot/exts/fun/uwu.py | 46 | 
1 files changed, 46 insertions, 0 deletions
| diff --git a/bot/exts/fun/uwu.py b/bot/exts/fun/uwu.py index 26c5c741..053e64cd 100644 --- a/bot/exts/fun/uwu.py +++ b/bot/exts/fun/uwu.py @@ -1,6 +1,7 @@  import random  import re  import typing as t +from dataclasses import dataclass  from functools import partial  import discord @@ -56,6 +57,30 @@ SUBSTITUTE_STUTTER = r"\g<1>\g<2>-\g<2>"  REGEX_NYA = re.compile(r"n([aeou][^aeiou])")  SUBSTITUTE_NYA = r"ny\1" +RE_EMOJI = re.compile(r"<(a?)?:(\w+):(\d{18})>?") + + +@dataclass(frozen=True, eq=True) +class Emoji: +    name: str +    uid: int +    animated: bool = False + +    def __str__(self): +        anim_bit = "a" if self.animated else "" +        return f"<{anim_bit}:{self.name}:{self.uid}>" + +    def can_display(self, bot: Bot) -> bool: +        """Determines if a bot is in a server with the emoji.""" +        return bot.get_emoji(self.uid) is not None + +    @classmethod +    def from_match(cls, match: tuple[str, str, str]) -> t.Optional['Emoji']: +        """Creates an Emoji from a regex match tuple.""" +        if not match or len(match) != 3 or not match[2].isdigit(): +            return None +        return cls(match[1], int(match[2]), match[0] == "a") +  class Uwu(Cog):      """Cog for the uwu command.""" @@ -99,6 +124,26 @@ class Uwu(Cog):              return f" {random.choice(EMOJIS)} "          return match_string +    def _ext_emoji_replace(self, input_string: str) -> str: +        """Replaces external emojis with emoticons""" +        groups = RE_EMOJI.findall(input_string) +        emojis = {Emoji.from_match(match) for match in groups} +        # Replace with random emoticon if unable to display +        emojis_map = { +            re.escape(str(e)): random.choice(EMOJIS) +            for e in emojis if e and not e.can_display(self.bot) +        } +        if emojis_map: +            # Pattern for all emoji markdowns to be replaced +            emojis_re = re.compile("|".join(emojis_map.keys())) +            # Replace matches with random emoticon +            return emojis_re.sub( +                lambda m: emojis_map[re.escape(m.group())], +                input_string +            ) +        # Return original if no replacement +        return input_string +      def _uwuify(self, input_string: str, *, stutter_strength: float = 0.2, emoji_strength: float = 0.1) -> str:          """Takes a string and returns an uwuified version of it."""          input_string = input_string.lower() @@ -107,6 +152,7 @@ class Uwu(Cog):          input_string = self._char_replace(input_string)          input_string = self._stutter(stutter_strength, input_string)          input_string = self._emoji(emoji_strength, input_string) +        input_string = self._ext_emoji_replace(input_string)          return input_string      @commands.command(name="uwu", aliases=("uwuwize", "uwuify",)) | 
