@@ -43,7 +43,7 @@ def __init__(self, bot: Bot):
43
43
self .bot = bot
44
44
45
45
@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 :
47
47
"""
48
48
Build an embed containing a conversation topic.
49
49
@@ -56,21 +56,46 @@ def _build_topic_embed(channel_id: int) -> discord.Embed:
56
56
color = discord .Colour .og_blurple ()
57
57
)
58
58
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 )} **"
64
68
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
+
66
91
return embed
67
92
68
93
@staticmethod
69
94
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
74
99
) -> bool :
75
100
user_is_moderator = any (role .id in MODERATION_ROLES for role in getattr (user , "roles" , []))
76
101
user_is_invoker = user .id == command_invoker .id
@@ -83,9 +108,9 @@ def _predicate(
83
108
return is_right_reaction
84
109
85
110
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
89
114
) -> None :
90
115
await message .add_reaction ("🔄" )
91
116
while True :
@@ -101,23 +126,25 @@ async def _listen_for_refresh(
101
126
break
102
127
103
128
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 ))
105
132
except discord .NotFound :
106
133
break
107
134
108
135
with suppress (discord .NotFound ):
109
136
await message .remove_reaction (reaction , user )
110
137
111
138
@commands .command ()
112
- @commands .cooldown (1 , 60 * 2 , commands .BucketType .channel )
139
+ @commands .cooldown (1 , 60 * 2 , commands .BucketType .channel )
113
140
@whitelist_override (channels = ALL_ALLOWED_CHANNELS )
114
141
async def topic (self , ctx : commands .Context ) -> None :
115
142
"""
116
143
Responds with a random topic to start a conversation.
117
144
118
145
Allows the refresh of a topic by pressing an emoji.
119
146
"""
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 ))
121
148
self .bot .loop .create_task (self ._listen_for_refresh (ctx .author , message ))
122
149
123
150
0 commit comments