Add delete button check

This commit is contained in:
JelNiSław 2022-06-03 14:56:12 +02:00
parent 065b1b6d7e
commit 121eae99da
No known key found for this signature in database
GPG key ID: EA41571A0A88E97E
4 changed files with 28 additions and 21 deletions

14
poetry.lock generated
View file

@ -85,7 +85,7 @@ unicode_backport = ["unicodedata2"]
[[package]] [[package]]
name = "click" name = "click"
version = "8.1.2" version = "8.1.3"
description = "Composable command line interface toolkit" description = "Composable command line interface toolkit"
category = "dev" category = "dev"
optional = false optional = false
@ -104,7 +104,7 @@ python-versions = ">=2.7, !=3.0.*, !=3.1.*, !=3.2.*, !=3.3.*, !=3.4.*"
[[package]] [[package]]
name = "discord.py" name = "discord.py"
version = "2.0.0a4171+g5c2431d5" version = "2.0.0a4320+g06c1f44d"
description = "A Python wrapper for the Discord API" description = "A Python wrapper for the Discord API"
category = "main" category = "main"
optional = false optional = false
@ -112,11 +112,11 @@ python-versions = ">=3.8.0"
develop = false develop = false
[package.dependencies] [package.dependencies]
aiohttp = ">=3.6.0,<4" aiohttp = ">=3.7.4,<4"
[package.extras] [package.extras]
docs = ["sphinx (==4.4.0)", "sphinxcontrib-trio (==1.1.2)", "sphinxcontrib-websupport", "typing-extensions"] 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"] test = ["coverage", "pytest", "pytest-asyncio", "pytest-cov", "pytest-mock"]
voice = ["PyNaCl (>=1.3.0,<1.6)"] voice = ["PyNaCl (>=1.3.0,<1.6)"]
@ -124,7 +124,7 @@ voice = ["PyNaCl (>=1.3.0,<1.6)"]
type = "git" type = "git"
url = "https://github.com/Rapptz/discord.py" url = "https://github.com/Rapptz/discord.py"
reference = "master" reference = "master"
resolved_reference = "5c2431d5435d8a921a83cc05de29968f22a777ef" resolved_reference = "06c1f44d749fa66b3b276adb7ee5616922112aaa"
[[package]] [[package]]
name = "flake8" name = "flake8"
@ -382,8 +382,8 @@ charset-normalizer = [
{file = "charset_normalizer-2.0.12-py3-none-any.whl", hash = "sha256:6881edbebdb17b39b4eaaa821b438bf6eddffb4468cf344f09f89def34a8b1df"}, {file = "charset_normalizer-2.0.12-py3-none-any.whl", hash = "sha256:6881edbebdb17b39b4eaaa821b438bf6eddffb4468cf344f09f89def34a8b1df"},
] ]
click = [ click = [
{file = "click-8.1.2-py3-none-any.whl", hash = "sha256:24e1a4a9ec5bf6299411369b208c1df2188d9eb8d916302fe6bf03faed227f1e"}, {file = "click-8.1.3-py3-none-any.whl", hash = "sha256:bb4d8133cb15a609f44e8213d9b391b0809795062913b383c62be0ee95b1db48"},
{file = "click-8.1.2.tar.gz", hash = "sha256:479707fe14d9ec9a0757618b7a100a0ae4c4e236fac5b7f80ca68028141a1a72"}, {file = "click-8.1.3.tar.gz", hash = "sha256:7682dc8afb30297001674575ea00d1814d808d6a36af415a82bd481d37ba7b8e"},
] ]
colorama = [ colorama = [
{file = "colorama-0.4.4-py2.py3-none-any.whl", hash = "sha256:9f47eda37229f68eee03b24b9748937c7dc3868f906e8ba69fbcbdd3bc5dc3e2"}, {file = "colorama-0.4.4-py2.py3-none-any.whl", hash = "sha256:9f47eda37229f68eee03b24b9748937c7dc3868f906e8ba69fbcbdd3bc5dc3e2"},

View file

@ -14,16 +14,16 @@ IOS_REQUEST_WORDS = ("kiedy", "bedzie", "wulkanowy", "wulkanowego", "pobrac", "o
def is_ios_request(text: str, /) -> bool: def is_ios_request(text: str, /) -> bool:
words = remove_diacritics(text.replace("?", "").replace("!", "")).lower().split() if len(text) > 100:
if len(words) > 10:
# the text is longer and doesn't look like just a simple question # the text is longer and doesn't look like just a simple question
return False return False
words = set(remove_diacritics(text.replace("?", "").replace("!", "")).lower().split())
if "ios" not in words: if "ios" not in words:
return False return False
return True return any(word in words for word in IOS_REQUEST_WORDS)
class Automod(commands.Cog): class Automod(commands.Cog):
@ -33,7 +33,7 @@ class Automod(commands.Cog):
return return
if is_ios_request(message.content): if is_ios_request(message.content):
view = DeleteButton() view = DeleteButton(message.author)
reply = await message.reply("Witam chyba nigdy", view=view) reply = await message.reply("Witam chyba nigdy", view=view)
view.message = reply view.message = reply

View file

@ -11,6 +11,7 @@ from discord.ext import commands
from .. import bot from .. import bot
from ..utils import github from ..utils import github
from ..utils.views import DeleteButton
GITHUB_REPO = re.compile(r"(?:\s|^)(?P<owner>[\w-]+)/(?P<repo>[\w-]+)(?:\s|$)", re.ASCII) GITHUB_REPO = re.compile(r"(?:\s|^)(?P<owner>[\w-]+)/(?P<repo>[\w-]+)(?:\s|$)", re.ASCII)
@ -70,7 +71,9 @@ class GitHub(commands.Cog):
if match := match_repo(message.content): if match := match_repo(message.content):
if repo := await self.github.fetch_repo(*match): 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): async def setup(bot: bot.Wulkabot):

View file

@ -2,18 +2,22 @@ import discord
class DeleteButton(discord.ui.View): class DeleteButton(discord.ui.View):
def __init__(self, timeout: float | None = 30): def __init__(self, invoker: discord.User | discord.Member) -> None:
super().__init__(timeout=timeout) super().__init__(timeout=10)
self.invoker = invoker
self.message: discord.Message | None = None self.message: discord.Message | None = None
@discord.ui.button(label="Usuń", style=discord.ButtonStyle.danger) def interaction_check(self, interaction: discord.Interaction) -> bool:
async def confirm(self, interaction: discord.Interaction, button: discord.ui.Button): return (
message = interaction.message interaction.user == self.invoker
await message.edit( or isinstance(interaction.user, discord.Member)
content=f"Wiadomość usunięta przez {interaction.user}", embeds=[], attachments=[], view=None, delete_after=5 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() self.stop()
await self.message.delete()
async def on_timeout(self) -> None: async def on_timeout(self) -> None:
await self.message.edit(view=None) await self.message.edit(view=None)
self.stop()