Skip to content

Commit 9418b02

Browse files
committed
Fix apply level up function to be more robust, have XP system ignore short messages
1 parent 640003a commit 9418b02

1 file changed

Lines changed: 29 additions & 32 deletions

File tree

  • techsupport_bot/functions

techsupport_bot/functions/xp.py

Lines changed: 29 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,10 @@ async def match(
7474
# Ignore messages outside of tracked categories
7575
if ctx.channel.category_id not in config.extensions.xp.categories_counted.value:
7676
return False
77+
78+
# Ignore messages that are too short
79+
if len(ctx.message.clean_content) < 20:
80+
return False
7781

7882
prefix = await self.bot.get_prefix(ctx.message)
7983

@@ -109,66 +113,59 @@ async def response(
109113
content (str): The string content of the message
110114
"""
111115
current_XP = await get_current_XP(self.bot, ctx.author, ctx.guild)
112-
new_XP = random.randint(10, 50)
116+
new_XP = random.randint(10, 20)
113117

114118
await update_current_XP(self.bot, ctx.author, ctx.guild, (current_XP + new_XP))
115119

116-
await self.apply_level_ups(ctx.author, current_XP, (current_XP + new_XP))
120+
await self.apply_level_ups(ctx.author, (current_XP + new_XP))
117121

118122
await ctx.channel.send(
119123
f"{ctx.author.display_name}: XP. New: {new_XP}, Total: {current_XP+new_XP}"
120124
)
121125
self.ineligible[ctx.author.id] = True
122126

123-
async def apply_level_ups(
124-
self: Self, user: discord.Member, old_xp: int, new_xp: int
125-
) -> None:
127+
async def apply_level_ups(self: Self, user: discord.Member, new_xp: int) -> None:
126128
"""This function will determine if a user leveled up and apply the proper roles
127129
128130
Args:
129131
user (discord.Member): The user who just gained XP
130-
old_xp (int): The old amount of XP the user had
131132
new_xp (int): The new amount of XP the user has
132133
"""
133-
old_level = None
134-
new_level = None
135-
136134
config = self.bot.guild_configs[str(user.guild.id)]
137135
levels = config.extensions.xp.level_roles.value
138-
print(levels)
136+
139137
if len(levels) == 0:
140138
return
141139

142-
old_level = max(
143-
((int(xp), role_id) for xp, role_id in levels.items() if old_xp >= int(xp)),
144-
default=(-1, None),
145-
key=lambda t: t[0],
146-
)[1]
140+
configured_levels = [
141+
(int(xp_threshold), int(role_id))
142+
for xp_threshold, role_id in levels.items()
143+
]
144+
configured_role_ids = {role_id for _, role_id in configured_levels}
147145

148-
new_level = max(
149-
((int(xp), role_id) for xp, role_id in levels.items() if new_xp >= int(xp)),
146+
# Determine the role id that corresponds to the new XP (target role)
147+
target_role_id = max(
148+
((xp, role_id) for xp, role_id in configured_levels if new_xp >= xp),
150149
default=(-1, None),
151150
key=lambda t: t[0],
152151
)[1]
153152

154-
if old_level != new_level:
155-
guild = user.guild
153+
# A list of roles IDs related to the level system that the user currently has.
154+
user_level_roles_ids = [
155+
role.id for role in user.roles if role.id in configured_role_ids
156+
]
156157

157-
if old_level:
158-
old_role = guild.get_role(old_level)
159-
if old_role in user.roles:
160-
await user.remove_roles(
161-
old_role, reason="Level up - replacing old level role"
162-
)
158+
# If the user has only the correct role, do nothing.
159+
if user_level_roles_ids == [target_role_id]:
160+
return
163161

164-
if new_level:
165-
new_role = guild.get_role(new_level)
166-
if new_role not in user.roles:
167-
await user.add_roles(
168-
new_role, reason="Level up - new level role applied"
169-
)
162+
# Otherwise, remove all the roles from user_level_roles and then apply target_role_id
163+
for role_id in user_level_roles_ids:
164+
role_object = await user.guild.fetch_role(role_id)
165+
await user.remove_roles(role_object, reason="Level up")
170166

171-
return
167+
target_role_object = await user.guild.fetch_role(target_role_id)
168+
await user.add_roles(target_role_object, reason="Level up")
172169

173170

174171
async def get_current_XP(

0 commit comments

Comments
 (0)