discordbot/cogs/owoify_cog.py
2025-05-13 10:20:26 -06:00

66 lines
2.9 KiB
Python

import discord
from discord.ext import commands
from discord import app_commands
import re
import random
class OwoifyCog(commands.Cog):
def __init__(self, bot: commands.Bot):
self.bot = bot
def _owoify_text(self, text: str) -> str:
"""Helper function to owoify text."""
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)
# Add some cute faces/suffixes
suffixes = [" owo", " uwu", " >w<", " ^w^", " OwO", " UwU", " >.<"] # Escaped > and < for XML
text += random.choice(suffixes)
return text
@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 = self._owoify_text(message_to_owoify)
await interaction.response.send_message(owo_text)
@app_commands.context_menu(name="Owoify Message")
async def owoify_context_menu(self, 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 = self._owoify_text(original_content)
try:
# Attempt to reply to the message
await message.reply(owo_text)
# Send a silent confirmation to the user who invoked the command
await interaction.response.send_message("Message owoified and wepwied! uwu", ephemeral=True)
except discord.Forbidden:
# If replying is forbidden, send the owoified text as an ephemeral message to the user
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:
# Handle other potential HTTP errors during reply
await interaction.response.send_message(f"Oopsie! A tiny ewwow occuwwed: {e} >w<", ephemeral=True)
async def setup(bot: commands.Bot):
await bot.add_cog(OwoifyCog(bot))
print("OwoifyCog woaded! uwu")