aboutsummaryrefslogtreecommitdiffstats
path: root/bot/exts/fun/uwu.py
blob: ce2ed45d1fce1ea3f0eb810938ce2693793b83ac (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
import random
import re
from functools import partial
from typing import Callable

from discord.ext import commands
from discord.ext.commands import Cog, Context, clean_content

from bot.bot import Bot
from bot.exts.fun.fun import Fun
from bot.utils import helpers


# Wepwacement
WEPWACE_HASH = {
    "small": "smol",
    "cute": "kawaii~",
    "fluff": "floof",
    "love": "luv",
    "stupid": "baka",
    "idiot": "baka",
    "what": "nani",
    "meow": "nya~",
    "roar": "rawrr~",
}

EMOJI_LUT = [
    "rawr x3",
    "OwO",
    "UwU",
    "o.O",
    "-.-",
    ">w<",
    "(⑅˘꒳˘)",
    "(ꈍᴗꈍ)",
    "(˘ω˘)",
    "(U ᵕ U❁)",
    "σωσ",
    "òωó",
    "(///ˬ///✿)",
    "(U ﹏ U)",
    "( ͡o ω ͡o )",
    "ʘwʘ",
    ":3",
    ":3",  # important enough to have twice
    "XD",
    "nyaa~~",
    "mya",
    ">_<",
    "😳",
    "🥺",
    "😳😳😳",
    "rawr",
    "uwu",
    "^^",
    "^^;;",
    "(ˆ ﻌ ˆ)♡",
    "^•ﻌ•^",
    "/(^•ω•^)",
    "(✿oωo)",
]

wepwace_regex = re.compile(r"(?<![w])[lr](?![w])")


def _word_replace(input_string: str) -> str:
    for word in WEPWACE_HASH:
        input_string = input_string.replace(word, WEPWACE_HASH[word])

    return input_string


def _char_replace(input_string: str) -> str:
    return wepwace_regex.sub("w", input_string)


# Stuttering
stutter_regex = re.compile(r"(\s)([a-zA-Z])")
stutter_subst = "\\g<1>\\g<2>-\\g<2>"


def _stutter(strength: float, input_string: str) -> str:
    return stutter_regex.sub(partial(_stutter_replace, strength=strength), input_string, 0)


def _stutter_replace(match: Callable, strength: float = 0.0) -> str:
    match_string = match.string[slice(*match.span())]
    if random.random() < strength:
        char = match_string[-1]
        return f"{match_string}-{char}"
    return match_string


# Nyaification
nya_regex = re.compile(r"n([aeou])([^aeiou])")
nya_subst = "ny\\g<1>\\g<2>"


def _nyaify(input_string: str) -> str:
    return nya_regex.sub(nya_subst, input_string, 0)


# Emoji
punctuation_regex = re.compile(r"\s+")


def _emoji(strength: float, input_string: str) -> str:
    return punctuation_regex.sub(partial(_emoji_replace, strength=strength), input_string, 0)


def _emoji_replace(match: Callable, strength: float = 0.0) -> str:
    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


# Main

def _uwuify(input_string: str, *, stutter_strength: float = 0.2, emoji_strength: float = 0.2) -> str:
    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


class Uwu(Cog):
    """Cog for uwuification."""

    def __init__(self, bot: Bot):
        self.bot = bot

    @commands.command(name="uwu", aliases=("uwuwize", "uwuify",))
    async def uwu_command(self, ctx: Context, *, text: clean_content(fix_channel_mentions=True)) -> None:
        """Converts a given `text` into it's uwu equivalent."""
        text, embed = await Fun._get_text_and_embed(ctx, text)
        # Convert embed if it exists
        if embed is not None:
            embed = Fun._convert_embed(_uwuify, embed)
        converted_text = _uwuify(text)
        converted_text = helpers.suppress_links(converted_text)
        # Don't put >>> if only embed present
        if converted_text:
            converted_text = f">>> {converted_text.lstrip('> ')}"
        await ctx.send(content=converted_text, embed=embed)


def setup(bot: Bot) -> None:
    """Load the uwu cog."""
    bot.add_cog(Uwu(bot))