Merge pull request #159 from stijndcl/3.3.0

3.3.0
pull/162/head v3.3.0
Stijn De Clercq 2023-01-11 23:18:08 +01:00 committed by GitHub
commit 5bfb1cecb2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
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.menus.memes import MemeSource
from didier.utils.discord import constants
from didier.utils.types.string import mock
from didier.views.modals import GenerateMeme
@ -133,6 +134,30 @@ class Fun(commands.Cog):
"""Autocompletion for the 'template'-parameter"""
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.")
message = discord.utils.escape_markdown(message)
# Escaping md syntax can make the message longer than the limit
if len(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(mock(message))
@commands.hybrid_command(name="xkcd")
@app_commands.rename(comic_id="id")
async def xkcd(self, ctx: commands.Context, comic_id: Optional[int] = None):

View File

@ -1,8 +1,9 @@
import math
import random
import re
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:
@ -38,6 +39,28 @@ def leading(character: str, string: str, target_length: Optional[int] = 2) -> st
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:
"""Turn a word into plural"""
if amount == 1: