Sora
Sora

Reputation: 140

Missing len() within sqlite query

I have been messing around with trying to rank users by experience, although when call on the command rank, I seem to get a type error for a missing len()

error: Command raised an exception: TypeError: object of type 'sqlite3.Cursor' has no len()

I'm calling a help class (although this is also my first time using classes) and within it, it calls len which is giving me an error.

Thank you for all of the help!

class HelpMenu(ListPageSource):
    def __init__(self, ctx, data):
        self.ctx = ctx
        super().__init__(data, per_page = 10)

    async def write_page(self, menu, fields=[]):
         offset = (menu.current_page * self.per_page) + 1
         len_data = len(self.entries)

         embed = Embed(title="Server XP Leaderboard",
                       colour=self.ctx.author.colour)
         embed.set_thumbnail(url = self.ctx.guild.icon_url)
         embed.set_footer(text = f"{offset:,} - {min(len_data, offset+self.per_page-1):,} of {len_data:, } members.")

         for name, value in fields:
             embed.add_field(name=name, value=value, inline=False)
         return embed

    async def format_page(self, menu, entries):
        fields = []

        for idx, entry in enumerate(entries):
            fields.append(("Ranks", "\n".join(f'{idx}. {entry[0]} (XP: {entry[1]} | Level: {entry[2]})')))

            return await self.write_page(menu, fields)


class Exp(Cog):
    def __init__(self, bot):
        self.bot = bot

    @command(aliases = ['lvl', 'level'])
    async def rank(self, ctx):

        db = sqlite3.connect('xpdata.db')
        cursor = db.cursor()

        # user_id = self.author.id
        guild_id = ctx.guild.id
        #
        # cursor.execute(f'SELECT * FROM xpdata WHERE user_id = {user_id}  AND guild_id = {guild_id}')
        # for row in cursor.fetchall():
        #     level_display = row[3]

        xp_ranking = cursor.execute(f'SELECT user_id, xp, level FROM xpdata WHERE guild_id = {guild_id} ORDER BY xp DESC')

        #menu
        ranking_menu = MenuPages(source=HelpMenu(ctx, xp_ranking))
        await ranking_menu.start(ctx)

        #await ctx.channel.send('{} is currently level {} and rank {}'.format(ctx.author.mention, level_display))

Upvotes: 0

Views: 490

Answers (1)

Sven Eberth
Sven Eberth

Reputation: 3116

You didn't fetch the queried results.

Correct this line:

 xp_ranking = cursor.execute(f'SELECT user_id, xp, level FROM xpdata WHERE guild_id = {guild_id} ORDER BY xp DESC')

to

cursor.execute(f'SELECT user_id, xp, level FROM xpdata WHERE guild_id = {guild_id} ORDER BY xp DESC')
xp_ranking = cursor.fetchall()

Upvotes: 1

Related Questions