Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ability to view previous topics with .topic command #1148

Open
wants to merge 13 commits into
base: main
Choose a base branch
from
32 changes: 28 additions & 4 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) -> 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 @@ -63,7 +65,24 @@ def _build_topic_embed(channel_id: int) -> discord.Embed:
embed.title = f"**{next(TOPICS['default'])}**"
else:
embed.title = f"**{next(channel_topics)}**"
return embed

if previous_topic is None:
# This is the first topic being sent
return embed, False

total_topics = previous_topic.count("\n") + 1
# Add 1 before first topic
if total_topics == 1:
previous_topic = f"1. {previous_topic}"

embed.title = previous_topic + f"\n{total_topics + 1}. {embed.title}"

# When the embed will be larger than the limit, use the previous embed instead
Anonymous4045 marked this conversation as resolved.
Show resolved Hide resolved
if len(embed.title) > 256:
embed.title = previous_topic
return embed, True

return embed, False

@staticmethod
def _predicate(
Expand Down Expand Up @@ -101,7 +120,12 @@ async def _listen_for_refresh(
break

try:
await message.edit(embed=self._build_topic_embed(message.channel.id))
# 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
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 @@ -117,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))
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