From 121eae99da955a4fec61d59c1897351ce1327725 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?JelNiS=C5=82aw?= Date: Fri, 3 Jun 2022 14:56:12 +0200 Subject: [PATCH] Add delete button check --- poetry.lock | 14 +++++++------- wulkabot/cogs/automod.py | 10 +++++----- wulkabot/cogs/github.py | 5 ++++- wulkabot/utils/views.py | 20 ++++++++++++-------- 4 files changed, 28 insertions(+), 21 deletions(-) diff --git a/poetry.lock b/poetry.lock index e1ee05c..9025771 100644 --- a/poetry.lock +++ b/poetry.lock @@ -85,7 +85,7 @@ unicode_backport = ["unicodedata2"] [[package]] name = "click" -version = "8.1.2" +version = "8.1.3" 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.0a4171+g5c2431d5" +version = "2.0.0a4320+g06c1f44d" description = "A Python wrapper for the Discord API" category = "main" optional = false @@ -112,11 +112,11 @@ python-versions = ">=3.8.0" develop = false [package.dependencies] -aiohttp = ">=3.6.0,<4" +aiohttp = ">=3.7.4,<4" [package.extras] docs = ["sphinx (==4.4.0)", "sphinxcontrib-trio (==1.1.2)", "sphinxcontrib-websupport", "typing-extensions"] -speed = ["orjson (>=3.5.4)"] +speed = ["orjson (>=3.5.4)", "aiodns (>=1.1)", "brotli", "cchardet"] test = ["coverage", "pytest", "pytest-asyncio", "pytest-cov", "pytest-mock"] voice = ["PyNaCl (>=1.3.0,<1.6)"] @@ -124,7 +124,7 @@ voice = ["PyNaCl (>=1.3.0,<1.6)"] type = "git" url = "https://github.com/Rapptz/discord.py" reference = "master" -resolved_reference = "5c2431d5435d8a921a83cc05de29968f22a777ef" +resolved_reference = "06c1f44d749fa66b3b276adb7ee5616922112aaa" [[package]] name = "flake8" @@ -382,8 +382,8 @@ charset-normalizer = [ {file = "charset_normalizer-2.0.12-py3-none-any.whl", hash = "sha256:6881edbebdb17b39b4eaaa821b438bf6eddffb4468cf344f09f89def34a8b1df"}, ] click = [ - {file = "click-8.1.2-py3-none-any.whl", hash = "sha256:24e1a4a9ec5bf6299411369b208c1df2188d9eb8d916302fe6bf03faed227f1e"}, - {file = "click-8.1.2.tar.gz", hash = "sha256:479707fe14d9ec9a0757618b7a100a0ae4c4e236fac5b7f80ca68028141a1a72"}, + {file = "click-8.1.3-py3-none-any.whl", hash = "sha256:bb4d8133cb15a609f44e8213d9b391b0809795062913b383c62be0ee95b1db48"}, + {file = "click-8.1.3.tar.gz", hash = "sha256:7682dc8afb30297001674575ea00d1814d808d6a36af415a82bd481d37ba7b8e"}, ] colorama = [ {file = "colorama-0.4.4-py2.py3-none-any.whl", hash = "sha256:9f47eda37229f68eee03b24b9748937c7dc3868f906e8ba69fbcbdd3bc5dc3e2"}, diff --git a/wulkabot/cogs/automod.py b/wulkabot/cogs/automod.py index 2d4a074..d4fee49 100644 --- a/wulkabot/cogs/automod.py +++ b/wulkabot/cogs/automod.py @@ -14,16 +14,16 @@ IOS_REQUEST_WORDS = ("kiedy", "bedzie", "wulkanowy", "wulkanowego", "pobrac", "o def is_ios_request(text: str, /) -> bool: - words = remove_diacritics(text.replace("?", "").replace("!", "")).lower().split() - - if len(words) > 10: + if len(text) > 100: # the text is longer and doesn't look like just a simple question return False + words = set(remove_diacritics(text.replace("?", "").replace("!", "")).lower().split()) + if "ios" not in words: return False - return True + return any(word in words for word in IOS_REQUEST_WORDS) class Automod(commands.Cog): @@ -33,7 +33,7 @@ class Automod(commands.Cog): return if is_ios_request(message.content): - view = DeleteButton() + view = DeleteButton(message.author) reply = await message.reply("Witam chyba nigdy", view=view) view.message = reply diff --git a/wulkabot/cogs/github.py b/wulkabot/cogs/github.py index cf0cc77..d5fa219 100644 --- a/wulkabot/cogs/github.py +++ b/wulkabot/cogs/github.py @@ -11,6 +11,7 @@ from discord.ext import commands from .. import bot from ..utils import github +from ..utils.views import DeleteButton GITHUB_REPO = re.compile(r"(?:\s|^)(?P[\w-]+)/(?P[\w-]+)(?:\s|$)", re.ASCII) @@ -70,7 +71,9 @@ class GitHub(commands.Cog): if match := match_repo(message.content): if repo := await self.github.fetch_repo(*match): - await message.reply(embed=self.github_repo_embed(repo)) + view = DeleteButton(message.author) + reply = await message.reply(embed=self.github_repo_embed(repo), view=view) + view.message = reply async def setup(bot: bot.Wulkabot): diff --git a/wulkabot/utils/views.py b/wulkabot/utils/views.py index 38b6738..1379e2b 100644 --- a/wulkabot/utils/views.py +++ b/wulkabot/utils/views.py @@ -2,18 +2,22 @@ import discord class DeleteButton(discord.ui.View): - def __init__(self, timeout: float | None = 30): - super().__init__(timeout=timeout) + def __init__(self, invoker: discord.User | discord.Member) -> None: + super().__init__(timeout=10) + self.invoker = invoker 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 + def interaction_check(self, interaction: discord.Interaction) -> bool: + return ( + interaction.user == self.invoker + or isinstance(interaction.user, discord.Member) + and interaction.user.resolved_permissions.manage_messages ) + + @discord.ui.button(label="Usuń", style=discord.ButtonStyle.danger) + async def confirm(self, interaction: discord.Interaction, button: discord.ui.Button) -> None: self.stop() + await self.message.delete() async def on_timeout(self) -> None: await self.message.edit(view=None) - self.stop()