From 10041a4651676cd02c6282b0cec09b1dcea973f1 Mon Sep 17 00:00:00 2001 From: TizzySaurus <47674925+TizzySaurus@users.noreply.github.com> Date: Fri, 23 Jul 2021 13:23:08 +0100 Subject: Prevent ghost-pings in docs get command Won't delete the invoking message when the giving symbol is invalid if the message contains user/role mentions. If it has mentions, allows deletions of error message through reactions. --- bot/exts/info/doc/_cog.py | 30 +++++++++++++++++++++++++----- 1 file changed, 25 insertions(+), 5 deletions(-) diff --git a/bot/exts/info/doc/_cog.py b/bot/exts/info/doc/_cog.py index c54a3ee1c..b83c3c47e 100644 --- a/bot/exts/info/doc/_cog.py +++ b/bot/exts/info/doc/_cog.py @@ -10,6 +10,7 @@ from types import SimpleNamespace from typing import Dict, NamedTuple, Optional, Tuple, Union import aiohttp +import asyncio import discord from discord.ext import commands @@ -34,6 +35,7 @@ FORCE_PREFIX_GROUPS = ( "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) @@ -340,11 +342,29 @@ class DocCog(commands.Cog): 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 ctx.message.delete() - with suppress(discord.NotFound): - await error_message.delete() + + 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) + 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,)) -- cgit v1.2.3 From 4b7962a1d128f29af4caccbf9ae610087a59a142 Mon Sep 17 00:00:00 2001 From: TizzySaurus <47674925+TizzySaurus@users.noreply.github.com> Date: Fri, 23 Jul 2021 13:37:39 +0100 Subject: Remove duplicate asyncio import --- bot/exts/info/doc/_cog.py | 1 - 1 file changed, 1 deletion(-) diff --git a/bot/exts/info/doc/_cog.py b/bot/exts/info/doc/_cog.py index b83c3c47e..e7ca634b2 100644 --- a/bot/exts/info/doc/_cog.py +++ b/bot/exts/info/doc/_cog.py @@ -10,7 +10,6 @@ from types import SimpleNamespace from typing import Dict, NamedTuple, Optional, Tuple, Union import aiohttp -import asyncio import discord from discord.ext import commands -- cgit v1.2.3 From 155422cdaaaaf7e53892c0f9cd3dde4bb94797a4 Mon Sep 17 00:00:00 2001 From: TizzySaurus <47674925+TizzySaurus@users.noreply.github.com> Date: Fri, 23 Jul 2021 13:58:24 +0100 Subject: Revamped imports --- bot/exts/info/doc/_cog.py | 53 +++++++++++++++++++++++++++-------------------- 1 file changed, 30 insertions(+), 23 deletions(-) diff --git a/bot/exts/info/doc/_cog.py b/bot/exts/info/doc/_cog.py index e7ca634b2..92a92d201 100644 --- a/bot/exts/info/doc/_cog.py +++ b/bot/exts/info/doc/_cog.py @@ -11,7 +11,9 @@ from typing import Dict, NamedTuple, Optional, Tuple, Union import aiohttp import discord -from discord.ext import commands +from discord import Colour, Embed, Message, NotFound, Reaction, User + +from discord.ext.commands import BadArgument, Cog, Context, group, has_any_role from bot.bot import Bot from bot.constants import MODERATION_ROLES, RedirectOutput @@ -57,7 +59,7 @@ class DocItem(NamedTuple): return self.base_url + self.relative_url_path -class DocCog(commands.Cog): +class DocCog(Cog): """A set of commands for querying & displaying documentation.""" def __init__(self, bot: Bot): @@ -265,7 +267,7 @@ class DocCog(commands.Cog): return "Unable to parse the requested symbol." return markdown - async def create_symbol_embed(self, symbol_name: str) -> Optional[discord.Embed]: + async def create_symbol_embed(self, symbol_name: str) -> Optional[Embed]: """ Attempt to scrape and fetch the data for the given `symbol_name`, and build an embed from its contents. @@ -294,7 +296,7 @@ class DocCog(commands.Cog): else: footer_text = "" - embed = discord.Embed( + embed = Embed( title=discord.utils.escape_markdown(symbol_name), url=f"{doc_item.url}#{doc_item.symbol_id}", description=await self.get_symbol_markdown(doc_item) @@ -302,13 +304,13 @@ class DocCog(commands.Cog): embed.set_footer(text=footer_text) return embed - @commands.group(name="docs", aliases=("doc", "d"), invoke_without_command=True) - async def docs_group(self, ctx: commands.Context, *, symbol_name: Optional[str]) -> None: + @group(name="docs", aliases=("doc", "d"), invoke_without_command=True) + async def docs_group(self, ctx: Context, *, symbol_name: Optional[str]) -> None: """Look up documentation for Python symbols.""" await self.get_command(ctx, symbol_name=symbol_name) @docs_group.command(name="getdoc", aliases=("g",)) - async def get_command(self, ctx: commands.Context, *, symbol_name: Optional[str]) -> None: + async def get_command(self, ctx: Context, *, symbol_name: Optional[str]) -> None: """ Return a documentation embed for a given symbol. @@ -321,9 +323,9 @@ class DocCog(commands.Cog): !docs getdoc aiohttp.ClientSession """ if not symbol_name: - inventory_embed = discord.Embed( + inventory_embed = Embed( title=f"All inventories (`{len(self.base_urls)}` total)", - colour=discord.Colour.blue() + colour=Colour.blue() ) lines = sorted(f"• [`{name}`]({url})" for name, url in self.base_urls.items()) @@ -345,14 +347,15 @@ class DocCog(commands.Cog): if ctx.message.mentions or ctx.message.role_mentions: await error_message.add_reaction(DELETE_ERROR_MESSAGE_REACTION) + _predicate_emoji_reaction = partial(predicate_emoji_reaction, ctx, error_message) 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, + check=_predicate_emoji_reaction timeout=NOT_FOUND_DELETE_DELAY ) - with suppress(discord.HTTPException): + with suppress(NotFound): await error_message.delete() except asyncio.TimeoutError: @@ -360,20 +363,20 @@ class DocCog(commands.Cog): else: await wait_for_deletion(error_message, (ctx.author.id,), timeout=NOT_FOUND_DELETE_DELAY) - with suppress(discord.NotFound): + with suppress(NotFound): await ctx.message.delete() - with suppress(discord.NotFound): + with suppress(NotFound): await error_message.delete() else: msg = await ctx.send(embed=doc_embed) await wait_for_deletion(msg, (ctx.author.id,)) @docs_group.command(name="setdoc", aliases=("s",)) - @commands.has_any_role(*MODERATION_ROLES) + @has_any_role(*MODERATION_ROLES) @lock(NAMESPACE, COMMAND_LOCK_SINGLETON, raise_error=True) async def set_command( self, - ctx: commands.Context, + ctx: Context, package_name: PackageName, base_url: ValidURL, inventory: Inventory, @@ -390,7 +393,7 @@ class DocCog(commands.Cog): https://docs.python.org/3/objects.inv """ if not base_url.endswith("/"): - raise commands.BadArgument("The base url must end with a slash.") + raise BadArgument("The base url must end with a slash.") inventory_url, inventory_dict = inventory body = { "package": package_name, @@ -408,9 +411,9 @@ class DocCog(commands.Cog): await ctx.send(f"Added the package `{package_name}` to the database and updated the inventories.") @docs_group.command(name="deletedoc", aliases=("removedoc", "rm", "d")) - @commands.has_any_role(*MODERATION_ROLES) + @has_any_role(*MODERATION_ROLES) @lock(NAMESPACE, COMMAND_LOCK_SINGLETON, raise_error=True) - async def delete_command(self, ctx: commands.Context, package_name: PackageName) -> None: + async def delete_command(self, ctx: Context, package_name: PackageName) -> None: """ Removes the specified package from the database. @@ -425,9 +428,9 @@ class DocCog(commands.Cog): await ctx.send(f"Successfully deleted `{package_name}` and refreshed the inventories.") @docs_group.command(name="refreshdoc", aliases=("rfsh", "r")) - @commands.has_any_role(*MODERATION_ROLES) + @has_any_role(*MODERATION_ROLES) @lock(NAMESPACE, COMMAND_LOCK_SINGLETON, raise_error=True) - async def refresh_command(self, ctx: commands.Context) -> None: + async def refresh_command(self, ctx: Context) -> None: """Refresh inventories and show the difference.""" old_inventories = set(self.base_urls) with ctx.typing(): @@ -440,17 +443,17 @@ class DocCog(commands.Cog): if removed := ", ".join(old_inventories - new_inventories): removed = "- " + removed - embed = discord.Embed( + embed = Embed( title="Inventories refreshed", description=f"```diff\n{added}\n{removed}```" if added or removed else "" ) await ctx.send(embed=embed) @docs_group.command(name="cleardoccache", aliases=("deletedoccache",)) - @commands.has_any_role(*MODERATION_ROLES) + @has_any_role(*MODERATION_ROLES) async def clear_cache_command( self, - ctx: commands.Context, + ctx: Context, package_name: Union[PackageName, allowed_strings("*")] # noqa: F722 ) -> None: """Clear the persistent redis cache for `package`.""" @@ -464,3 +467,7 @@ class DocCog(commands.Cog): self.inventory_scheduler.cancel_all() self.init_refresh_task.cancel() asyncio.create_task(self.item_fetcher.clear(), name="DocCog.item_fetcher unload clear") + + +def predicate_emoji_reaction(ctx: Context, error_message: Message, reaction: Reaction, user: User): + return reaction.message == error_message and user == ctx.author and str(reaction) == DELETE_ERROR_MESSAGE_REACTION -- cgit v1.2.3 From cb4098d816e069e622202c11f2a6fbaea7475c0f Mon Sep 17 00:00:00 2001 From: TizzySaurus <47674925+TizzySaurus@users.noreply.github.com> Date: Fri, 23 Jul 2021 14:02:26 +0100 Subject: Add missing functools.partial import --- 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 92a92d201..01358c453 100644 --- a/bot/exts/info/doc/_cog.py +++ b/bot/exts/info/doc/_cog.py @@ -6,6 +6,7 @@ import sys import textwrap from collections import defaultdict from contextlib import suppress +from functools import partial from types import SimpleNamespace from typing import Dict, NamedTuple, Optional, Tuple, Union -- cgit v1.2.3 From 8f66672728e8f1d9adcc07882bcfe037bd1b9d2b Mon Sep 17 00:00:00 2001 From: TizzySaurus <47674925+TizzySaurus@users.noreply.github.com> Date: Fri, 23 Jul 2021 14:06:07 +0100 Subject: Add missing comma --- 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 01358c453..54960ce11 100644 --- a/bot/exts/info/doc/_cog.py +++ b/bot/exts/info/doc/_cog.py @@ -352,7 +352,7 @@ class DocCog(Cog): try: await self.bot.wait_for( 'reaction_add', - check=_predicate_emoji_reaction + check=_predicate_emoji_reaction, timeout=NOT_FOUND_DELETE_DELAY ) -- cgit v1.2.3 From b9203197f891932d2aa8d0066cfb495393e6b5ce Mon Sep 17 00:00:00 2001 From: TizzySaurus <47674925+TizzySaurus@users.noreply.github.com> Date: Fri, 23 Jul 2021 14:51:29 +0100 Subject: Remove trailing whitespace --- bot/exts/info/doc/_cog.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/bot/exts/info/doc/_cog.py b/bot/exts/info/doc/_cog.py index 54960ce11..8faff926c 100644 --- a/bot/exts/info/doc/_cog.py +++ b/bot/exts/info/doc/_cog.py @@ -468,7 +468,7 @@ class DocCog(Cog): self.inventory_scheduler.cancel_all() self.init_refresh_task.cancel() asyncio.create_task(self.item_fetcher.clear(), name="DocCog.item_fetcher unload clear") - - + + def predicate_emoji_reaction(ctx: Context, error_message: Message, reaction: Reaction, user: User): return reaction.message == error_message and user == ctx.author and str(reaction) == DELETE_ERROR_MESSAGE_REACTION -- cgit v1.2.3 From 17234954f2ca61c543225d110edbc22edcb55f9b Mon Sep 17 00:00:00 2001 From: TizzySaurus <47674925+TizzySaurus@users.noreply.github.com> Date: Fri, 23 Jul 2021 14:57:08 +0100 Subject: Add return type-hint and docstring --- bot/exts/info/doc/_cog.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/bot/exts/info/doc/_cog.py b/bot/exts/info/doc/_cog.py index 8faff926c..a768d6af7 100644 --- a/bot/exts/info/doc/_cog.py +++ b/bot/exts/info/doc/_cog.py @@ -470,5 +470,6 @@ class DocCog(Cog): asyncio.create_task(self.item_fetcher.clear(), name="DocCog.item_fetcher unload clear") -def predicate_emoji_reaction(ctx: Context, error_message: Message, reaction: Reaction, user: User): +def predicate_emoji_reaction(ctx: Context, error_message: Message, reaction: Reaction, user: User) -> bool: + """Return whether command author added the `:x:` emote to the `error_message`.""" return reaction.message == error_message and user == ctx.author and str(reaction) == DELETE_ERROR_MESSAGE_REACTION -- cgit v1.2.3 From c37cbe534d124f35cb9a654d078dca5d7fdb7b42 Mon Sep 17 00:00:00 2001 From: TizzySaurus <47674925+TizzySaurus@users.noreply.github.com> Date: Fri, 23 Jul 2021 14:59:31 +0100 Subject: Remove blankline that flake8 didn't like --- bot/exts/info/doc/_cog.py | 1 - 1 file changed, 1 deletion(-) diff --git a/bot/exts/info/doc/_cog.py b/bot/exts/info/doc/_cog.py index a768d6af7..cd9b69818 100644 --- a/bot/exts/info/doc/_cog.py +++ b/bot/exts/info/doc/_cog.py @@ -13,7 +13,6 @@ from typing import Dict, NamedTuple, Optional, Tuple, Union import aiohttp import discord from discord import Colour, Embed, Message, NotFound, Reaction, User - from discord.ext.commands import BadArgument, Cog, Context, group, has_any_role from bot.bot import Bot -- cgit v1.2.3 From 24aa14f5856994ebb2df85284c3cd7140c52aa96 Mon Sep 17 00:00:00 2001 From: TizzySaurus <47674925+TizzySaurus@users.noreply.github.com> Date: Fri, 23 Jul 2021 15:04:03 +0100 Subject: Fix shadowed name --- bot/exts/info/doc/_cog.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/bot/exts/info/doc/_cog.py b/bot/exts/info/doc/_cog.py index cd9b69818..0b30526a4 100644 --- a/bot/exts/info/doc/_cog.py +++ b/bot/exts/info/doc/_cog.py @@ -101,11 +101,11 @@ class DocCog(Cog): """ self.base_urls[package_name] = base_url - for group, items in inventory.items(): + for _group, items in inventory.items(): for symbol_name, relative_doc_url in items: # e.g. get 'class' from 'py:class' - group_name = group.split(":")[1] + group_name = _group.split(":")[1] symbol_name = self.ensure_unique_symbol_name( package_name, group_name, -- cgit v1.2.3 From a4fcca34bdf5ddd67e91e2cbc66506fffac3fe77 Mon Sep 17 00:00:00 2001 From: TizzySaurus <47674925+TizzySaurus@users.noreply.github.com> Date: Fri, 23 Jul 2021 15:20:00 +0100 Subject: Undo change in import style --- bot/exts/info/doc/_cog.py | 61 +++++++++++++++++++++++++---------------------- 1 file changed, 33 insertions(+), 28 deletions(-) diff --git a/bot/exts/info/doc/_cog.py b/bot/exts/info/doc/_cog.py index 0b30526a4..f6fd92302 100644 --- a/bot/exts/info/doc/_cog.py +++ b/bot/exts/info/doc/_cog.py @@ -12,8 +12,8 @@ from typing import Dict, NamedTuple, Optional, Tuple, Union import aiohttp import discord -from discord import Colour, Embed, Message, NotFound, Reaction, User -from discord.ext.commands import BadArgument, Cog, Context, group, has_any_role + +from discord.ext import commands from bot.bot import Bot from bot.constants import MODERATION_ROLES, RedirectOutput @@ -59,7 +59,7 @@ class DocItem(NamedTuple): return self.base_url + self.relative_url_path -class DocCog(Cog): +class DocCog(commands.Cog): """A set of commands for querying & displaying documentation.""" def __init__(self, bot: Bot): @@ -101,11 +101,11 @@ class DocCog(Cog): """ self.base_urls[package_name] = base_url - for _group, items in inventory.items(): + for group, items in inventory.items(): for symbol_name, relative_doc_url in items: # e.g. get 'class' from 'py:class' - group_name = _group.split(":")[1] + group_name = group.split(":")[1] symbol_name = self.ensure_unique_symbol_name( package_name, group_name, @@ -267,7 +267,7 @@ class DocCog(Cog): return "Unable to parse the requested symbol." return markdown - async def create_symbol_embed(self, symbol_name: str) -> Optional[Embed]: + async def create_symbol_embed(self, symbol_name: str) -> Optional[discord.Embed]: """ Attempt to scrape and fetch the data for the given `symbol_name`, and build an embed from its contents. @@ -296,7 +296,7 @@ class DocCog(Cog): else: footer_text = "" - embed = Embed( + embed = discord.Embed( title=discord.utils.escape_markdown(symbol_name), url=f"{doc_item.url}#{doc_item.symbol_id}", description=await self.get_symbol_markdown(doc_item) @@ -304,13 +304,13 @@ class DocCog(Cog): embed.set_footer(text=footer_text) return embed - @group(name="docs", aliases=("doc", "d"), invoke_without_command=True) - async def docs_group(self, ctx: Context, *, symbol_name: Optional[str]) -> None: + @commands.group(name="docs", aliases=("doc", "d"), invoke_without_command=True) + async def docs_group(self, ctx: commands.Context, *, symbol_name: Optional[str]) -> None: """Look up documentation for Python symbols.""" await self.get_command(ctx, symbol_name=symbol_name) @docs_group.command(name="getdoc", aliases=("g",)) - async def get_command(self, ctx: Context, *, symbol_name: Optional[str]) -> None: + async def get_command(self, ctx: commands.Context, *, symbol_name: Optional[str]) -> None: """ Return a documentation embed for a given symbol. @@ -323,9 +323,9 @@ class DocCog(Cog): !docs getdoc aiohttp.ClientSession """ if not symbol_name: - inventory_embed = Embed( + inventory_embed = discord.Embed( title=f"All inventories (`{len(self.base_urls)}` total)", - colour=Colour.blue() + colour=discord.Colour.blue() ) lines = sorted(f"• [`{name}`]({url})" for name, url in self.base_urls.items()) @@ -355,7 +355,7 @@ class DocCog(Cog): timeout=NOT_FOUND_DELETE_DELAY ) - with suppress(NotFound): + with suppress(discord.NotFound): await error_message.delete() except asyncio.TimeoutError: @@ -363,20 +363,20 @@ class DocCog(Cog): else: await wait_for_deletion(error_message, (ctx.author.id,), timeout=NOT_FOUND_DELETE_DELAY) - with suppress(NotFound): + with suppress(discord.NotFound): await ctx.message.delete() - with suppress(NotFound): + with suppress(discord.NotFound): await error_message.delete() else: msg = await ctx.send(embed=doc_embed) await wait_for_deletion(msg, (ctx.author.id,)) @docs_group.command(name="setdoc", aliases=("s",)) - @has_any_role(*MODERATION_ROLES) + @commands.has_any_role(*MODERATION_ROLES) @lock(NAMESPACE, COMMAND_LOCK_SINGLETON, raise_error=True) async def set_command( self, - ctx: Context, + ctx: commands.Context, package_name: PackageName, base_url: ValidURL, inventory: Inventory, @@ -393,7 +393,7 @@ class DocCog(Cog): https://docs.python.org/3/objects.inv """ if not base_url.endswith("/"): - raise BadArgument("The base url must end with a slash.") + raise commands.BadArgument("The base url must end with a slash.") inventory_url, inventory_dict = inventory body = { "package": package_name, @@ -411,9 +411,9 @@ class DocCog(Cog): await ctx.send(f"Added the package `{package_name}` to the database and updated the inventories.") @docs_group.command(name="deletedoc", aliases=("removedoc", "rm", "d")) - @has_any_role(*MODERATION_ROLES) + @commands.has_any_role(*MODERATION_ROLES) @lock(NAMESPACE, COMMAND_LOCK_SINGLETON, raise_error=True) - async def delete_command(self, ctx: Context, package_name: PackageName) -> None: + async def delete_command(self, ctx: commands.Context, package_name: PackageName) -> None: """ Removes the specified package from the database. @@ -428,9 +428,9 @@ class DocCog(Cog): await ctx.send(f"Successfully deleted `{package_name}` and refreshed the inventories.") @docs_group.command(name="refreshdoc", aliases=("rfsh", "r")) - @has_any_role(*MODERATION_ROLES) + @commands.has_any_role(*MODERATION_ROLES) @lock(NAMESPACE, COMMAND_LOCK_SINGLETON, raise_error=True) - async def refresh_command(self, ctx: Context) -> None: + async def refresh_command(self, ctx: commands.Context) -> None: """Refresh inventories and show the difference.""" old_inventories = set(self.base_urls) with ctx.typing(): @@ -443,17 +443,17 @@ class DocCog(Cog): if removed := ", ".join(old_inventories - new_inventories): removed = "- " + removed - embed = Embed( + embed = discord.Embed( title="Inventories refreshed", description=f"```diff\n{added}\n{removed}```" if added or removed else "" ) await ctx.send(embed=embed) @docs_group.command(name="cleardoccache", aliases=("deletedoccache",)) - @has_any_role(*MODERATION_ROLES) + @commands.has_any_role(*MODERATION_ROLES) async def clear_cache_command( self, - ctx: Context, + ctx: commands.Context, package_name: Union[PackageName, allowed_strings("*")] # noqa: F722 ) -> None: """Clear the persistent redis cache for `package`.""" @@ -469,6 +469,11 @@ class DocCog(Cog): asyncio.create_task(self.item_fetcher.clear(), name="DocCog.item_fetcher unload clear") -def predicate_emoji_reaction(ctx: Context, error_message: Message, reaction: Reaction, user: User) -> bool: - """Return whether command author added the `:x:` emote to the `error_message`.""" - return reaction.message == error_message and user == ctx.author and str(reaction) == DELETE_ERROR_MESSAGE_REACTION +def predicate_emoji_reaction( + ctx: commands.Context, + message: discord.Message, + reaction: discord.Reaction, + user: discord.User +) -> bool: + """Return whether command author added the `:x:` emote to the `message`.""" + return reaction.message == message and user == ctx.author and str(reaction) == DELETE_ERROR_MESSAGE_REACTION -- cgit v1.2.3 From 66f7492a028256f66a20b9255ebd695af67f507a Mon Sep 17 00:00:00 2001 From: TizzySaurus <47674925+TizzySaurus@users.noreply.github.com> Date: Fri, 23 Jul 2021 15:21:05 +0100 Subject: Remove blankline that flake8 doesn't like --- bot/exts/info/doc/_cog.py | 1 - 1 file changed, 1 deletion(-) diff --git a/bot/exts/info/doc/_cog.py b/bot/exts/info/doc/_cog.py index f6fd92302..c9daa3680 100644 --- a/bot/exts/info/doc/_cog.py +++ b/bot/exts/info/doc/_cog.py @@ -12,7 +12,6 @@ from typing import Dict, NamedTuple, Optional, Tuple, Union import aiohttp import discord - from discord.ext import commands from bot.bot import Bot -- cgit v1.2.3 From e76057e63452d91b48dbbba70c287cdf3d18423a Mon Sep 17 00:00:00 2001 From: TizzySaurus <47674925+TizzySaurus@users.noreply.github.com> Date: Fri, 23 Jul 2021 16:27:27 +0100 Subject: Update code to use `utils.messages.wait_for_deletion` --- bot/exts/info/doc/_cog.py | 25 +++---------------------- 1 file changed, 3 insertions(+), 22 deletions(-) diff --git a/bot/exts/info/doc/_cog.py b/bot/exts/info/doc/_cog.py index c9daa3680..e00c64150 100644 --- a/bot/exts/info/doc/_cog.py +++ b/bot/exts/info/doc/_cog.py @@ -6,7 +6,6 @@ import sys import textwrap from collections import defaultdict from contextlib import suppress -from functools import partial from types import SimpleNamespace from typing import Dict, NamedTuple, Optional, Tuple, Union @@ -35,7 +34,6 @@ FORCE_PREFIX_GROUPS = ( "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) @@ -343,29 +341,12 @@ class DocCog(commands.Cog): if doc_embed is None: error_message = await send_denial(ctx, "No documentation found for the requested symbol.") - if ctx.message.mentions or ctx.message.role_mentions: - await error_message.add_reaction(DELETE_ERROR_MESSAGE_REACTION) + await wait_for_deletion(error_message, (ctx.author.id,), timeout=NOT_FOUND_DELETE_DELAY) - _predicate_emoji_reaction = partial(predicate_emoji_reaction, ctx, error_message) - try: - await self.bot.wait_for( - 'reaction_add', - check=_predicate_emoji_reaction, - timeout=NOT_FOUND_DELETE_DELAY - ) - - with suppress(discord.NotFound): - 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) + 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,)) -- cgit v1.2.3 From b2061e4f5afab8d8d714ca7a1f969d84c6f1e213 Mon Sep 17 00:00:00 2001 From: TizzySaurus <47674925+TizzySaurus@users.noreply.github.com> Date: Fri, 23 Jul 2021 16:29:56 +0100 Subject: Remove deprecated function --- bot/exts/info/doc/_cog.py | 10 ---------- 1 file changed, 10 deletions(-) diff --git a/bot/exts/info/doc/_cog.py b/bot/exts/info/doc/_cog.py index e00c64150..ddf8e65e3 100644 --- a/bot/exts/info/doc/_cog.py +++ b/bot/exts/info/doc/_cog.py @@ -447,13 +447,3 @@ class DocCog(commands.Cog): self.inventory_scheduler.cancel_all() self.init_refresh_task.cancel() asyncio.create_task(self.item_fetcher.clear(), name="DocCog.item_fetcher unload clear") - - -def predicate_emoji_reaction( - ctx: commands.Context, - message: discord.Message, - reaction: discord.Reaction, - user: discord.User -) -> bool: - """Return whether command author added the `:x:` emote to the `message`.""" - return reaction.message == message and user == ctx.author and str(reaction) == DELETE_ERROR_MESSAGE_REACTION -- cgit v1.2.3 From c5ab6af7770745d69e0d1d04fc07d7b8c601f98a Mon Sep 17 00:00:00 2001 From: TizzySaurus <47674925+TizzySaurus@users.noreply.github.com> Date: Fri, 23 Jul 2021 16:33:52 +0100 Subject: Remove extra lines --- bot/exts/info/doc/_cog.py | 1 - 1 file changed, 1 deletion(-) diff --git a/bot/exts/info/doc/_cog.py b/bot/exts/info/doc/_cog.py index ddf8e65e3..2cac7c10e 100644 --- a/bot/exts/info/doc/_cog.py +++ b/bot/exts/info/doc/_cog.py @@ -340,7 +340,6 @@ class DocCog(commands.Cog): 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) if not (ctx.message.mentions or ctx.message.role_mentions): -- cgit v1.2.3 From d5b6f3d934215eceefdb9905e1b39f7c5c2a8a61 Mon Sep 17 00:00:00 2001 From: TizzySaurus <47674925+TizzySaurus@users.noreply.github.com> Date: Fri, 23 Jul 2021 16:59:39 +0100 Subject: Delete reaction if error_message not deleted. --- bot/exts/info/doc/_cog.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/bot/exts/info/doc/_cog.py b/bot/exts/info/doc/_cog.py index 2cac7c10e..c0cb4db29 100644 --- a/bot/exts/info/doc/_cog.py +++ b/bot/exts/info/doc/_cog.py @@ -14,7 +14,7 @@ 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 @@ -342,6 +342,9 @@ class DocCog(commands.Cog): 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 message.clear_reaction(Emojis.trashcan) + if not (ctx.message.mentions or ctx.message.role_mentions): with suppress(discord.NotFound): await ctx.message.delete() -- cgit v1.2.3 From 75a77e1e85ee9e4a7ea337564dcc25e327d94b8a Mon Sep 17 00:00:00 2001 From: TizzySaurus <47674925+TizzySaurus@users.noreply.github.com> Date: Fri, 23 Jul 2021 17:01:27 +0100 Subject: Fix typo causing NameError --- 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 c0cb4db29..25d69cbed 100644 --- a/bot/exts/info/doc/_cog.py +++ b/bot/exts/info/doc/_cog.py @@ -343,7 +343,7 @@ class DocCog(commands.Cog): await wait_for_deletion(error_message, (ctx.author.id,), timeout=NOT_FOUND_DELETE_DELAY) with suppress(discord.NotFound): - await message.clear_reaction(Emojis.trashcan) + await error_message.clear_reaction(Emojis.trashcan) if not (ctx.message.mentions or ctx.message.role_mentions): with suppress(discord.NotFound): -- cgit v1.2.3 From 47abb8d17c531e75d977ab395752690115f05cab Mon Sep 17 00:00:00 2001 From: TizzySaurus <47674925+TizzySaurus@users.noreply.github.com> Date: Fri, 23 Jul 2021 17:17:11 +0100 Subject: Remove extra line Co-authored-by: Bluenix --- bot/exts/info/doc/_cog.py | 1 - 1 file changed, 1 deletion(-) diff --git a/bot/exts/info/doc/_cog.py b/bot/exts/info/doc/_cog.py index 25d69cbed..eebd39451 100644 --- a/bot/exts/info/doc/_cog.py +++ b/bot/exts/info/doc/_cog.py @@ -341,7 +341,6 @@ class DocCog(commands.Cog): 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) -- cgit v1.2.3 From 9078afd4465838406fc5db8bba527f1b18f6d175 Mon Sep 17 00:00:00 2001 From: TizzySaurus <47674925+TizzySaurus@users.noreply.github.com> Date: Fri, 23 Jul 2021 18:13:16 +0100 Subject: Add comment Co-authored-by: Bluenix --- 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 eebd39451..704884fd1 100644 --- a/bot/exts/info/doc/_cog.py +++ b/bot/exts/info/doc/_cog.py @@ -344,6 +344,7 @@ class DocCog(commands.Cog): 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): with suppress(discord.NotFound): await ctx.message.delete() -- cgit v1.2.3