Skip to content

Commit fc8e458

Browse files
AnonymousIbrahim2750mi
authored andcommitted
Optimize nested code in conversationstarters.py
This commit will remove some duplicate and unnecessary code, as suggested by @ChrisLovering.
1 parent b8ebf28 commit fc8e458

File tree

1 file changed

+17
-25
lines changed

1 file changed

+17
-25
lines changed

bot/exts/utilities/conversationstarters.py

Lines changed: 17 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -56,36 +56,28 @@ def _build_topic_embed(channel_id: int, previous_topic: None | str) -> discord.E
5656
color=discord.Colour.og_blurple()
5757
)
5858

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)}**"
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'])}**"
6864
else:
69-
# Message is being edited
70-
try:
71-
channel_topics = TOPICS[channel_id]
72-
except KeyError:
73-
# Channel doesn't have any topics.
74-
new_topic = f"**{next(TOPICS['default'])}**"
75-
else:
76-
new_topic = f"**{next(channel_topics)}**"
65+
embed.title = f"**{next(channel_topics)}**"
7766

78-
total_topics = previous_topic.count("\n") + 1
67+
if previous_topic is None:
68+
# This is the first topic being sent
69+
return embed
7970

80-
# Add 1 before first topic
81-
if total_topics == 1:
82-
previous_topic = f"1. {previous_topic}"
71+
total_topics = previous_topic.count("\n") + 1
72+
# Add 1 before first topic
73+
if total_topics == 1:
74+
previous_topic = f"1. {previous_topic}"
8375

84-
embed.title = previous_topic + f"\n{total_topics + 1}. {new_topic}"
76+
embed.title = previous_topic + f"\n{total_topics + 1}. {embed.title}"
8577

86-
# When the embed will be larger than the limit, use the previous embed instead
87-
if len(embed.title) > 256:
88-
embed.title = previous_topic
78+
# When the embed will be larger than the limit, use the previous embed instead
79+
if len(embed.title) > 256:
80+
embed.title = previous_topic
8981

9082
return embed
9183

0 commit comments

Comments
 (0)