From bdfeef5d16ff64a7ca8a9a610d39a654f2e1fb99 Mon Sep 17 00:00:00 2001 From: Chris Date: Sun, 28 Mar 2021 23:01:55 +0100 Subject: Rework bookmark command to allow other users to bookmark the same message. --- bot/exts/evergreen/bookmark.py | 116 +++++++++++++++++++++++++++++++++-------- 1 file changed, 95 insertions(+), 21 deletions(-) (limited to 'bot') diff --git a/bot/exts/evergreen/bookmark.py b/bot/exts/evergreen/bookmark.py index 5fa05d2e..d26c4845 100644 --- a/bot/exts/evergreen/bookmark.py +++ b/bot/exts/evergreen/bookmark.py @@ -1,14 +1,18 @@ +import asyncio import logging import random import discord from discord.ext import commands -from bot.constants import Colours, ERROR_REPLIES, Emojis, Icons +from bot.constants import Colours, ERROR_REPLIES, Icons from bot.utils.converters import WrappedMessageConverter log = logging.getLogger(__name__) +# Number of seconds to wait for other users to bookmark the same message +TIMEOUT = 120 + class Bookmark(commands.Cog): """Creates personal bookmarks by relaying a message link to the user's DMs.""" @@ -16,6 +20,64 @@ class Bookmark(commands.Cog): def __init__(self, bot: commands.Bot): self.bot = bot + @staticmethod + def build_bookmark_dm(target_message: WrappedMessageConverter, title: str) -> discord.Embed: + """Builds the embed to DM the bookmark requestor.""" + embed = discord.Embed( + title=title, + description=target_message.content, + colour=Colours.soft_green + ) + embed.add_field(name="Wanna give it a visit?", value=f"[Visit original message]({target_message.jump_url})") + embed.set_author(name=target_message.author, icon_url=target_message.author.avatar_url) + embed.set_thumbnail(url=Icons.bookmark) + + return embed + + @staticmethod + def build_error_embed(user: discord.Member) -> discord.Embed: + """Builds an error embed for when a bookmark requestor has DMs disabled.""" + return discord.Embed( + title=random.choice(ERROR_REPLIES), + description=f"{user.mention}, please enable your DMs to receive the bookmark", + colour=Colours.soft_red + ) + + async def action_bookmark( + self, + channel: discord.TextChannel, + user: discord.Member, + target_message: WrappedMessageConverter, + title: str + ) -> None: + """Sends the bookmark DM, or sends an error embed when a user bookmarks a message.""" + try: + embed = self.build_bookmark_dm(target_message, title) + await user.send(embed=embed) + except discord.Forbidden: + error_embed = self.build_error_embed(user) + await channel.send(embed=error_embed) + else: + log.info(f"{user} bookmarked {target_message.jump_url} with title '{title}'") + + @staticmethod + async def send_reaction_embed( + channel: discord.TextChannel, + target_message: WrappedMessageConverter + ) -> discord.Message: + """Sends an embed, with a reaction, so users can react to bookmark the message too.""" + message = await channel.send( + embed=discord.Embed( + description=( + f"React with 📌 to be sent your very own bookmark to [this message]({target_message.jump_url})." + ), + colour=Colours.soft_green + ) + ) + + await message.add_reaction("📌") + return message + @commands.command(name="bookmark", aliases=("bm", "pin")) async def bookmark( self, @@ -37,27 +99,39 @@ class Bookmark(commands.Cog): await ctx.send(embed=embed) return - embed = discord.Embed( - title=title, - colour=Colours.soft_green, - description=target_message.content - ) - embed.add_field(name="Wanna give it a visit?", value=f"[Visit original message]({target_message.jump_url})") - embed.set_author(name=target_message.author, icon_url=target_message.author.avatar_url) - embed.set_thumbnail(url=Icons.bookmark) - - try: - await ctx.author.send(embed=embed) - except discord.Forbidden: - error_embed = discord.Embed( - title=random.choice(ERROR_REPLIES), - description=f"{ctx.author.mention}, please enable your DMs to receive the bookmark", - colour=Colours.soft_red + def event_check(reaction_: discord.Reaction, user_: discord.Member) -> bool: + """Make sure that this reaction is what we want to operate on.""" + return ( + # Conditions for a successful pagination: + all(( + # Reaction is on this message + reaction_.message.id == reaction_message.id, + # User has not already bookmarked this message + user_.id not in bookmarked_users, + # Reaction is the 📌 emoji + str(reaction_.emoji) == "📌", + # Reaction was not made by the Bot + user_.id != self.bot.user.id + )) ) - await ctx.send(embed=error_embed) - else: - log.info(f"{ctx.author} bookmarked {target_message.jump_url} with title '{title}'") - await ctx.message.add_reaction(Emojis.envelope) + await self.action_bookmark(ctx.channel, ctx.author, target_message, title) + + # Keep track of who has already bookmarked, so users can't spam reactions and cause loads of DMs + bookmarked_users = [ctx.author.id] + reaction_message = await self.send_reaction_embed(ctx.channel, target_message) + + while True: + try: + _, user = await self.bot.wait_for("reaction_add", timeout=TIMEOUT, check=event_check) + except asyncio.TimeoutError: + log.debug("Timed out waiting for a reaction") + break # We're done, no reactions for the last 5 minutes + log.trace(f"{user} has sucessfully bookmarked from a reaction, attempting to DM them.") + await self.action_bookmark(ctx.channel, user, target_message, title) + bookmarked_users.append(user.id) + + # Delete the message now that we're done with it. + await reaction_message.delete() def setup(bot: commands.Bot) -> None: -- cgit v1.2.3 From 99a0fccf221ae9e365a76d9b8c6c330de6264c90 Mon Sep 17 00:00:00 2001 From: Chris Date: Sun, 28 Mar 2021 23:30:32 +0100 Subject: Update phrasing of comments in bookmark command --- bot/exts/evergreen/bookmark.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'bot') diff --git a/bot/exts/evergreen/bookmark.py b/bot/exts/evergreen/bookmark.py index d26c4845..bfb1e700 100644 --- a/bot/exts/evergreen/bookmark.py +++ b/bot/exts/evergreen/bookmark.py @@ -125,12 +125,12 @@ class Bookmark(commands.Cog): _, user = await self.bot.wait_for("reaction_add", timeout=TIMEOUT, check=event_check) except asyncio.TimeoutError: log.debug("Timed out waiting for a reaction") - break # We're done, no reactions for the last 5 minutes + break # No reactions for the last `TIMEOUT` seconds log.trace(f"{user} has sucessfully bookmarked from a reaction, attempting to DM them.") await self.action_bookmark(ctx.channel, user, target_message, title) bookmarked_users.append(user.id) - # Delete the message now that we're done with it. + # Delete the message now that the bot isn't listening to it to save screen space await reaction_message.delete() -- cgit v1.2.3 From a308990d1432a1a29f54e74678630f374a5fac2c Mon Sep 17 00:00:00 2001 From: ChrisJL Date: Mon, 29 Mar 2021 12:28:40 +0100 Subject: Fix spelling and grammar errors in bookmark command Co-authored-by: Shivansh-007 <69356296+Shivansh-007@users.noreply.github.com> --- bot/exts/evergreen/bookmark.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (limited to 'bot') diff --git a/bot/exts/evergreen/bookmark.py b/bot/exts/evergreen/bookmark.py index bfb1e700..9246825a 100644 --- a/bot/exts/evergreen/bookmark.py +++ b/bot/exts/evergreen/bookmark.py @@ -22,7 +22,7 @@ class Bookmark(commands.Cog): @staticmethod def build_bookmark_dm(target_message: WrappedMessageConverter, title: str) -> discord.Embed: - """Builds the embed to DM the bookmark requestor.""" + """Builds the embed to DM the bookmark requester.""" embed = discord.Embed( title=title, description=target_message.content, @@ -36,10 +36,10 @@ class Bookmark(commands.Cog): @staticmethod def build_error_embed(user: discord.Member) -> discord.Embed: - """Builds an error embed for when a bookmark requestor has DMs disabled.""" + """Builds an error embed for when a bookmark requester has DMs disabled.""" return discord.Embed( title=random.choice(ERROR_REPLIES), - description=f"{user.mention}, please enable your DMs to receive the bookmark", + description=f"{user.mention}, please enable your DMs to receive the bookmark.", colour=Colours.soft_red ) @@ -126,7 +126,7 @@ class Bookmark(commands.Cog): except asyncio.TimeoutError: log.debug("Timed out waiting for a reaction") break # No reactions for the last `TIMEOUT` seconds - log.trace(f"{user} has sucessfully bookmarked from a reaction, attempting to DM them.") + log.trace(f"{user} has successfully bookmarked from a reaction, attempting to DM them.") await self.action_bookmark(ctx.channel, user, target_message, title) bookmarked_users.append(user.id) -- cgit v1.2.3 From 6e71554c1604cb372415a505e4db7ca0f80f30fd Mon Sep 17 00:00:00 2001 From: ChrisJL Date: Mon, 29 Mar 2021 12:29:25 +0100 Subject: Split out an embed add field call for readability Co-authored-by: Shivansh-007 <69356296+Shivansh-007@users.noreply.github.com> --- bot/exts/evergreen/bookmark.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) (limited to 'bot') diff --git a/bot/exts/evergreen/bookmark.py b/bot/exts/evergreen/bookmark.py index 9246825a..c4c238c3 100644 --- a/bot/exts/evergreen/bookmark.py +++ b/bot/exts/evergreen/bookmark.py @@ -28,7 +28,10 @@ class Bookmark(commands.Cog): description=target_message.content, colour=Colours.soft_green ) - embed.add_field(name="Wanna give it a visit?", value=f"[Visit original message]({target_message.jump_url})") + embed.add_field( + name="Wanna give it a visit?", + value=f"[Visit original message]({target_message.jump_url})" + ) embed.set_author(name=target_message.author, icon_url=target_message.author.avatar_url) embed.set_thumbnail(url=Icons.bookmark) -- cgit v1.2.3 From 22e062ab679afd5b6aa1790617097ed45ffee1f5 Mon Sep 17 00:00:00 2001 From: Chris Date: Mon, 29 Mar 2021 12:31:10 +0100 Subject: Make bookmark emoji a constant --- bot/exts/evergreen/bookmark.py | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) (limited to 'bot') diff --git a/bot/exts/evergreen/bookmark.py b/bot/exts/evergreen/bookmark.py index c4c238c3..59e2c9d0 100644 --- a/bot/exts/evergreen/bookmark.py +++ b/bot/exts/evergreen/bookmark.py @@ -12,6 +12,7 @@ log = logging.getLogger(__name__) # Number of seconds to wait for other users to bookmark the same message TIMEOUT = 120 +BOOKMARK_EMOJI = "📌" class Bookmark(commands.Cog): @@ -29,7 +30,7 @@ class Bookmark(commands.Cog): colour=Colours.soft_green ) embed.add_field( - name="Wanna give it a visit?", + name="Wanna give it a visit?", value=f"[Visit original message]({target_message.jump_url})" ) embed.set_author(name=target_message.author, icon_url=target_message.author.avatar_url) @@ -72,13 +73,14 @@ class Bookmark(commands.Cog): message = await channel.send( embed=discord.Embed( description=( - f"React with 📌 to be sent your very own bookmark to [this message]({target_message.jump_url})." + f"React with {BOOKMARK_EMOJI} to be sent your very own bookmark to " + f"[this message]({target_message.jump_url})." ), colour=Colours.soft_green ) ) - await message.add_reaction("📌") + await message.add_reaction(BOOKMARK_EMOJI) return message @commands.command(name="bookmark", aliases=("bm", "pin")) @@ -111,8 +113,8 @@ class Bookmark(commands.Cog): reaction_.message.id == reaction_message.id, # User has not already bookmarked this message user_.id not in bookmarked_users, - # Reaction is the 📌 emoji - str(reaction_.emoji) == "📌", + # Reaction is the `BOOKMARK_EMOJI` emoji + str(reaction_.emoji) == BOOKMARK_EMOJI, # Reaction was not made by the Bot user_.id != self.bot.user.id )) -- cgit v1.2.3 From c082ab627c4a4ecf5e497f85b1424f78c73c2120 Mon Sep 17 00:00:00 2001 From: ChrisJL Date: Tue, 30 Mar 2021 21:22:06 +0100 Subject: Consistent comments in bookmark command Co-authored-by: Xithrius <15021300+Xithrius@users.noreply.github.com> --- bot/exts/evergreen/bookmark.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'bot') diff --git a/bot/exts/evergreen/bookmark.py b/bot/exts/evergreen/bookmark.py index 59e2c9d0..2088c610 100644 --- a/bot/exts/evergreen/bookmark.py +++ b/bot/exts/evergreen/bookmark.py @@ -130,7 +130,8 @@ class Bookmark(commands.Cog): _, user = await self.bot.wait_for("reaction_add", timeout=TIMEOUT, check=event_check) except asyncio.TimeoutError: log.debug("Timed out waiting for a reaction") - break # No reactions for the last `TIMEOUT` seconds + # No reactions for the last `TIMEOUT` seconds + break log.trace(f"{user} has successfully bookmarked from a reaction, attempting to DM them.") await self.action_bookmark(ctx.channel, user, target_message, title) bookmarked_users.append(user.id) -- cgit v1.2.3 From fd57de601752db923f558b726e17af665a69df55 Mon Sep 17 00:00:00 2001 From: Chris Date: Tue, 30 Mar 2021 21:26:10 +0100 Subject: Remove unnecessary underscores from variables in the bm command --- bot/exts/evergreen/bookmark.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) (limited to 'bot') diff --git a/bot/exts/evergreen/bookmark.py b/bot/exts/evergreen/bookmark.py index 2088c610..16919d8c 100644 --- a/bot/exts/evergreen/bookmark.py +++ b/bot/exts/evergreen/bookmark.py @@ -104,19 +104,19 @@ class Bookmark(commands.Cog): await ctx.send(embed=embed) return - def event_check(reaction_: discord.Reaction, user_: discord.Member) -> bool: + def event_check(reaction: discord.Reaction, user: discord.Member) -> bool: """Make sure that this reaction is what we want to operate on.""" return ( # Conditions for a successful pagination: all(( # Reaction is on this message - reaction_.message.id == reaction_message.id, + reaction.message.id == reaction_message.id, # User has not already bookmarked this message - user_.id not in bookmarked_users, + user.id not in bookmarked_users, # Reaction is the `BOOKMARK_EMOJI` emoji - str(reaction_.emoji) == BOOKMARK_EMOJI, + str(reaction.emoji) == BOOKMARK_EMOJI, # Reaction was not made by the Bot - user_.id != self.bot.user.id + user.id != self.bot.user.id )) ) await self.action_bookmark(ctx.channel, ctx.author, target_message, title) -- cgit v1.2.3 From c837894af55deafff478bf8aa3b115d4589b6e07 Mon Sep 17 00:00:00 2001 From: Chris Date: Thu, 22 Apr 2021 21:59:29 +0100 Subject: Only use WrappedMessageConverter in the actual command signature --- bot/exts/evergreen/bookmark.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'bot') diff --git a/bot/exts/evergreen/bookmark.py b/bot/exts/evergreen/bookmark.py index 16919d8c..f0b828f8 100644 --- a/bot/exts/evergreen/bookmark.py +++ b/bot/exts/evergreen/bookmark.py @@ -22,7 +22,7 @@ class Bookmark(commands.Cog): self.bot = bot @staticmethod - def build_bookmark_dm(target_message: WrappedMessageConverter, title: str) -> discord.Embed: + def build_bookmark_dm(target_message: discord.Message, title: str) -> discord.Embed: """Builds the embed to DM the bookmark requester.""" embed = discord.Embed( title=title, @@ -51,7 +51,7 @@ class Bookmark(commands.Cog): self, channel: discord.TextChannel, user: discord.Member, - target_message: WrappedMessageConverter, + target_message: discord.Message, title: str ) -> None: """Sends the bookmark DM, or sends an error embed when a user bookmarks a message.""" @@ -67,7 +67,7 @@ class Bookmark(commands.Cog): @staticmethod async def send_reaction_embed( channel: discord.TextChannel, - target_message: WrappedMessageConverter + target_message: discord.Message ) -> discord.Message: """Sends an embed, with a reaction, so users can react to bookmark the message too.""" message = await channel.send( -- cgit v1.2.3 From df65a5091100a59424bedc14144f37c43954d0b3 Mon Sep 17 00:00:00 2001 From: Chris Date: Thu, 6 May 2021 14:45:29 +0100 Subject: Use bot.bot.Bot for type hint, over commands.Bot --- bot/exts/evergreen/bookmark.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) (limited to 'bot') diff --git a/bot/exts/evergreen/bookmark.py b/bot/exts/evergreen/bookmark.py index f0b828f8..5f550a4a 100644 --- a/bot/exts/evergreen/bookmark.py +++ b/bot/exts/evergreen/bookmark.py @@ -5,6 +5,7 @@ import random import discord from discord.ext import commands +from bot.bot import Bot from bot.constants import Colours, ERROR_REPLIES, Icons from bot.utils.converters import WrappedMessageConverter @@ -18,7 +19,7 @@ BOOKMARK_EMOJI = "📌" class Bookmark(commands.Cog): """Creates personal bookmarks by relaying a message link to the user's DMs.""" - def __init__(self, bot: commands.Bot): + def __init__(self, bot: Bot): self.bot = bot @staticmethod @@ -140,6 +141,6 @@ class Bookmark(commands.Cog): await reaction_message.delete() -def setup(bot: commands.Bot) -> None: +def setup(bot: Bot) -> None: """Load the Bookmark cog.""" bot.add_cog(Bookmark(bot)) -- cgit v1.2.3 From 8c9298495a86da7c7b67b0a5d7557819ce0057e7 Mon Sep 17 00:00:00 2001 From: Chris Date: Mon, 10 May 2021 20:52:13 +0100 Subject: Remove redundant comments in bookmark cog --- bot/exts/evergreen/bookmark.py | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) (limited to 'bot') diff --git a/bot/exts/evergreen/bookmark.py b/bot/exts/evergreen/bookmark.py index 5f550a4a..7a97a40d 100644 --- a/bot/exts/evergreen/bookmark.py +++ b/bot/exts/evergreen/bookmark.py @@ -24,7 +24,7 @@ class Bookmark(commands.Cog): @staticmethod def build_bookmark_dm(target_message: discord.Message, title: str) -> discord.Embed: - """Builds the embed to DM the bookmark requester.""" + """Build the embed to DM the bookmark requester.""" embed = discord.Embed( title=title, description=target_message.content, @@ -131,13 +131,11 @@ class Bookmark(commands.Cog): _, user = await self.bot.wait_for("reaction_add", timeout=TIMEOUT, check=event_check) except asyncio.TimeoutError: log.debug("Timed out waiting for a reaction") - # No reactions for the last `TIMEOUT` seconds break log.trace(f"{user} has successfully bookmarked from a reaction, attempting to DM them.") await self.action_bookmark(ctx.channel, user, target_message, title) bookmarked_users.append(user.id) - # Delete the message now that the bot isn't listening to it to save screen space await reaction_message.delete() -- cgit v1.2.3