2023-12-28 19:07:42 -05:00
|
|
|
# _____ _
|
|
|
|
# / ____| (_)
|
|
|
|
# | (___ ___ __ _ _____ ___ _ __ ___ _ __ ___ ___ _ __
|
|
|
|
# \___ \ / _ \/ _` / __\ \ /\ / / | '_ ` _ \| '_ ` _ \ / _ \ '__|
|
|
|
|
# ____) | __/ (_| \__ \\ V V /| | | | | | | | | | | | __/ |
|
|
|
|
# |_____/ \___|\__,_|___/ \_/\_/ |_|_| |_| |_|_| |_| |_|\___|_|
|
|
|
|
|
2023-12-28 18:58:41 -05:00
|
|
|
import asyncio
|
|
|
|
from contextlib import suppress
|
2024-01-16 13:07:20 -05:00
|
|
|
from typing import Any, Optional, Union
|
2023-12-28 18:58:41 -05:00
|
|
|
|
|
|
|
import discord
|
|
|
|
from redbot.core import commands
|
2024-01-05 04:20:48 -05:00
|
|
|
from redbot.core.utils import chat_formatting, common_filters
|
2023-12-28 18:58:41 -05:00
|
|
|
|
|
|
|
|
|
|
|
class Nerdify(commands.Cog):
|
|
|
|
"""Nerdify your text."""
|
|
|
|
|
2024-03-07 03:38:34 -05:00
|
|
|
__author__ = ["SeaswimmerTheFsh"]
|
2024-03-29 07:18:22 -04:00
|
|
|
__version__ = "1.3.4"
|
|
|
|
__documentation__ = "https://seacogs.coastalcommits.com/nerdify/"
|
2024-01-07 09:09:07 -05:00
|
|
|
|
|
|
|
def __init__(self, bot):
|
|
|
|
self.bot = bot
|
2024-01-07 09:02:32 -05:00
|
|
|
|
2024-03-07 03:38:34 -05:00
|
|
|
def format_help_for_context(self, ctx: commands.Context) -> str:
|
|
|
|
pre_processed = super().format_help_for_context(ctx) or ""
|
|
|
|
n = "\n" if "\n\n" not in pre_processed else ""
|
|
|
|
text = [
|
|
|
|
f"{pre_processed}{n}",
|
|
|
|
f"Cog Version: **{self.__version__}**",
|
|
|
|
f"Author: {chat_formatting.humanize_list(self.__author__)}",
|
2024-03-29 07:18:22 -04:00
|
|
|
f"Documentation: {self.__documentation__}"
|
2024-03-07 03:38:34 -05:00
|
|
|
]
|
|
|
|
return "\n".join(text)
|
|
|
|
|
2023-12-28 18:58:41 -05:00
|
|
|
@commands.command(aliases=["nerd"])
|
2024-02-02 11:22:08 -05:00
|
|
|
async def nerdify(
|
|
|
|
self, ctx: commands.Context, *, text: Optional[str] = None
|
|
|
|
) -> None:
|
2024-01-04 21:57:54 -05:00
|
|
|
"""Nerdify the replied to message, previous message, or your own text."""
|
2023-12-28 18:58:41 -05:00
|
|
|
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
|
2024-01-05 04:34:46 -05:00
|
|
|
text = messages[1].content
|
2024-01-05 04:36:29 -05:00
|
|
|
if text == "":
|
|
|
|
await ctx.send(chat_formatting.error("I can't translate that!"))
|
|
|
|
return
|
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:
|
2024-01-04 18:03:34 -05:00
|
|
|
"""Convert text to nerd speak.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
text: The text to convert.
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
The converted text."""
|
2024-02-02 11:22:08 -05:00
|
|
|
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
|
2024-01-16 13:07:20 -05:00
|
|
|
) -> Union[discord.Message, None]:
|
2023-12-28 19:01:34 -05:00
|
|
|
"""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.
|
2024-01-04 18:03:34 -05:00
|
|
|
|
|
|
|
Args:
|
|
|
|
destination: The [destination](https://discordpy.readthedocs.io/en/stable/api.html#discord.abc.Messageable) to send the message to.
|
|
|
|
content: The content of the message to send.
|
|
|
|
**kwargs: Any keyword arguments to pass to the [destination.send](https://discordpy.readthedocs.io/en/stable/api.html#discord.TextChannel.send) function.
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
The message sent, or None if an error occurred.
|
2023-12-28 19:01:34 -05:00
|
|
|
"""
|
|
|
|
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)
|