diff options
author | 2022-09-18 22:03:34 +0100 | |
---|---|---|
committer | 2022-09-18 22:03:34 +0100 | |
commit | 46d1e8ddb217f1bb5e07179b32db50b6a04b6de8 (patch) | |
tree | c3f4db63c2751c51acfee97d016551b8f677e29b /bot/exts/fun/fun.py | |
parent | Remove unnecessary hasattr check (diff) | |
parent | Fix Poetry 1.2 Support (#1099) (diff) |
Merge branch 'main' into fix-whitelist-inheritance
Diffstat (limited to 'bot/exts/fun/fun.py')
-rw-r--r-- | bot/exts/fun/fun.py | 116 |
1 files changed, 14 insertions, 102 deletions
diff --git a/bot/exts/fun/fun.py b/bot/exts/fun/fun.py index b148f1f3..e7337cb6 100644 --- a/bot/exts/fun/fun.py +++ b/bot/exts/fun/fun.py @@ -1,35 +1,21 @@ -import functools import json import logging import random from collections.abc import Iterable from pathlib import Path -from typing import Callable, Optional, Union +from typing import Literal -from discord import Embed, Message +import pyjokes +from discord import Embed from discord.ext import commands -from discord.ext.commands import BadArgument, Cog, Context, MessageConverter, clean_content +from discord.ext.commands import BadArgument, Cog, Context, clean_content -from bot import utils from bot.bot import Bot from bot.constants import Client, Colours, Emojis -from bot.utils import helpers +from bot.utils import helpers, messages log = logging.getLogger(__name__) -UWU_WORDS = { - "fi": "fwi", - "l": "w", - "r": "w", - "some": "sum", - "th": "d", - "thing": "fing", - "tho": "fo", - "you're": "yuw'we", - "your": "yur", - "you": "yuw", -} - def caesar_cipher(text: str, offset: int) -> Iterable[str]: """ @@ -56,7 +42,6 @@ class Fun(Cog): def __init__(self, bot: Bot): self.bot = bot - self._caesar_cipher_embed = json.loads(Path("bot/resources/fun/caesar_info.json").read_text("UTF-8")) @staticmethod @@ -74,23 +59,6 @@ class Fun(Cog): else: raise BadArgument(f"`{Client.prefix}roll` only supports between 1 and 6 rolls.") - @commands.command(name="uwu", aliases=("uwuwize", "uwuify",)) - async def uwu_command(self, ctx: Context, *, text: clean_content(fix_channel_mentions=True)) -> None: - """Converts a given `text` into it's uwu equivalent.""" - conversion_func = functools.partial( - utils.replace_many, replacements=UWU_WORDS, ignore_case=True, match_case=True - ) - text, embed = await Fun._get_text_and_embed(ctx, text) - # Convert embed if it exists - if embed is not None: - embed = Fun._convert_embed(conversion_func, embed) - converted_text = conversion_func(text) - converted_text = helpers.suppress_links(converted_text) - # Don't put >>> if only embed present - if converted_text: - converted_text = f">>> {converted_text.lstrip('> ')}" - await ctx.send(content=converted_text, embed=embed) - @commands.command(name="randomcase", aliases=("rcase", "randomcaps", "rcaps",)) async def randomcase_command(self, ctx: Context, *, text: clean_content(fix_channel_mentions=True)) -> None: """Randomly converts the casing of a given `text`.""" @@ -99,10 +67,10 @@ class Fun(Cog): return "".join( char.upper() if round(random.random()) else char.lower() for char in text ) - text, embed = await Fun._get_text_and_embed(ctx, text) + text, embed = await messages.get_text_and_embed(ctx, text) # Convert embed if it exists if embed is not None: - embed = Fun._convert_embed(conversion_func, embed) + embed = messages.convert_embed(conversion_func, embed) converted_text = conversion_func(text) converted_text = helpers.suppress_links(converted_text) # Don't put >>> if only embed present @@ -148,10 +116,10 @@ class Fun(Cog): """Encrypts the given string using the Caesar Cipher.""" return "".join(caesar_cipher(text, offset)) - text, embed = await Fun._get_text_and_embed(ctx, msg) + text, embed = await messages.get_text_and_embed(ctx, msg) if embed is not None: - embed = Fun._convert_embed(conversion_func, embed) + embed = messages.convert_embed(conversion_func, embed) converted_text = conversion_func(text) @@ -182,67 +150,11 @@ class Fun(Cog): """ await self._caesar_cipher(ctx, offset, msg, left_shift=True) - @staticmethod - async def _get_text_and_embed(ctx: Context, text: str) -> tuple[str, Optional[Embed]]: - """ - Attempts to extract the text and embed from a possible link to a discord Message. - - Does not retrieve the text and embed from the Message if it is in a channel the user does - not have read permissions in. - - Returns a tuple of: - str: If `text` is a valid discord Message, the contents of the message, else `text`. - Optional[Embed]: The embed if found in the valid Message, else None - """ - embed = None - - msg = await Fun._get_discord_message(ctx, text) - # Ensure the user has read permissions for the channel the message is in - if isinstance(msg, Message): - permissions = msg.channel.permissions_for(ctx.author) - if permissions.read_messages: - text = msg.clean_content - # Take first embed because we can't send multiple embeds - if msg.embeds: - embed = msg.embeds[0] - - return (text, embed) - - @staticmethod - async def _get_discord_message(ctx: Context, text: str) -> Union[Message, str]: - """ - Attempts to convert a given `text` to a discord Message object and return it. - - Conversion will succeed if given a discord Message ID or link. - Returns `text` if the conversion fails. - """ - try: - text = await MessageConverter().convert(ctx, text) - except commands.BadArgument: - log.debug(f"Input '{text:.20}...' is not a valid Discord Message") - return text - - @staticmethod - def _convert_embed(func: Callable[[str, ], str], embed: Embed) -> Embed: - """ - Converts the text in an embed using a given conversion function, then return the embed. - - Only modifies the following fields: title, description, footer, fields - """ - embed_dict = embed.to_dict() - - embed_dict["title"] = func(embed_dict.get("title", "")) - embed_dict["description"] = func(embed_dict.get("description", "")) - - if "footer" in embed_dict: - embed_dict["footer"]["text"] = func(embed_dict["footer"].get("text", "")) - - if "fields" in embed_dict: - for field in embed_dict["fields"]: - field["name"] = func(field.get("name", "")) - field["value"] = func(field.get("value", "")) - - return Embed.from_dict(embed_dict) + @commands.command() + async def joke(self, ctx: commands.Context, category: Literal["neutral", "chuck", "all"] = "all") -> None: + """Retrieves a joke of the specified `category` from the pyjokes api.""" + joke = pyjokes.get_joke(category=category) + await ctx.send(joke) def setup(bot: Bot) -> None: |