SeaCogs/nerdify/nerdify.py

55 lines
2.1 KiB
Python
Raw Normal View History

2023-12-28 18:58:41 -05:00
import asyncio
from contextlib import suppress
from typing import Any, Optional
import discord
from redbot.core import commands
from redbot.core.utils import common_filters
class Nerdify(commands.Cog):
"""Nerdify your text."""
def __init__(self, bot):
self.bot = bot
@commands.command(aliases=["nerd"])
async def nerdify(self, ctx: commands.Context, *, text: Optional[str] = None) -> None:
"""Uwuize the replied to message, previous message, or your own text."""
if not text:
if hasattr(ctx.message, "reference") and ctx.message.reference:
with suppress(
discord.Forbidden, discord.NotFound, discord.HTTPException
):
message_id = ctx.message.reference.message_id
if message_id:
text = (await ctx.fetch_message(message_id)).content
if not text:
messages = [message async for message in ctx.channel.history(limit=2)]
# [0] is the command, [1] is the message before the command
text = messages[1].content or "I can't translate that!"
2023-12-28 19:01:34 -05:00
await self.type_message(
2023-12-28 18:58:41 -05:00
ctx.channel,
2023-12-28 19:01:34 -05:00
self.nerdify_text(text),
2023-12-28 18:58:41 -05:00
allowed_mentions=discord.AllowedMentions(
everyone=False, users=False, roles=False
),
)
2023-12-28 19:01:34 -05:00
def nerdify_text(self, text: str) -> str:
"""Convert text to nerd speak."""
return f"\"{text}\" 🤓"
2023-12-28 18:58:41 -05:00
2023-12-28 19:01:34 -05:00
async def type_message(
2023-12-28 19:04:19 -05:00
self, destination: discord.abc.Messageable, content: str, **kwargs: Any
2023-12-28 19:01:34 -05:00
) -> discord.Message | None:
"""Simulate typing and sending a message to a destination.
2023-12-28 18:58:41 -05:00
2023-12-28 19:01:34 -05:00
Will send a typing indicator, wait a variable amount of time based on the length
of the text (to simulate typing speed), then send the message.
"""
content = common_filters.filter_urls(content)
with suppress(discord.HTTPException):
async with destination.typing():
await asyncio.sleep(max(0.25, min(2.5, len(content) * 0.01)))
return await destination.send(content=content, **kwargs)