From df3d011bd6490eadc2b611c8d21062b14ebd18c4 Mon Sep 17 00:00:00 2001 From: TizzySaurus <47674925+TizzySaurus@users.noreply.github.com> Date: Fri, 23 Jul 2021 21:07:01 +0100 Subject: [PATCH 01/14] Update `utils.messages.wait_for_deletion` Will now clear reactions after the timeout ends to indicate it's no longer possible to delete the message through reactions. --- bot/utils/messages.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/bot/utils/messages.py b/bot/utils/messages.py index d4a9211619..6fe32abd6c 100644 --- a/bot/utils/messages.py +++ b/bot/utils/messages.py @@ -95,8 +95,11 @@ async def wait_for_deletion( allow_mods=allow_mods, ) - with contextlib.suppress(asyncio.TimeoutError): + try: await bot.instance.wait_for('reaction_add', check=check, timeout=timeout) + except asyncio.TimeoutError: + await message.clear_reactions() + else: await message.delete() From e5622342636656a44339d7ef116c71240f669a23 Mon Sep 17 00:00:00 2001 From: TizzySaurus <47674925+TizzySaurus@users.noreply.github.com> Date: Fri, 23 Jul 2021 21:15:35 +0100 Subject: [PATCH 02/14] Update pypi command to not ghost-ping users Will no longer ghost-ping users when an invalid packaged is search containing a ping and reaction is pressed to delete message. --- bot/exts/info/pypi.py | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/bot/exts/info/pypi.py b/bot/exts/info/pypi.py index 2e42e7d6b4..12a2cb8e5f 100644 --- a/bot/exts/info/pypi.py +++ b/bot/exts/info/pypi.py @@ -67,8 +67,14 @@ async def get_package_info(self, ctx: Context, package: str) -> None: log.trace(f"Error when fetching PyPi package: {response.status}.") if error: - await ctx.send(embed=embed, delete_after=INVALID_INPUT_DELETE_DELAY) - await ctx.message.delete(delay=INVALID_INPUT_DELETE_DELAY) + error_message = await ctx.send(embed=embed) + await wait_for_deletion(error_message, (ctx.author.id,), timeout=INVALID_INPUT_DELETE_DELAY) + + # If won't ghost-ping when deleting message + if not (ctx.message.mentions or ctx.message.role_mentions): + with suppress(NotFound): + await ctx.message.delete() + else: await ctx.send(embed=embed) From fd2f53f213341cb7c3c652d70314d437626d17e7 Mon Sep 17 00:00:00 2001 From: TizzySaurus <47674925+TizzySaurus@users.noreply.github.com> Date: Fri, 23 Jul 2021 21:17:51 +0100 Subject: [PATCH 03/14] Update local file --- bot/exts/info/doc/_cog.py | 30 +++++++----------------------- 1 file changed, 7 insertions(+), 23 deletions(-) diff --git a/bot/exts/info/doc/_cog.py b/bot/exts/info/doc/_cog.py index b83c3c47e2..704884fd13 100644 --- a/bot/exts/info/doc/_cog.py +++ b/bot/exts/info/doc/_cog.py @@ -10,12 +10,11 @@ from typing import Dict, NamedTuple, Optional, Tuple, Union import aiohttp -import asyncio import discord from discord.ext import commands from bot.bot import Bot -from bot.constants import MODERATION_ROLES, RedirectOutput +from bot.constants import Emojis, MODERATION_ROLES, RedirectOutput from bot.converters import Inventory, PackageName, ValidURL, allowed_strings from bot.pagination import LinePaginator from bot.utils.lock import SharedEvent, lock @@ -35,7 +34,6 @@ "pdbcommand", "2to3fixer", ) -DELETE_ERROR_MESSAGE_REACTION = '\u274c' # :x: NOT_FOUND_DELETE_DELAY = RedirectOutput.delete_delay # Delay to wait before trying to reach a rescheduled inventory again, in minutes FETCH_RESCHEDULE_DELAY = SimpleNamespace(first=2, repeated=5) @@ -342,29 +340,15 @@ async def get_command(self, ctx: commands.Context, *, symbol_name: Optional[str] if doc_embed is None: error_message = await send_denial(ctx, "No documentation found for the requested symbol.") + await wait_for_deletion(error_message, (ctx.author.id,), timeout=NOT_FOUND_DELETE_DELAY) + with suppress(discord.NotFound): + await error_message.clear_reaction(Emojis.trashcan) - if ctx.message.mentions or ctx.message.role_mentions: - await error_message.add_reaction(DELETE_ERROR_MESSAGE_REACTION) - - try: - await self.bot.wait_for( - 'reaction_add', - check=lambda reaction, user: reaction.message == error_message and user == ctx.author and str(reaction) == DELETE_ERROR_MESSAGE_REACTION, - timeout=NOT_FOUND_DELETE_DELAY - ) - - with suppress(discord.HTTPException): - await error_message.delete() - - except asyncio.TimeoutError: - await error_message.clear_reaction(DELETE_ERROR_MESSAGE_REACTION) - - else: - await wait_for_deletion(error_message, (ctx.author.id,), timeout=NOT_FOUND_DELETE_DELAY) + # Make sure that we won't cause a ghost-ping by deleting the message + if not (ctx.message.mentions or ctx.message.role_mentions): with suppress(discord.NotFound): await ctx.message.delete() - with suppress(discord.NotFound): - await error_message.delete() + else: msg = await ctx.send(embed=doc_embed) await wait_for_deletion(msg, (ctx.author.id,)) From 5b1f31769b7b71b1d7bb60739b14ee4db4790789 Mon Sep 17 00:00:00 2001 From: TizzySaurus <47674925+TizzySaurus@users.noreply.github.com> Date: Fri, 23 Jul 2021 21:18:54 +0100 Subject: [PATCH 04/14] Remove redundant code No longer try to clear reactions after calling `utils.messages.wait_for_deletion()` since the util now does it. --- bot/exts/info/doc/_cog.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/bot/exts/info/doc/_cog.py b/bot/exts/info/doc/_cog.py index 704884fd13..bca2282399 100644 --- a/bot/exts/info/doc/_cog.py +++ b/bot/exts/info/doc/_cog.py @@ -341,8 +341,6 @@ async def get_command(self, ctx: commands.Context, *, symbol_name: Optional[str] if doc_embed is None: error_message = await send_denial(ctx, "No documentation found for the requested symbol.") await wait_for_deletion(error_message, (ctx.author.id,), timeout=NOT_FOUND_DELETE_DELAY) - with suppress(discord.NotFound): - await error_message.clear_reaction(Emojis.trashcan) # Make sure that we won't cause a ghost-ping by deleting the message if not (ctx.message.mentions or ctx.message.role_mentions): From 92db18a544ec3fa0fef5fdf198d19589baf20909 Mon Sep 17 00:00:00 2001 From: TizzySaurus <47674925+TizzySaurus@users.noreply.github.com> Date: Fri, 23 Jul 2021 21:48:43 +0100 Subject: [PATCH 05/14] Remove trailing whitespace --- bot/exts/info/pypi.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bot/exts/info/pypi.py b/bot/exts/info/pypi.py index 12a2cb8e5f..d531e2a2db 100644 --- a/bot/exts/info/pypi.py +++ b/bot/exts/info/pypi.py @@ -70,7 +70,7 @@ async def get_package_info(self, ctx: Context, package: str) -> None: error_message = await ctx.send(embed=embed) await wait_for_deletion(error_message, (ctx.author.id,), timeout=INVALID_INPUT_DELETE_DELAY) - # If won't ghost-ping when deleting message + # If won't ghost-ping when deleting message if not (ctx.message.mentions or ctx.message.role_mentions): with suppress(NotFound): await ctx.message.delete() From 32d5a5ab7a075f66153abbefe44c9b7a01173d4f Mon Sep 17 00:00:00 2001 From: TizzySaurus <47674925+TizzySaurus@users.noreply.github.com> Date: Fri, 23 Jul 2021 21:52:58 +0100 Subject: [PATCH 06/14] Remove redundant import --- bot/exts/info/doc/_cog.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bot/exts/info/doc/_cog.py b/bot/exts/info/doc/_cog.py index bca2282399..ef983903e8 100644 --- a/bot/exts/info/doc/_cog.py +++ b/bot/exts/info/doc/_cog.py @@ -14,7 +14,7 @@ from discord.ext import commands from bot.bot import Bot -from bot.constants import Emojis, MODERATION_ROLES, RedirectOutput +from bot.constants import MODERATION_ROLES, RedirectOutput from bot.converters import Inventory, PackageName, ValidURL, allowed_strings from bot.pagination import LinePaginator from bot.utils.lock import SharedEvent, lock From d78b42414336eb8c055ee16fd5ae360e342eac2c Mon Sep 17 00:00:00 2001 From: TizzySaurus <47674925+TizzySaurus@users.noreply.github.com> Date: Fri, 23 Jul 2021 21:57:32 +0100 Subject: [PATCH 07/14] Fix NameErrors --- bot/exts/info/pypi.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/bot/exts/info/pypi.py b/bot/exts/info/pypi.py index d531e2a2db..89bfe9a13b 100644 --- a/bot/exts/info/pypi.py +++ b/bot/exts/info/pypi.py @@ -1,14 +1,16 @@ +import contextlib import itertools import logging import random import re -from discord import Embed +from discord import Embed, NotFound from discord.ext.commands import Cog, Context, command from discord.utils import escape_markdown from bot.bot import Bot from bot.constants import Colours, NEGATIVE_REPLIES, RedirectOutput +from bot.utils.messages import wait_for_deletion URL = "https://pypi.org/pypi/{package}/json" PYPI_ICON = "https://cdn.discordapp.com/emojis/766274397257334814.png" @@ -72,7 +74,7 @@ async def get_package_info(self, ctx: Context, package: str) -> None: # If won't ghost-ping when deleting message if not (ctx.message.mentions or ctx.message.role_mentions): - with suppress(NotFound): + with contextlib.suppress(NotFound): await ctx.message.delete() else: From 8635527645b9311eb7414c382728ca69b95b6fe9 Mon Sep 17 00:00:00 2001 From: TizzySaurus <47674925+TizzySaurus@users.noreply.github.com> Date: Fri, 23 Jul 2021 21:59:08 +0100 Subject: [PATCH 08/14] Remove redundant import --- bot/utils/messages.py | 1 - 1 file changed, 1 deletion(-) diff --git a/bot/utils/messages.py b/bot/utils/messages.py index 6fe32abd6c..86853b3f6f 100644 --- a/bot/utils/messages.py +++ b/bot/utils/messages.py @@ -1,5 +1,4 @@ import asyncio -import contextlib import logging import random import re From cdf76735e81e343f6a344c810fe7bbc299febf8c Mon Sep 17 00:00:00 2001 From: TizzySaurus <47674925+TizzySaurus@users.noreply.github.com> Date: Fri, 23 Jul 2021 22:30:10 +0100 Subject: [PATCH 09/14] Reword comment --- bot/exts/info/pypi.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bot/exts/info/pypi.py b/bot/exts/info/pypi.py index 89bfe9a13b..9f478b5a3e 100644 --- a/bot/exts/info/pypi.py +++ b/bot/exts/info/pypi.py @@ -72,7 +72,7 @@ async def get_package_info(self, ctx: Context, package: str) -> None: error_message = await ctx.send(embed=embed) await wait_for_deletion(error_message, (ctx.author.id,), timeout=INVALID_INPUT_DELETE_DELAY) - # If won't ghost-ping when deleting message + # Make sure that we won't cause a ghost-ping by deleting the message if not (ctx.message.mentions or ctx.message.role_mentions): with contextlib.suppress(NotFound): await ctx.message.delete() From d52bf9d921465320ba8f70fb36933aed8340480d Mon Sep 17 00:00:00 2001 From: TizzySaurus <47674925+TizzySaurus@users.noreply.github.com> Date: Fri, 23 Jul 2021 22:31:48 +0100 Subject: [PATCH 10/14] Update `contextlib.suppress` import to be consistent --- bot/exts/info/pypi.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/bot/exts/info/pypi.py b/bot/exts/info/pypi.py index 9f478b5a3e..fbf21d9fe3 100644 --- a/bot/exts/info/pypi.py +++ b/bot/exts/info/pypi.py @@ -1,8 +1,8 @@ -import contextlib import itertools import logging import random import re +from contextlib import suppress from discord import Embed, NotFound from discord.ext.commands import Cog, Context, command @@ -74,7 +74,7 @@ async def get_package_info(self, ctx: Context, package: str) -> None: # Make sure that we won't cause a ghost-ping by deleting the message if not (ctx.message.mentions or ctx.message.role_mentions): - with contextlib.suppress(NotFound): + with suppress(NotFound): await ctx.message.delete() else: From aa77ce65dff915611ce5bc56d77f41b5803a5e65 Mon Sep 17 00:00:00 2001 From: TizzySaurus <47674925+TizzySaurus@users.noreply.github.com> Date: Fri, 23 Jul 2021 22:34:07 +0100 Subject: [PATCH 11/14] Update docstring to reflect earlier changes --- bot/utils/messages.py | 1 + 1 file changed, 1 insertion(+) diff --git a/bot/utils/messages.py b/bot/utils/messages.py index 86853b3f6f..75dec9baaf 100644 --- a/bot/utils/messages.py +++ b/bot/utils/messages.py @@ -69,6 +69,7 @@ async def wait_for_deletion( ) -> None: """ Wait for up to `timeout` seconds for a reaction by any of the specified `user_ids` to delete the message. + If user doesn't respond in time, will clear all reactions to indicate option to delete has expired. An `attach_emojis` bool may be specified to determine whether to attach the given `deletion_emojis` to the message in the given `context`. From 1abd1c230aedea6c0e8e680253bd7690cc27fd4c Mon Sep 17 00:00:00 2001 From: TizzySaurus <47674925+TizzySaurus@users.noreply.github.com> Date: Fri, 23 Jul 2021 22:54:07 +0100 Subject: [PATCH 12/14] Update docstring to be more informative --- bot/utils/messages.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/bot/utils/messages.py b/bot/utils/messages.py index 75dec9baaf..90672fba2a 100644 --- a/bot/utils/messages.py +++ b/bot/utils/messages.py @@ -68,8 +68,9 @@ async def wait_for_deletion( allow_mods: bool = True ) -> None: """ - Wait for up to `timeout` seconds for a reaction by any of the specified `user_ids` to delete the message. - If user doesn't respond in time, will clear all reactions to indicate option to delete has expired. + Wait for any of `user_ids` to react with one of the `deletion_emojis` within `timeout` seconds to delete `message`. + + If `timeout` expires then reactions are cleared to indicate the option to delete has expired. An `attach_emojis` bool may be specified to determine whether to attach the given `deletion_emojis` to the message in the given `context`. From b058eee1b658e7c375d92233e7aabac39cc8b4ac Mon Sep 17 00:00:00 2001 From: TizzySaurus <47674925+TizzySaurus@users.noreply.github.com> Date: Sat, 24 Jul 2021 17:03:59 +0100 Subject: [PATCH 13/14] Update to delete error message if invocation doesn't ping --- bot/exts/info/doc/_cog.py | 1 + 1 file changed, 1 insertion(+) diff --git a/bot/exts/info/doc/_cog.py b/bot/exts/info/doc/_cog.py index ef983903e8..fb9b2584a7 100644 --- a/bot/exts/info/doc/_cog.py +++ b/bot/exts/info/doc/_cog.py @@ -346,6 +346,7 @@ async def get_command(self, ctx: commands.Context, *, symbol_name: Optional[str] if not (ctx.message.mentions or ctx.message.role_mentions): with suppress(discord.NotFound): await ctx.message.delete() + await error_message.delete() else: msg = await ctx.send(embed=doc_embed) From a5f7b9bc5d1ab6978477e1cb562273200b755e50 Mon Sep 17 00:00:00 2001 From: TizzySaurus <47674925+TizzySaurus@users.noreply.github.com> Date: Sat, 24 Jul 2021 17:05:10 +0100 Subject: [PATCH 14/14] Update to delete error message if invocation doesn't ping --- bot/exts/info/pypi.py | 1 + 1 file changed, 1 insertion(+) diff --git a/bot/exts/info/pypi.py b/bot/exts/info/pypi.py index fbf21d9fe3..62498ce0ba 100644 --- a/bot/exts/info/pypi.py +++ b/bot/exts/info/pypi.py @@ -76,6 +76,7 @@ async def get_package_info(self, ctx: Context, package: str) -> None: if not (ctx.message.mentions or ctx.message.role_mentions): with suppress(NotFound): await ctx.message.delete() + await error_message.delete() else: await ctx.send(embed=embed)