From a759cf2a49f20ac4ce08ad53c73fb4577236efff Mon Sep 17 00:00:00 2001 From: SeaswimmerTheFsh Date: Thu, 28 Dec 2023 18:58:41 -0500 Subject: [PATCH] feat(nerdify): introduced cog --- nerdify/__init__.py | 5 +++++ nerdify/info.json | 10 +++++++++ nerdify/nerdify.py | 54 +++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 69 insertions(+) create mode 100644 nerdify/__init__.py create mode 100644 nerdify/info.json create mode 100644 nerdify/nerdify.py diff --git a/nerdify/__init__.py b/nerdify/__init__.py new file mode 100644 index 0000000..5ebe09c --- /dev/null +++ b/nerdify/__init__.py @@ -0,0 +1,5 @@ +from .nerdify import Nerdify + + +async def setup(bot): + await bot.add_cog(Nerdify(bot)) diff --git a/nerdify/info.json b/nerdify/info.json new file mode 100644 index 0000000..b22465e --- /dev/null +++ b/nerdify/info.json @@ -0,0 +1,10 @@ +{ + "author" : ["SeaswimmerTheFsh"], + "install_msg" : "Thank you for installing Nerdify!\nYou can find the source code of this cog [here](https://coastalcommits.com/SeaswimmerTheFsh/SeaCogs).", + "name" : "Nerdify", + "short" : "Nerdify your text!", + "description" : "Nerdify your text!", + "end_user_data_statement" : "This cog does not store end user data.", + "hidden": true, + "disabled": true + } diff --git a/nerdify/nerdify.py b/nerdify/nerdify.py new file mode 100644 index 0000000..1e27d8b --- /dev/null +++ b/nerdify/nerdify.py @@ -0,0 +1,54 @@ +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!" + await type_message( + ctx.channel, + nerdify_text(text), + allowed_mentions=discord.AllowedMentions( + everyone=False, users=False, roles=False + ), + ) + +def nerdify_text(self, text: str) -> str: + """Convert text to nerd speak.""" + return f"\"{text}\" 🤓" + +async def type_message( + destination: discord.abc.Messageable, content: str, **kwargs: Any # noqa: ANN401 +) -> discord.Message | None: + """Simulate typing and sending a message to a destination. + + 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)