Skip to content

Commit

Permalink
Remove reroll reaction at max embed length
Browse files Browse the repository at this point in the history
Once the number of characters in the embed's title exceeds the limit of 256, the bot will remove the reroll emoji.

Suggested (here)[python-discord#1148 (comment)]
  • Loading branch information
Anonymous authored and Ibrahim2750mi committed Feb 25, 2023
1 parent fc8e458 commit 45e98d8
Showing 1 changed file with 11 additions and 5 deletions.
16 changes: 11 additions & 5 deletions bot/exts/utilities/conversationstarters.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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
Expand All @@ -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(
Expand Down Expand Up @@ -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

Expand All @@ -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))


Expand Down

0 comments on commit 45e98d8

Please sign in to comment.