diff options
| author | 2022-08-08 11:09:44 -0400 | |
|---|---|---|
| committer | 2022-08-08 11:09:44 -0400 | |
| commit | 765388d00f4d6f9dda7a464a518d76745a3256e1 (patch) | |
| tree | d9f1ffbedf9fbe2d2af27680c77dfdf6c6b1966b /bot/exts/fun/uwu.py | |
| parent | Fixed conflicts from PR 1078 (diff) | |
| parent | Merge pull request #1083 from algmyr/latex (diff) | |
Pulled changes from main and fixed conflicts
Diffstat (limited to 'bot/exts/fun/uwu.py')
| -rw-r--r-- | bot/exts/fun/uwu.py | 50 |
1 files changed, 49 insertions, 1 deletions
diff --git a/bot/exts/fun/uwu.py b/bot/exts/fun/uwu.py index 0d650de0..0d75b0e2 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 @@ -46,7 +47,7 @@ EMOJIS = [ "^^;;", ] -REGEX_WORD_REPLACE = re.compile(r"(?<![w])[lr](?![w])") +REGEX_WORD_REPLACE = re.compile(r"(?<!w)[lr](?!w)") REGEX_PUNCTUATION = re.compile(r"[.!?\r\n\t]") @@ -56,6 +57,32 @@ SUBSTITUTE_STUTTER = r"\g<1>\g<2>-\g<2>" REGEX_NYA = re.compile(r"n([aeou][^aeiou])") SUBSTITUTE_NYA = r"ny\1" +REGEX_EMOJI = re.compile(r"<(a)?:(\w+?):(\d{15,21}?)>", re.ASCII) + + +@dataclass(frozen=True, eq=True) +class Emoji: + """Data class for an 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].isdecimal(): + return None + return cls(match[1], int(match[2]), match[0] == "a") + class Uwu(Cog): """Cog for the uwu command.""" @@ -99,6 +126,26 @@ class Uwu(Cog): return f" {random.choice(EMOJIS)} " return match_string + def _ext_emoji_replace(self, input_string: str) -> str: + """Replaces any emoji the bot cannot send in input_text with a random emoticons.""" + groups = REGEX_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 +154,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",)) |