Skip to content

Commit

Permalink
Merge pull request #96 from LewisProjects/development
Browse files Browse the repository at this point in the history
New features and fix
  • Loading branch information
LevaniVashadze authored Dec 25, 2023
2 parents 368351c + 726ebb3 commit ea0dea1
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 105 deletions.
59 changes: 26 additions & 33 deletions cogs/Levels.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,12 +118,9 @@ def get_total_xp_for_level(level: int) -> int:

try:
return sum(
[
exp
for exp in [
LEVELS_AND_XP[lvl] for lvl in range(1, level + 1)
]
][::-1]
[exp for exp in [LEVELS_AND_XP[lvl] for lvl in range(1, level + 1)]][
::-1
]
)
except KeyError:
raise ValueError(
Expand Down Expand Up @@ -153,9 +150,7 @@ async def change_cooldown(self, rate: int, per: float) -> None:
raise LevelingSystemError(
"Invalid rate or per. Values must be greater than zero"
)
self._cooldown = CooldownMapping.from_cooldown(
rate, per, BucketType.member
)
self._cooldown = CooldownMapping.from_cooldown(rate, per, BucketType.member)
self.__rate = rate
self.__per = per

Expand Down Expand Up @@ -183,9 +178,7 @@ async def set_level(self, member: Member, level: int) -> None:
lvl=level,
)
else:
raise LevelingSystemError(
f'Parameter "level" must be from 0-{MAX_LEVEL}'
)
raise LevelingSystemError(f'Parameter "level" must be from 0-{MAX_LEVEL}')

async def _update_record(
self, member: Union[Member, int], level: int, xp: int, guild_id: int
Expand All @@ -200,9 +193,7 @@ async def _update_record(
(
level,
xp,
member.id
if isinstance(member, (Member, ClientUser))
else member,
member.id if isinstance(member, (Member, ClientUser)) else member,
guild_id,
),
)
Expand Down Expand Up @@ -383,9 +374,7 @@ def roundify(im, rad):
d.text((115, 96), str(lvl), font=fnt, fill=(0, 0, 0, 255))
# Rank
d.text((113, 130), f"#{rank}", font=fnt, fill=(0, 0, 0, 255))
d.rectangle(
(44, 186, 44 + width, 186 + 21), fill=(255, 255, 255, 255)
)
d.rectangle((44, 186, 44 + width, 186 + 21), fill=(255, 255, 255, 255))
txt.paste(avatar_img, (489, 23))

out = Image.alpha_composite(base, txt)
Expand Down Expand Up @@ -468,9 +457,7 @@ async def on_level_up(self, msg: Message, level: int):
if await self.is_role_reward(msg.guild, level):
role = await self.get_role_reward(msg.guild, level)
if role is not None:
await msg.author.add_roles(
role, reason=f"Level up to level {level}"
)
await msg.author.add_roles(role, reason=f"Level up to level {level}")

async def is_role_reward(self, guild: Guild, level: int) -> bool:
query = await self.bot.db.execute(
Expand Down Expand Up @@ -628,9 +615,8 @@ async def leaderboard(self, inter: ApplicationCommandInteraction):
await inter.response.defer()
limit = 10
set_user = False
records = await self.controller.get_leaderboard(
inter.guild, limit=limit
)
# get a few more users than limit so in case user is not in server the leaderboard is still full
records = await self.controller.get_leaderboard(inter.guild, limit=limit + 5)
try:
cmd_user = await self.controller.get_user(inter.author)
except UserNotFound:
Expand All @@ -640,21 +626,32 @@ async def leaderboard(self, inter: ApplicationCommandInteraction):
return await errorEmb(inter, text="No records found!")
embed = Embed(title="Leaderboard", color=0x00FF00)

for i, record in enumerate(records):
j = 0
for i in range(len(records)):
if i == len(records) or j == limit:
break
record = records[i]
user = await self.bot.fetch_user(record.user_id)
# check if the user is in the server
if user not in inter.guild.members:
records.pop(i)
continue

if record.user_id == inter.author.id:
embed.add_field(
name=f"{i + 1}. {user.name} ~ You ",
name=f"{j + 1}. {user.name} ~ You ",
value=f"Level: {record.lvl}\nTotal XP: {record.total_exp:,}",
inline=False,
)
set_user = True
else:
embed.add_field(
name=f"{i + 1}. {user.name}",
name=f"{j + 1}. {user.name}",
value=f"Level: {record.lvl}\nTotal XP: {record.total_exp:,}",
inline=False,
)

j += 1
if not set_user:
rank = await self.controller.get_rank(inter.guild.id, cmd_user)
embed.add_field(
Expand Down Expand Up @@ -684,9 +681,7 @@ async def set_lvl(
self,
inter: ApplicationCommandInteraction,
user: Member,
level: int = Param(
description="The level to set the user to", le=100, ge=0
),
level: int = Param(description="The level to set the user to", le=100, ge=0),
):
"""
Set a user's level
Expand Down Expand Up @@ -734,9 +729,7 @@ async def add(
(inter.guild.id, role.id),
):
sql = "INSERT OR IGNORE INTO role_rewards (guild_id, role_id, required_lvl) VALUES (?, ?, ?)"
await self.bot.db.execute(
sql, (inter.guild.id, role.id, level_needed)
)
await self.bot.db.execute(sql, (inter.guild.id, role.id, level_needed))
await self.bot.db.commit()
return await sucEmb(
inter,
Expand Down
Loading

0 comments on commit ea0dea1

Please sign in to comment.