Add Mock back in

pull/156/head
stijndcl 2022-11-21 22:46:19 +01:00
parent 3bc9c15af7
commit accda93461
2 changed files with 49 additions and 1 deletions

View File

@ -13,6 +13,7 @@ from didier.data.apis.xkcd import fetch_xkcd_post
from didier.exceptions.no_match import expect from didier.exceptions.no_match import expect
from didier.menus.memes import MemeSource from didier.menus.memes import MemeSource
from didier.utils.discord import constants from didier.utils.discord import constants
from didier.utils.types.string import mock
from didier.views.modals import GenerateMeme from didier.views.modals import GenerateMeme
@ -133,6 +134,30 @@ class Fun(commands.Cog):
"""Autocompletion for the 'template'-parameter""" """Autocompletion for the 'template'-parameter"""
return self.client.database_caches.memes.get_autocomplete_suggestions(current) return self.client.database_caches.memes.get_autocomplete_suggestions(current)
@app_commands.command()
@app_commands.describe(message="The text to convert.")
async def mock(self, interaction: discord.Interaction, message: str):
"""Mock a message.
This returns the mocked version ephemerally so that you can copy-paste it easily,
instead of the old version where Didier would send a public message.
The mocked message escapes all Markdown syntax.
"""
await interaction.response.defer(ephemeral=True)
# Nitro users can send longer messages that Didier can't repeat back to them
if len(message) > constants.Limits.MESSAGE_LENGTH:
return await interaction.followup.send("That message is too long.")
mocked_message = mock(discord.utils.escape_markdown(message))
# Escaping md syntax can make the message longer than the limit
if len(mocked_message) > constants.Limits.MESSAGE_LENGTH:
return await interaction.followup.send("Because of Markdown syntax escaping, that message is too long.")
return await interaction.followup.send(mocked_message)
@commands.hybrid_command(name="xkcd") @commands.hybrid_command(name="xkcd")
@app_commands.rename(comic_id="id") @app_commands.rename(comic_id="id")
async def xkcd(self, ctx: commands.Context, comic_id: Optional[int] = None): async def xkcd(self, ctx: commands.Context, comic_id: Optional[int] = None):

View File

@ -1,8 +1,9 @@
import math import math
import random
import re import re
from typing import Optional, Union from typing import Optional, Union
__all__ = ["abbreviate", "leading", "pluralize", "re_find_all", "re_replace_with_list", "get_edu_year_name"] __all__ = ["abbreviate", "leading", "mock", "pluralize", "re_find_all", "re_replace_with_list", "get_edu_year_name"]
def abbreviate(text: str, max_length: int) -> str: def abbreviate(text: str, max_length: int) -> str:
@ -38,6 +39,28 @@ def leading(character: str, string: str, target_length: Optional[int] = 2) -> st
return (frequency * character) + string return (frequency * character) + string
def mock(string: str) -> str:
"""Mock an input string
The result of this is comparable to the Mocking Spongebob memes
"""
replacements = {"a": "4", "b": "8", "e": "3", "i": "1", "o": "0", "s": "5"}
result_string = ""
for letter in string.lower():
# Letter should be mocked
if letter.isalpha() and random.random() < 0.5:
# Use replacement if it exists
if letter in replacements and random.random() < 0.5:
result_string += replacements[letter]
else:
result_string += letter.upper()
else:
result_string += letter
return result_string
def pluralize(word: str, amount: int, plural_form: Optional[str] = None) -> str: def pluralize(word: str, amount: int, plural_form: Optional[str] = None) -> str:
"""Turn a word into plural""" """Turn a word into plural"""
if amount == 1: if amount == 1: