aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorGravatar Gintautas Kazlauskas <[email protected]>2021-10-02 14:08:17 +0300
committerGravatar Gintautas Kazlauskas <[email protected]>2021-10-02 14:08:17 +0300
commite5d86a18c57852d2a9058510921c53cdf9d1267d (patch)
treed7017ce924c4445c3c8836c7135a92a213a36e03
parentUpdate bot/exts/fun/uwu.py (diff)
Refactored cog.
-rw-r--r--bot/exts/fun/uwu.py123
1 files changed, 57 insertions, 66 deletions
diff --git a/bot/exts/fun/uwu.py b/bot/exts/fun/uwu.py
index 7ea96ff2..ac36deec 100644
--- a/bot/exts/fun/uwu.py
+++ b/bot/exts/fun/uwu.py
@@ -44,70 +44,15 @@ EMOJI_LUT = [
"^^;;",
]
-word_replace_regex = re.compile(r"(?<![w])[lr](?![w])")
+REGEX_WORD_REPLACE = re.compile(r"(?<![w])[lr](?![w])")
-punctuation_regex = re.compile(r"[.!?\r\n\t]")
+REGEX_PUNCTUATION = re.compile(r"[.!?\r\n\t]")
-stutter_regex = re.compile(r"(\s)([a-zA-Z])")
-stutter_substitute = "\\g<1>\\g<2>-\\g<2>"
+REGEX_STUTTER = re.compile(r"(\s)([a-zA-Z])")
+SUBSTITUTE_STUTTER = r"\g<1>\g<2>-\g<2>"
-nya_regex = re.compile(r"n([aeou])([^aeiou])")
-nya_substitute = "ny\\g<1>\\g<2>"
-
-
-def _word_replace(input_string: str) -> str:
- """Replaces words that are keys in the word replacement hash to the values specified."""
- for word in WORD_REPLACE_HASH:
- input_string = input_string.replace(word, WORD_REPLACE_HASH[word])
- return input_string
-
-
-def _char_replace(input_string: str) -> str:
- """Replaces characters with 'w'."""
- return word_replace_regex.sub("w", input_string)
-
-
-def _stutter(strength: float, input_string: str) -> str:
- """Adds stuttering to a string."""
- return stutter_regex.sub(partial(_stutter_replace, strength=strength), input_string, 0)
-
-
-def _stutter_replace(match: Callable, strength: float = 0.0) -> str:
- """Replaces a single character with a stuttered character."""
- match_string = match.string[slice(*match.span())]
- if random.random() < strength:
- char = match_string[-1]
- return f"{match_string}-{char}"
- return match_string
-
-
-def _nyaify(input_string: str) -> str:
- """Nyaifies a string."""
- return nya_regex.sub(nya_substitute, input_string, 0)
-
-
-def _emoji(strength: float, input_string: str) -> str:
- """Replaces some punctuation with emoticons."""
- return punctuation_regex.sub(partial(_emoji_replace, strength=strength), input_string, 0)
-
-
-def _emoji_replace(match: Callable, strength: float = 0.0) -> str:
- """Replaces a punctuation character with an emoticon."""
- match_string = match.string[slice(*match.span())]
- if random.random() < strength:
- return f" {EMOJI_LUT[random.randint(0, len(EMOJI_LUT) - 1)]} "
- return match_string
-
-
-def _uwuify(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()
- input_string = _word_replace(input_string)
- input_string = _nyaify(input_string)
- input_string = _char_replace(input_string)
- input_string = _stutter(stutter_strength, input_string)
- input_string = _emoji(emoji_strength, input_string)
- return input_string
+REGEX_NYA = re.compile(r"n([aeou])([^aeiou])")
+SUBSTITUTE_NYA = r"ny\g<1>\g<2>"
class Uwu(Cog):
@@ -116,10 +61,57 @@ class Uwu(Cog):
def __init__(self, bot: Bot):
self.bot = bot
+ def _word_replace(self, input_string: str) -> str:
+ """Replaces words that are keys in the word replacement hash to the values specified."""
+ for word in WORD_REPLACE_HASH:
+ input_string = input_string.replace(word, WORD_REPLACE_HASH[word])
+ return input_string
+
+ def _char_replace(self, input_string: str) -> str:
+ """Replaces characters with 'w'."""
+ return REGEX_WORD_REPLACE.sub("w", input_string)
+
+ def _stutter(self, strength: float, input_string: str) -> str:
+ """Adds stuttering to a string."""
+ return REGEX_STUTTER.sub(partial(self._stutter_replace, strength=strength), input_string, 0)
+
+ def _stutter_replace(self, match: Callable, strength: float = 0.0) -> str:
+ """Replaces a single character with a stuttered character."""
+ match_string = match.string[slice(*match.span())]
+ if random.random() < strength:
+ char = match_string[-1]
+ return f"{match_string}-{char}"
+ return match_string
+
+ def _nyaify(self, input_string: str) -> str:
+ """Nyaifies a string."""
+ return REGEX_NYA.sub(SUBSTITUTE_NYA, input_string, 0)
+
+ def _emoji(self, strength: float, input_string: str) -> str:
+ """Replaces some punctuation with emoticons."""
+ return REGEX_PUNCTUATION.sub(partial(self._emoji_replace, strength=strength), input_string, 0)
+
+ def _emoji_replace(self, match: Callable, strength: float = 0.0) -> str:
+ """Replaces a punctuation character with an emoticon."""
+ match_string = match.string[slice(*match.span())]
+ if random.random() < strength:
+ return f" {EMOJI_LUT[random.randint(0, len(EMOJI_LUT) - 1)]} "
+ return match_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()
+ input_string = self._word_replace(input_string)
+ input_string = self._nyaify(input_string)
+ input_string = self._char_replace(input_string)
+ input_string = self._stutter(stutter_strength, input_string)
+ input_string = self._emoji(emoji_strength, input_string)
+ return input_string
+
@commands.command(name="uwu", aliases=("uwuwize", "uwuify",))
async def uwu_command(self, ctx: Context, *, text: clean_content(fix_channel_mentions=True)) -> None:
"""
- Echo an uwuified version of the passed text.
+ Echoes an uwuified version of a message.
Example:
'.uwu Hello, my name is John' returns something like
@@ -129,13 +121,12 @@ class Uwu(Cog):
# Grabs the text from the embed for uwuification.
if embed is not None:
- embed = Fun._convert_embed(_uwuify, embed)
- converted_text = _uwuify(text)
+ embed = Fun._convert_embed(self._uwuify, embed)
+ converted_text = self._uwuify(text)
converted_text = helpers.suppress_links(converted_text)
# Adds the text harvested from an embed to be put into another quote block.
- if converted_text:
- converted_text = f">>> {converted_text.lstrip('> ')}"
+ converted_text = f">>> {converted_text.lstrip('> ')}"
await ctx.send(content=converted_text, embed=embed)