From 065b1b6d7e63d99bc9d583776e21073d67973a34 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?JelNiS=C5=82aw?= Date: Wed, 27 Apr 2022 00:16:40 +0200 Subject: [PATCH] Add iOS request detection --- example.env | 2 +- poetry.lock | 24 ++++++++++----------- pyproject.toml | 4 ++-- wulkabot/cogs/automod.py | 42 ++++++++++++++++++++++++++++++++++++ wulkabot/utils/text_utils.py | 33 ++++++++++++++++++++++++++++ wulkabot/utils/views.py | 19 ++++++++++++++++ 6 files changed, 109 insertions(+), 15 deletions(-) create mode 100644 wulkabot/cogs/automod.py create mode 100644 wulkabot/utils/text_utils.py create mode 100644 wulkabot/utils/views.py diff --git a/example.env b/example.env index 66ab3df..47b0cab 100644 --- a/example.env +++ b/example.env @@ -1 +1 @@ -DISCORD_TOKEN="" +DISCORD_TOKEN= diff --git a/poetry.lock b/poetry.lock index 1209e33..e1ee05c 100644 --- a/poetry.lock +++ b/poetry.lock @@ -85,7 +85,7 @@ unicode_backport = ["unicodedata2"] [[package]] name = "click" -version = "8.1.0" +version = "8.1.2" description = "Composable command line interface toolkit" category = "dev" optional = false @@ -104,7 +104,7 @@ python-versions = ">=2.7, !=3.0.*, !=3.1.*, !=3.2.*, !=3.3.*, !=3.4.*" [[package]] name = "discord.py" -version = "2.0.0a3989+ge5153780" +version = "2.0.0a4171+g5c2431d5" description = "A Python wrapper for the Discord API" category = "main" optional = false @@ -123,8 +123,8 @@ voice = ["PyNaCl (>=1.3.0,<1.6)"] [package.source] type = "git" url = "https://github.com/Rapptz/discord.py" -reference = "HEAD" -resolved_reference = "e515378076c64c5ab2f3a06237f2362ac8556290" +reference = "master" +resolved_reference = "5c2431d5435d8a921a83cc05de29968f22a777ef" [[package]] name = "flake8" @@ -203,15 +203,15 @@ python-versions = "!=3.0.*,!=3.1.*,!=3.2.*,!=3.3.*,!=3.4.*,>=2.7" [[package]] name = "platformdirs" -version = "2.5.1" +version = "2.5.2" description = "A small Python module for determining appropriate platform-specific dirs, e.g. a \"user data dir\"." category = "dev" optional = false python-versions = ">=3.7" [package.extras] -docs = ["Sphinx (>=4)", "furo (>=2021.7.5b38)", "proselint (>=0.10.2)", "sphinx-autodoc-typehints (>=1.12)"] -test = ["appdirs (==1.4.4)", "pytest (>=6)", "pytest-cov (>=2.7)", "pytest-mock (>=3.6)"] +docs = ["furo (>=2021.7.5b38)", "proselint (>=0.10.2)", "sphinx-autodoc-typehints (>=1.12)", "sphinx (>=4)"] +test = ["appdirs (==1.4.4)", "pytest-cov (>=2.7)", "pytest-mock (>=3.6)", "pytest (>=6)"] [[package]] name = "pycodestyle" @@ -263,7 +263,7 @@ multidict = ">=4.0" [metadata] lock-version = "1.1" python-versions = "^3.10" -content-hash = "41ef34b1d795becacbfdd1efcbb925d2ccf5d89029cfad6f6087d2050ac2a905" +content-hash = "e02794cfd8d7eed71d5c9a704f5b37186bfe918a6ac9f17f612b8d952dc7195b" [metadata.files] aiohttp = [ @@ -382,8 +382,8 @@ charset-normalizer = [ {file = "charset_normalizer-2.0.12-py3-none-any.whl", hash = "sha256:6881edbebdb17b39b4eaaa821b438bf6eddffb4468cf344f09f89def34a8b1df"}, ] click = [ - {file = "click-8.1.0-py3-none-any.whl", hash = "sha256:19a4baa64da924c5e0cd889aba8e947f280309f1a2ce0947a3e3a7bcb7cc72d6"}, - {file = "click-8.1.0.tar.gz", hash = "sha256:977c213473c7665d3aa092b41ff12063227751c41d7b17165013e10069cc5cd2"}, + {file = "click-8.1.2-py3-none-any.whl", hash = "sha256:24e1a4a9ec5bf6299411369b208c1df2188d9eb8d916302fe6bf03faed227f1e"}, + {file = "click-8.1.2.tar.gz", hash = "sha256:479707fe14d9ec9a0757618b7a100a0ae4c4e236fac5b7f80ca68028141a1a72"}, ] colorama = [ {file = "colorama-0.4.4-py2.py3-none-any.whl", hash = "sha256:9f47eda37229f68eee03b24b9748937c7dc3868f906e8ba69fbcbdd3bc5dc3e2"}, @@ -537,8 +537,8 @@ pathspec = [ {file = "pathspec-0.9.0.tar.gz", hash = "sha256:e564499435a2673d586f6b2130bb5b95f04a3ba06f81b8f895b651a3c76aabb1"}, ] platformdirs = [ - {file = "platformdirs-2.5.1-py3-none-any.whl", hash = "sha256:bcae7cab893c2d310a711b70b24efb93334febe65f8de776ee320b517471e227"}, - {file = "platformdirs-2.5.1.tar.gz", hash = "sha256:7535e70dfa32e84d4b34996ea99c5e432fa29a708d0f4e394bbcb2a8faa4f16d"}, + {file = "platformdirs-2.5.2-py3-none-any.whl", hash = "sha256:027d8e83a2d7de06bbac4e5ef7e023c02b863d7ea5d079477e722bb41ab25788"}, + {file = "platformdirs-2.5.2.tar.gz", hash = "sha256:58c8abb07dcb441e6ee4b11d8df0ac856038f944ab98b7be6b27b2a3c7feef19"}, ] pycodestyle = [ {file = "pycodestyle-2.8.0-py2.py3-none-any.whl", hash = "sha256:720f8b39dde8b293825e7ff02c475f3077124006db4f440dcbc9a20b76548a20"}, diff --git a/pyproject.toml b/pyproject.toml index d7543da..f5e462a 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -8,11 +8,11 @@ packages = [{ include = "bot" }] [tool.poetry.dependencies] python = "^3.10" -discord = { git = "https://github.com/Rapptz/discord.py" } +"discord.py" = { git = "https://github.com/Rapptz/discord.py" } aiohttp = "^3.8.1" python-dotenv = "^0.20.0" -[tool.poetry.group.dev.dependencies] +[tool.poetry.dev-dependencies] flake8 = "^4.0.1" black = "^22.3.0" isort = "^5.10.1" diff --git a/wulkabot/cogs/automod.py b/wulkabot/cogs/automod.py new file mode 100644 index 0000000..2d4a074 --- /dev/null +++ b/wulkabot/cogs/automod.py @@ -0,0 +1,42 @@ +""" +Wulkabot +Copyright (C) 2022-present Stanisław Jelnicki +""" + +import discord +from discord.ext import commands + +from .. import bot +from ..utils.text_utils import remove_diacritics +from ..utils.views import DeleteButton + +IOS_REQUEST_WORDS = ("kiedy", "bedzie", "wulkanowy", "wulkanowego", "pobrac", "ogarnac") + + +def is_ios_request(text: str, /) -> bool: + words = remove_diacritics(text.replace("?", "").replace("!", "")).lower().split() + + if len(words) > 10: + # the text is longer and doesn't look like just a simple question + return False + + if "ios" not in words: + return False + + return True + + +class Automod(commands.Cog): + @commands.Cog.listener() + async def on_message(self, message: discord.Message): + if message.author.bot: + return + + if is_ios_request(message.content): + view = DeleteButton() + reply = await message.reply("Witam chyba nigdy", view=view) + view.message = reply + + +async def setup(bot: bot.Wulkabot): + await bot.add_cog(Automod()) diff --git a/wulkabot/utils/text_utils.py b/wulkabot/utils/text_utils.py new file mode 100644 index 0000000..19b84b0 --- /dev/null +++ b/wulkabot/utils/text_utils.py @@ -0,0 +1,33 @@ +POLISH_DIACRITICS = { + "Ą": "A", + "Ć": "C", + "Ę": "E", + "Ł": "L", + "Ń": "N", + "Ó": "O", + "Ś": "S", + "Ź": "Z", + "Ż": "Z", + "ą": "a", + "ć": "c", + "ę": "e", + "ł": "l", + "ń": "n", + "ó": "o", + "ś": "s", + "ź": "z", + "ż": "z", +} + + +def remove_diacritics(text: str, /) -> str: + """ + Replaces Polish diacritics with their ASCII counterparts + + Żółć => Zolc + Źdźbło => Zdzblo + """ + for (diacritic, ascii) in POLISH_DIACRITICS.items(): + text = text.replace(diacritic, ascii) + + return text diff --git a/wulkabot/utils/views.py b/wulkabot/utils/views.py new file mode 100644 index 0000000..38b6738 --- /dev/null +++ b/wulkabot/utils/views.py @@ -0,0 +1,19 @@ +import discord + + +class DeleteButton(discord.ui.View): + def __init__(self, timeout: float | None = 30): + super().__init__(timeout=timeout) + self.message: discord.Message | None = None + + @discord.ui.button(label="Usuń", style=discord.ButtonStyle.danger) + async def confirm(self, interaction: discord.Interaction, button: discord.ui.Button): + message = interaction.message + await message.edit( + content=f"Wiadomość usunięta przez {interaction.user}", embeds=[], attachments=[], view=None, delete_after=5 + ) + self.stop() + + async def on_timeout(self) -> None: + await self.message.edit(view=None) + self.stop()