From f9fa3e6a67a196c9b529c9a8b8b68bcd89f0dcec Mon Sep 17 00:00:00 2001 From: ks123 Date: Mon, 30 Mar 2020 09:29:50 +0300 Subject: (Tags): Added helper function `handle_trashcan_react` for tag response deletion handling. --- bot/cogs/tags.py | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/bot/cogs/tags.py b/bot/cogs/tags.py index 539105017..293fa36f6 100644 --- a/bot/cogs/tags.py +++ b/bot/cogs/tags.py @@ -1,14 +1,16 @@ import logging import re import time +from asyncio import TimeoutError from pathlib import Path from typing import Callable, Dict, Iterable, List, Optional -from discord import Colour, Embed +from discord import Colour, Embed, Message, Reaction, User from discord.ext.commands import Cog, Context, group from bot import constants from bot.bot import Bot +from bot.constants import Emojis from bot.converters import TagNameConverter from bot.pagination import LinePaginator @@ -139,6 +141,24 @@ class Tags(Cog): max_lines=15 ) + async def handle_trashcan_react(self, ctx: Context, msg: Message) -> None: + """Add `trashcan` emoji to Tag and handle deletion when user react to it.""" + await msg.add_reaction(Emojis.trashcan) + + def check_trashcan(reaction: Reaction, user: User) -> bool: + return ( + reaction.emoji == Emojis.trashcan + and user.id == ctx.author.id + and reaction.message == msg + ) + try: + await self.bot.wait_for("reaction_add", timeout=60.0, check=check_trashcan) + except TimeoutError: + await msg.remove_reaction(Emojis.trashcan, msg.author) + else: + await ctx.message.delete() + await msg.delete() + @group(name='tags', aliases=('tag', 't'), invoke_without_command=True) async def tags_group(self, ctx: Context, *, tag_name: TagNameConverter = None) -> None: """Show all known tags, a single tag, or run a subcommand.""" -- cgit v1.2.3 From b37f221b9b68efed48409a64a802ea0df009627f Mon Sep 17 00:00:00 2001 From: ks123 Date: Mon, 30 Mar 2020 09:36:45 +0300 Subject: (Tags): Added trashcan handling to `!tags get` command. --- bot/cogs/tags.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/bot/cogs/tags.py b/bot/cogs/tags.py index 293fa36f6..5dbb75c73 100644 --- a/bot/cogs/tags.py +++ b/bot/cogs/tags.py @@ -225,12 +225,14 @@ class Tags(Cog): "time": time.time(), "channel": ctx.channel.id } - await ctx.send(embed=Embed.from_dict(tag['embed'])) + msg = await ctx.send(embed=Embed.from_dict(tag['embed'])) + await self.handle_trashcan_react(ctx, msg) elif founds and len(tag_name) >= 3: - await ctx.send(embed=Embed( + msg = await ctx.send(embed=Embed( title='Did you mean ...', description='\n'.join(tag['title'] for tag in founds[:10]) )) + await self.handle_trashcan_react(ctx, msg) else: tags = self._cache.values() -- cgit v1.2.3 From 307aacbf1b7304ebb52d5193f19b5a12623cdbfd Mon Sep 17 00:00:00 2001 From: ks123 Date: Mon, 30 Mar 2020 09:44:13 +0300 Subject: (Tags): Fixed trashcan handling check. --- bot/cogs/tags.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/bot/cogs/tags.py b/bot/cogs/tags.py index 5dbb75c73..3f9647eb5 100644 --- a/bot/cogs/tags.py +++ b/bot/cogs/tags.py @@ -148,8 +148,8 @@ class Tags(Cog): def check_trashcan(reaction: Reaction, user: User) -> bool: return ( reaction.emoji == Emojis.trashcan - and user.id == ctx.author.id - and reaction.message == msg + and user == ctx.author + and reaction.message.id == msg.id ) try: await self.bot.wait_for("reaction_add", timeout=60.0, check=check_trashcan) -- cgit v1.2.3 From 6f273e96714c6de4738ec5ed2026e17cd3668594 Mon Sep 17 00:00:00 2001 From: ks123 Date: Tue, 31 Mar 2020 10:52:45 +0300 Subject: (Tags): Modified helper function `handle_trashcan_react` to `send_embed_with_trashcan`, applied to docstring and to command. --- bot/cogs/tags.py | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/bot/cogs/tags.py b/bot/cogs/tags.py index 3f9647eb5..3729b4511 100644 --- a/bot/cogs/tags.py +++ b/bot/cogs/tags.py @@ -5,7 +5,7 @@ from asyncio import TimeoutError from pathlib import Path from typing import Callable, Dict, Iterable, List, Optional -from discord import Colour, Embed, Message, Reaction, User +from discord import Colour, Embed, Reaction, User from discord.ext.commands import Cog, Context, group from bot import constants @@ -141,8 +141,9 @@ class Tags(Cog): max_lines=15 ) - async def handle_trashcan_react(self, ctx: Context, msg: Message) -> None: - """Add `trashcan` emoji to Tag and handle deletion when user react to it.""" + async def send_embed_with_trashcan(self, ctx: Context, embed: Embed) -> None: + """Send embed and handle it's and command message deletion with `trashcan` emoji.""" + msg = await ctx.send(embed=embed) await msg.add_reaction(Emojis.trashcan) def check_trashcan(reaction: Reaction, user: User) -> bool: @@ -225,14 +226,12 @@ class Tags(Cog): "time": time.time(), "channel": ctx.channel.id } - msg = await ctx.send(embed=Embed.from_dict(tag['embed'])) - await self.handle_trashcan_react(ctx, msg) + await self.send_embed_with_trashcan(ctx, Embed.from_dict(tag['embed'])) elif founds and len(tag_name) >= 3: - msg = await ctx.send(embed=Embed( + await self.send_embed_with_trashcan(ctx, Embed( title='Did you mean ...', description='\n'.join(tag['title'] for tag in founds[:10]) )) - await self.handle_trashcan_react(ctx, msg) else: tags = self._cache.values() -- cgit v1.2.3 From e28a580200243669b1a9219b9e9d19b7f5a503af Mon Sep 17 00:00:00 2001 From: ks123 Date: Tue, 31 Mar 2020 10:54:07 +0300 Subject: (Tags): Fixed `TimeoutError` shadowing with `asyncio.TimeoutError`. --- bot/cogs/tags.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/bot/cogs/tags.py b/bot/cogs/tags.py index 3729b4511..9548f2e43 100644 --- a/bot/cogs/tags.py +++ b/bot/cogs/tags.py @@ -1,7 +1,7 @@ +import asyncio import logging import re import time -from asyncio import TimeoutError from pathlib import Path from typing import Callable, Dict, Iterable, List, Optional @@ -154,7 +154,7 @@ class Tags(Cog): ) try: await self.bot.wait_for("reaction_add", timeout=60.0, check=check_trashcan) - except TimeoutError: + except asyncio.TimeoutError: await msg.remove_reaction(Emojis.trashcan, msg.author) else: await ctx.message.delete() -- cgit v1.2.3 From aa9757b30b4a9d4c65a994f90dfcc65f148ac655 Mon Sep 17 00:00:00 2001 From: ks123 Date: Tue, 31 Mar 2020 10:54:58 +0300 Subject: (Tags): Added blank line between check function and `try:` block on `send_embed_with_trashcan` function. --- bot/cogs/tags.py | 1 + 1 file changed, 1 insertion(+) diff --git a/bot/cogs/tags.py b/bot/cogs/tags.py index 9548f2e43..8115423cc 100644 --- a/bot/cogs/tags.py +++ b/bot/cogs/tags.py @@ -152,6 +152,7 @@ class Tags(Cog): and user == ctx.author and reaction.message.id == msg.id ) + try: await self.bot.wait_for("reaction_add", timeout=60.0, check=check_trashcan) except asyncio.TimeoutError: -- cgit v1.2.3 From 315ffa747c1e5b4527dce07061a3e0016eea7e5f Mon Sep 17 00:00:00 2001 From: ks123 Date: Tue, 31 Mar 2020 18:57:28 +0300 Subject: (Tags): Moved to existing `wait_for_deletion` function instead using custom/new one. --- bot/cogs/tags.py | 22 +++++++++++++++++----- 1 file changed, 17 insertions(+), 5 deletions(-) diff --git a/bot/cogs/tags.py b/bot/cogs/tags.py index 8115423cc..3da05679e 100644 --- a/bot/cogs/tags.py +++ b/bot/cogs/tags.py @@ -13,6 +13,7 @@ from bot.bot import Bot from bot.constants import Emojis from bot.converters import TagNameConverter from bot.pagination import LinePaginator +from bot.utils.messages import wait_for_deletion log = logging.getLogger(__name__) @@ -189,6 +190,7 @@ class Tags(Cog): @tags_group.command(name='get', aliases=('show', 'g')) async def get_command(self, ctx: Context, *, tag_name: TagNameConverter = None) -> None: """Get a specified tag, or a list of all tags if no tag is specified.""" + def _command_on_cooldown(tag_name: str) -> bool: """ Check if the command is currently on cooldown, on a per-tag, per-channel basis. @@ -227,12 +229,22 @@ class Tags(Cog): "time": time.time(), "channel": ctx.channel.id } - await self.send_embed_with_trashcan(ctx, Embed.from_dict(tag['embed'])) + await wait_for_deletion( + await ctx.send(embed=Embed.from_dict(tag['embed'])), + [ctx.author.id], + client=self.bot + ) elif founds and len(tag_name) >= 3: - await self.send_embed_with_trashcan(ctx, Embed( - title='Did you mean ...', - description='\n'.join(tag['title'] for tag in founds[:10]) - )) + await wait_for_deletion( + await ctx.send( + embed=Embed( + title='Did you mean ...', + description='\n'.join(tag['title'] for tag in founds[:10]) + ) + ), + [ctx.author.id], + client=self.bot + ) else: tags = self._cache.values() -- cgit v1.2.3 From 44534b650d8d69e02e7fc8b0189e533cea037e25 Mon Sep 17 00:00:00 2001 From: ks123 Date: Tue, 31 Mar 2020 18:59:36 +0300 Subject: (Tags): Removed unnecessary `send_embed_with_trashcan` function due using existing function. --- bot/cogs/tags.py | 24 +----------------------- 1 file changed, 1 insertion(+), 23 deletions(-) diff --git a/bot/cogs/tags.py b/bot/cogs/tags.py index 3da05679e..a6e5952ff 100644 --- a/bot/cogs/tags.py +++ b/bot/cogs/tags.py @@ -1,16 +1,14 @@ -import asyncio import logging import re import time from pathlib import Path from typing import Callable, Dict, Iterable, List, Optional -from discord import Colour, Embed, Reaction, User +from discord import Colour, Embed from discord.ext.commands import Cog, Context, group from bot import constants from bot.bot import Bot -from bot.constants import Emojis from bot.converters import TagNameConverter from bot.pagination import LinePaginator from bot.utils.messages import wait_for_deletion @@ -142,26 +140,6 @@ class Tags(Cog): max_lines=15 ) - async def send_embed_with_trashcan(self, ctx: Context, embed: Embed) -> None: - """Send embed and handle it's and command message deletion with `trashcan` emoji.""" - msg = await ctx.send(embed=embed) - await msg.add_reaction(Emojis.trashcan) - - def check_trashcan(reaction: Reaction, user: User) -> bool: - return ( - reaction.emoji == Emojis.trashcan - and user == ctx.author - and reaction.message.id == msg.id - ) - - try: - await self.bot.wait_for("reaction_add", timeout=60.0, check=check_trashcan) - except asyncio.TimeoutError: - await msg.remove_reaction(Emojis.trashcan, msg.author) - else: - await ctx.message.delete() - await msg.delete() - @group(name='tags', aliases=('tag', 't'), invoke_without_command=True) async def tags_group(self, ctx: Context, *, tag_name: TagNameConverter = None) -> None: """Show all known tags, a single tag, or run a subcommand.""" -- cgit v1.2.3