discordbot/cogs/owoify_cog.py
2025-05-13 10:35:34 -06:00

88 lines
4.1 KiB
Python

import discord
from discord.ext import commands
from discord import app_commands
import re
import random
def _owoify_text(text: str) -> str:
"""Improved owoification with more rules and randomness."""
# Basic substitutions
text = re.sub(r'[rl]', 'w', text)
text = re.sub(r'[RL]', 'W', text)
text = re.sub(r'n([aeiou])', r'ny\1', text)
text = re.sub(r'N([aeiou])', r'Ny\1', text)
text = re.sub(r'N([AEIOU])', r'NY\1', text)
text = re.sub(r'ove', 'uv', text)
text = re.sub(r'OVE', 'UV', text)
# Extra substitutions
text = re.sub(r'\bth', lambda m: 'd' if random.random() < 0.5 else 'f', text, flags=re.IGNORECASE)
text = re.sub(r'\bno\b', 'nu', text, flags=re.IGNORECASE)
text = re.sub(r'\bhas\b', 'haz', text, flags=re.IGNORECASE)
text = re.sub(r'\bhave\b', 'haz', text, flags=re.IGNORECASE)
text = re.sub(r'\byou\b', lambda m: 'u' if random.random() < 0.5 else 'yu', text, flags=re.IGNORECASE)
text = re.sub(r'\byour\b', 'ur', text, flags=re.IGNORECASE)
text = re.sub(r'tion\b', 'shun', text, flags=re.IGNORECASE)
# Playful punctuation
text = re.sub(r'!', lambda m: random.choice(['!!1!', '! UwU', '! owo', '!! >w<']), text)
text = re.sub(r'\?', lambda m: random.choice(['?? OwO', '? uwu', '?']), text)
text = re.sub(r'\.', lambda m: random.choice(['~', '.', ' ^w^']), text)
# Stutter (probabilistic, only for words with at least 2 letters)
def stutter_word(match):
word = match.group(0)
if len(word) > 2 and random.random() < 0.25 and word[0].isalpha():
return f"{word[0]}-{word}"
return word
text = re.sub(r'\b\w+\b', stutter_word, text)
# Random interjection insertion (after commas or randomly)
interjections = [" owo", " uwu", " >w<", " ^w^", " OwO", " UwU", " >.<", " XD", " nya~", ":3", "(^///^)", "(ᵘʷᵘ)", "(・`ω´・)", ";;w;;"]
parts = re.split(r'([,])', text)
for i in range(len(parts)):
if parts[i] == ',' or (random.random() < 0.1 and parts[i].strip()):
parts[i] += random.choice(interjections)
text = ''.join(parts)
# Suffix
text += random.choice(interjections)
return text
class OwoifyCog(commands.Cog):
def __init__(self, bot: commands.Bot):
self.bot = bot
@app_commands.command(name="owoify", description="Owoifies your message!")
@app_commands.describe(message_to_owoify="The message to owoify")
async def owoify_slash_command(self, interaction: discord.Interaction, message_to_owoify: str):
"""Owoifies the provided message via a slash command."""
if not message_to_owoify.strip():
await interaction.response.send_message("You nyeed to pwovide some text to owoify! >w<", ephemeral=True)
return
owo_text = _owoify_text(message_to_owoify)
await interaction.response.send_message(owo_text)
# Context menu command must be defined at module level
@app_commands.context_menu(name="Owoify Message")
async def owoify_context_menu(interaction: discord.Interaction, message: discord.Message):
"""Owoifies the content of the selected message and replies."""
if not message.content:
await interaction.response.send_message("The sewected message has no text content to owoify! >.<", ephemeral=True)
return
original_content = message.content
owo_text = _owoify_text(original_content)
try:
await message.reply(owo_text)
await interaction.response.send_message("Message owoified and wepwied! uwu", ephemeral=True)
except discord.Forbidden:
await interaction.response.send_message(
f"I couwdn't wepwy to the message (nyi Pwermissions? owo).\n"
f"But hewe's the owoified text fow you: {owo_text}",
ephemeral=True
)
except discord.HTTPException as e:
await interaction.response.send_message(f"Oopsie! A tiny ewwow occuwwed: {e} >w<", ephemeral=True)
async def setup(bot: commands.Bot):
cog = OwoifyCog(bot)
await bot.add_cog(cog)
# Add the context menu command to the tree
bot.tree.add_command(owoify_context_menu)
print("OwoifyCog woaded! uwu")