Defxult / discordLevelingSystem

A library to implement a leveling system into a discord bot. Contains features such as XP, level, ranks, and role awards.
MIT License
91 stars 17 forks source link

lvl has not attribute inter.guild or ctx.guild. #24

Closed virlion closed 7 months ago

virlion commented 7 months ago

Hello, con you fix that as fast as possible please.

This is my top command:

    @commands.command(name="top", description="Guarda la classifica del tuo server.")
    async def top(inter: commands.Context, self):
        data = await self.lvl.each_member_data(inter.guild, sort_by='rank')
        top_10 = data[:10]
        leaderboard_message = "Top 10 Leaderboard:\n"
        for member_data in top_10:
            member = inter.guild.get_member(member_data['user_id'])
            leaderboard_message += f"{member.display_name}: Rank {member_data['rank']}\n"
        await inter.send(leaderboard_message)

This is the traceback

Traceback (most recent call last):
  File "/home/container/.local/lib/python3.11/site-packages/discord/ext/commands/core.py", line 235, in wrapped
    ret = await coro(*args, **kwargs)
          ^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/container/cogs/leveling.py", line 47, in top
    data = await self.lvl.each_member_data(inter.guild, sort_by='rank')
                 ^^^^^^^^
AttributeError: 'Context' object has no attribute 'lvl'

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/home/container/.local/lib/python3.11/site-packages/discord/ext/commands/bot.py", line 1350, in invoke
    await ctx.command.invoke(ctx)
  File "/home/container/.local/lib/python3.11/site-packages/discord/ext/commands/core.py", line 1029, in invoke
    await injected(*ctx.args, **ctx.kwargs)  # type: ignore
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/container/.local/lib/python3.11/site-packages/discord/ext/commands/core.py", line 244, in wrapped
    raise CommandInvokeError(exc) from exc
discord.ext.commands.errors.CommandInvokeError: Command raised an exception: AttributeError: 'Context' object has no attribute 'lvl'

Required Checklist

Defxult commented 7 months ago

What you're showing doesn't have anything to do with the library. The error implies you're doing ctx.lvl or in your case, inter.lvl which would be wrong.

"AttributeError: 'Context' object has no attribute 'lvl'

Note 1: You're using member_data as a dictionary with a key of user_id/rank when the MemberData object is not subscriptable. It also has an attribute for member IDs (MemberData.id_number) and one for rank (MemberData.rank).

Note 2: You don't need to specifically create a top 10 list the way you did. The each_member_data() method comes with the limit parameter. So it would be easier to do lvl.each_member_data(..., limit=10)

virlion commented 7 months ago

HI, sorry for the late reply but I don't understand what the error is related to the self.lvl line. I fixed the rest but the problem persists.

To avoid opening other issues I would like to talk to him about another bug, namely that the roles of the awards are not added (I think I was not wrong since I followed the full example)

I would also suggest that you can put both the str and the embed in the levelupannouncement.