From accda93461c153f248c0a878a2f5e912006f68e1 Mon Sep 17 00:00:00 2001 From: stijndcl Date: Mon, 21 Nov 2022 22:46:19 +0100 Subject: [PATCH 1/5] Add Mock back in --- didier/cogs/fun.py | 25 +++++++++++++++++++++++++ didier/utils/types/string.py | 25 ++++++++++++++++++++++++- 2 files changed, 49 insertions(+), 1 deletion(-) diff --git a/didier/cogs/fun.py b/didier/cogs/fun.py index fed315f..c114b27 100644 --- a/didier/cogs/fun.py +++ b/didier/cogs/fun.py @@ -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.") + + 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") @app_commands.rename(comic_id="id") async def xkcd(self, ctx: commands.Context, comic_id: Optional[int] = None): diff --git a/didier/utils/types/string.py b/didier/utils/types/string.py index 5cc5f93..aec639b 100644 --- a/didier/utils/types/string.py +++ b/didier/utils/types/string.py @@ -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: From 6c8ab9a2a07ae36e0a2ed1ece4c06c47963e144e Mon Sep 17 00:00:00 2001 From: stijndcl Date: Mon, 21 Nov 2022 22:49:23 +0100 Subject: [PATCH 2/5] Check escaped message length before mocking it --- didier/cogs/fun.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/didier/cogs/fun.py b/didier/cogs/fun.py index c114b27..e824ab2 100644 --- a/didier/cogs/fun.py +++ b/didier/cogs/fun.py @@ -150,13 +150,13 @@ class Fun(commands.Cog): 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)) + message = discord.utils.escape_markdown(message) # Escaping md syntax can make the message longer than the limit - if len(mocked_message) > constants.Limits.MESSAGE_LENGTH: + 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(mocked_message) + return await interaction.followup.send(mock(message)) @commands.hybrid_command(name="xkcd") @app_commands.rename(comic_id="id") From a05d8e71381f2be601313430747e7ac3aa274de6 Mon Sep 17 00:00:00 2001 From: stijndcl Date: Wed, 11 Jan 2023 23:27:09 +0100 Subject: [PATCH 3/5] Fix CI --- .github/workflows/python.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/python.yml b/.github/workflows/python.yml index 23384df..8534ed0 100644 --- a/.github/workflows/python.yml +++ b/.github/workflows/python.yml @@ -5,7 +5,7 @@ on: jobs: dependencies: - runs-on: ubuntu-latest + runs-on: ubuntu-20.04 steps: - uses: actions/checkout@v3 - name: Setup Python From d7daeb02ff97fcdb761af4f6e89369753cacd6e4 Mon Sep 17 00:00:00 2001 From: stijndcl Date: Wed, 11 Jan 2023 23:30:49 +0100 Subject: [PATCH 4/5] Forgot some --- .github/workflows/python.yml | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/.github/workflows/python.yml b/.github/workflows/python.yml index 8534ed0..1ef38c9 100644 --- a/.github/workflows/python.yml +++ b/.github/workflows/python.yml @@ -23,7 +23,7 @@ jobs: run: pip3 install -r requirements.txt -r requirements-dev.txt tests: needs: [dependencies] - runs-on: ubuntu-latest + runs-on: ubuntu-20.04 services: postgres: image: postgres:14 @@ -63,7 +63,7 @@ jobs: token: ${{ secrets.CODECOV }} linting: needs: [tests] - runs-on: ubuntu-latest + runs-on: ubuntu-20.04 steps: - uses: actions/checkout@v3 - name: Setup Python @@ -83,7 +83,7 @@ jobs: run: flake8 typing: needs: [tests] - runs-on: ubuntu-latest + runs-on: ubuntu-20.04 steps: - uses: actions/checkout@v3 - name: Setup Python @@ -103,7 +103,7 @@ jobs: run: mypy formatting: needs: [tests] - runs-on: ubuntu-latest + runs-on: ubuntu-20.04 steps: - uses: actions/checkout@v3 - name: Setup Python From ff5de8e88b3e8d374ea2599d47ba4af89f2a6b64 Mon Sep 17 00:00:00 2001 From: stijndcl Date: Wed, 11 Jan 2023 23:37:39 +0100 Subject: [PATCH 5/5] Update CI & fix broken test --- .github/workflows/python.yml | 20 +++++++++---------- .../test_utils/test_types/test_datetime.py | 1 + 2 files changed, 11 insertions(+), 10 deletions(-) diff --git a/.github/workflows/python.yml b/.github/workflows/python.yml index 1ef38c9..be2f5cf 100644 --- a/.github/workflows/python.yml +++ b/.github/workflows/python.yml @@ -9,11 +9,11 @@ jobs: steps: - uses: actions/checkout@v3 - name: Setup Python - uses: actions/setup-python@v2 + uses: actions/setup-python@v4 with: python-version: '3.9.5' - name: Cache dependencies - uses: actions/cache@v2 + uses: actions/cache@v3 with: path: ~/.m2/repository key: ${{ runner.os }}-pip-${{ hashFiles('**/requirements.txt', '**/requirements-dev.txt') }} @@ -41,11 +41,11 @@ jobs: steps: - uses: actions/checkout@v3 - name: Setup Python - uses: actions/setup-python@v2 + uses: actions/setup-python@v4 with: python-version: '3.9.5' - name: Cache dependencies - uses: actions/cache@v2 + uses: actions/cache@v3 with: path: ~/.m2/repository key: ${{ runner.os }}-pip-${{ hashFiles('**/requirements.txt', '**/requirements-dev.txt') }} @@ -67,11 +67,11 @@ jobs: steps: - uses: actions/checkout@v3 - name: Setup Python - uses: actions/setup-python@v2 + uses: actions/setup-python@v4 with: python-version: '3.9.5' - name: Cache dependencies - uses: actions/cache@v2 + uses: actions/cache@v3 with: path: ~/.m2/repository key: ${{ runner.os }}-pip-${{ hashFiles('**/requirements.txt', '**/requirements-dev.txt') }} @@ -87,11 +87,11 @@ jobs: steps: - uses: actions/checkout@v3 - name: Setup Python - uses: actions/setup-python@v2 + uses: actions/setup-python@v4 with: python-version: '3.9.5' - name: Cache dependencies - uses: actions/cache@v2 + uses: actions/cache@v3 with: path: ~/.m2/repository key: ${{ runner.os }}-pip-${{ hashFiles('**/requirements.txt', '**/requirements-dev.txt') }} @@ -107,11 +107,11 @@ jobs: steps: - uses: actions/checkout@v3 - name: Setup Python - uses: actions/setup-python@v2 + uses: actions/setup-python@v4 with: python-version: '3.9.5' - name: Cache dependencies - uses: actions/cache@v2 + uses: actions/cache@v3 with: path: ~/.m2/repository key: ${{ runner.os }}-pip-${{ hashFiles('**/requirements.txt', '**/requirements-dev.txt') }} diff --git a/tests/test_didier/test_utils/test_types/test_datetime.py b/tests/test_didier/test_utils/test_types/test_datetime.py index 289285b..6582e99 100644 --- a/tests/test_didier/test_utils/test_types/test_datetime.py +++ b/tests/test_didier/test_utils/test_types/test_datetime.py @@ -28,6 +28,7 @@ def test_parse_dm_string_dm_garbage(): parse_dm_string("AC/DC") +@freeze_time("2022-09-11") def test_parse_dm_string_semantic(): """Test parsing date strings in the [DAY] [MONTH] and [MONTH] [DAY] formats""" result = parse_dm_string("23rd november")