From 45e98d82e0e6fdd038482db9d68047d588af03f6 Mon Sep 17 00:00:00 2001 From: Anonymous Date: Fri, 23 Dec 2022 01:27:53 +0000 Subject: [PATCH] Remove reroll reaction at max embed length Once the number of characters in the embed's title exceeds the limit of 256, the bot will remove the reroll emoji. Suggested (here)[https://github.com/python-discord/sir-lancebot/pull/1148#issuecomment-1362778083] --- bot/exts/utilities/conversationstarters.py | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/bot/exts/utilities/conversationstarters.py b/bot/exts/utilities/conversationstarters.py index 103ebdac1b..26850616c2 100644 --- a/bot/exts/utilities/conversationstarters.py +++ b/bot/exts/utilities/conversationstarters.py @@ -43,12 +43,14 @@ def __init__(self, bot: Bot): self.bot = bot @staticmethod - def _build_topic_embed(channel_id: int, previous_topic: None | str) -> discord.Embed: + def _build_topic_embed(channel_id: int, previous_topic: None | str) -> tuple[discord.Embed, bool]: """ Build an embed containing a conversation topic. If in a Python channel, a python-related topic will be given. Otherwise, a random conversation topic will be received by the user. + + Also returns a value that determines whether or not to remove the reaction afterwards """ # No matter what, the form will be shown. embed = discord.Embed( @@ -66,7 +68,7 @@ def _build_topic_embed(channel_id: int, previous_topic: None | str) -> discord.E if previous_topic is None: # This is the first topic being sent - return embed + return embed, False total_topics = previous_topic.count("\n") + 1 # Add 1 before first topic @@ -78,8 +80,9 @@ def _build_topic_embed(channel_id: int, previous_topic: None | str) -> discord.E # When the embed will be larger than the limit, use the previous embed instead if len(embed.title) > 256: embed.title = previous_topic + return embed, True - return embed + return embed, False @staticmethod def _predicate( @@ -119,7 +122,10 @@ async def _listen_for_refresh( try: # The returned discord.Message object from discord.Message.edit is different from the current # discord.Message object, so it must be reassigned to update properly - message = await message.edit(embed=self._build_topic_embed(message.channel.id, message.embeds[0].title)) + embed, remove_reactions = self._build_topic_embed(message.channel.id, message.embeds[0].title) + message = await message.edit(embed=embed) + if remove_reactions: + await message.clear_reaction("🔄") except discord.NotFound: break @@ -135,7 +141,7 @@ async def topic(self, ctx: commands.Context) -> None: Allows the refresh of a topic by pressing an emoji. """ - message = await ctx.send(embed=self._build_topic_embed(ctx.channel.id, None)) + message = await ctx.send(embed=self._build_topic_embed(ctx.channel.id, None)[0]) self.bot.loop.create_task(self._listen_for_refresh(ctx.author, message))