Skip to content

Commit 4a2b741

Browse files
Anonymous4045Ibrahim2750mi
authored andcommitted
Add ability to view previous topics with .topic command
After a user invokes the ".topic" command and reacts with the reroll emoji, the embed will now show the previous few messages. It cannot display more than 256 characters, which is usually around 5 prompts. Implements: python-discord#1145
1 parent a85401f commit 4a2b741

File tree

1 file changed

+44
-17
lines changed

1 file changed

+44
-17
lines changed

bot/exts/utilities/conversationstarters.py

+44-17
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ def __init__(self, bot: Bot):
4343
self.bot = bot
4444

4545
@staticmethod
46-
def _build_topic_embed(channel_id: int) -> discord.Embed:
46+
def _build_topic_embed(channel_id: int, previous_topic: None | str) -> discord.Embed:
4747
"""
4848
Build an embed containing a conversation topic.
4949
@@ -56,21 +56,46 @@ def _build_topic_embed(channel_id: int) -> discord.Embed:
5656
color=discord.Colour.og_blurple()
5757
)
5858

59-
try:
60-
channel_topics = TOPICS[channel_id]
61-
except KeyError:
62-
# Channel doesn't have any topics.
63-
embed.title = f"**{next(TOPICS['default'])}**"
59+
if previous_topic is None:
60+
# Message first sent
61+
try:
62+
channel_topics = TOPICS[channel_id]
63+
except KeyError:
64+
# Channel doesn't have any topics.
65+
embed.title = f"**{next(TOPICS['default'])}**"
66+
else:
67+
embed.title = f"**{next(channel_topics)}**"
6468
else:
65-
embed.title = f"**{next(channel_topics)}**"
69+
# Message is being edited
70+
71+
try:
72+
channel_topics = TOPICS[channel_id]
73+
except KeyError:
74+
# Channel doesn't have any topics.
75+
new_topic = f"**{next(TOPICS['default'])}**"
76+
else:
77+
new_topic = f"\n**{next(channel_topics)}**"
78+
79+
total_topics = previous_topic.count("\n") + 1
80+
81+
# Add 1 before first topic
82+
if total_topics == 1:
83+
previous_topic = f"1. {previous_topic}"
84+
85+
embed.title = previous_topic + f"\n{total_topics + 1}. {new_topic}"
86+
87+
# When the embed will be larger than the limit, use the previous embed instead
88+
if len(embed.title) > 256:
89+
embed.title = previous_topic
90+
6691
return embed
6792

6893
@staticmethod
6994
def _predicate(
70-
command_invoker: Union[discord.User, discord.Member],
71-
message: discord.Message,
72-
reaction: discord.Reaction,
73-
user: discord.User
95+
command_invoker: Union[discord.User, discord.Member],
96+
message: discord.Message,
97+
reaction: discord.Reaction,
98+
user: discord.User
7499
) -> bool:
75100
user_is_moderator = any(role.id in MODERATION_ROLES for role in getattr(user, "roles", []))
76101
user_is_invoker = user.id == command_invoker.id
@@ -83,9 +108,9 @@ def _predicate(
83108
return is_right_reaction
84109

85110
async def _listen_for_refresh(
86-
self,
87-
command_invoker: Union[discord.User, discord.Member],
88-
message: discord.Message
111+
self,
112+
command_invoker: Union[discord.User, discord.Member],
113+
message: discord.Message
89114
) -> None:
90115
await message.add_reaction("🔄")
91116
while True:
@@ -101,23 +126,25 @@ async def _listen_for_refresh(
101126
break
102127

103128
try:
104-
await message.edit(embed=self._build_topic_embed(message.channel.id))
129+
# The returned discord.Message object from discord.Message.edit is different than the current
130+
# discord.Message object, so it must be reassigned to update properly
131+
message = await message.edit(embed=self._build_topic_embed(message.channel.id, message.embeds[0].title))
105132
except discord.NotFound:
106133
break
107134

108135
with suppress(discord.NotFound):
109136
await message.remove_reaction(reaction, user)
110137

111138
@commands.command()
112-
@commands.cooldown(1, 60*2, commands.BucketType.channel)
139+
@commands.cooldown(1, 60 * 2, commands.BucketType.channel)
113140
@whitelist_override(channels=ALL_ALLOWED_CHANNELS)
114141
async def topic(self, ctx: commands.Context) -> None:
115142
"""
116143
Responds with a random topic to start a conversation.
117144
118145
Allows the refresh of a topic by pressing an emoji.
119146
"""
120-
message = await ctx.send(embed=self._build_topic_embed(ctx.channel.id))
147+
message = await ctx.send(embed=self._build_topic_embed(ctx.channel.id, None))
121148
self.bot.loop.create_task(self._listen_for_refresh(ctx.author, message))
122149

123150

0 commit comments

Comments
 (0)