Rapptz / discord-ext-menus

MIT License
234 stars 87 forks source link

MenuPages raises TypeError #8

Closed lossness closed 4 years ago

lossness commented 4 years ago

Exception has occurred: TypeError '<' not supported between instances of 'dict' and 'dict' File "C:\Projects\Anastasia-bot-master\cogs\embeded_menus.py", line 34, in deals pages = menus.MenuPages(source=Source(DATA, key=lambda t: t.key, per_page=12), clear_reactions_after=True) File "C:\Projects\Anastasia-bot-master\app.py", line 62, in on_message await bot.process_commands(message) File "C:\Projects\Anastasia-bot-master\app.py", line 72, in bot.run(TOKEN)

Running newest version of discord.py python 3.7.3 It was just working a couple days ago, and then I redid a bunch of unrelated paths.

Harmon758 commented 4 years ago

What's the full traceback? What is Source and DATA?

This is unlikely to be an issue with the extension since it hasn't changed in over a month. GitHub issues for this repository should be used to report issues with this extension. For help using the extension, you should join the official discord.py server.

For code block usage, see https://help.github.com/articles/creating-and-highlighting-code-blocks/.

lossness commented 4 years ago

I am using the example code from the main page of 'discord-ext-menus'.

from discord.ext import menus

class Test:
    def __init__(self, key, value):
        self.key = key
        self.value = value

data = [
    Test(key=key, value=value)
    for key in ['test', 'other', 'okay']
    for value in range(20)
]

class Source(menus.GroupByPageSource):
    async def format_page(self, menu, entry):
        joined = '\n'.join(f'{i}. <Test value={v.value}>' for i, v in enumerate(entry.items, start=1))
        return f'**{entry.key}**\n{joined}\nPage {menu.current_page + 1}/{self.get_max_pages()}'

pages = menus.MenuPages(source=Source(data, key=lambda t: t.key, per_page=12), clear_reactions_after=True)
await pages.start(ctx)

And my full code for the python file.

import discord as Discord
import json
import os
import config

from discord.ext import menus, commands

class Test:
    def __init__(self, key, value):
        self.key = key
        self.value = value

PRICE_DATA = []
for price_file in os.listdir(config.PRICE_DATA_DIR):
    with open(f"{config.PRICE_DATA_DIR}/{price_file}", "r") as stream:
        PRICE_DATA += json.load(stream)

DATA = [Test(key=key, value=value) for key in PRICE_DATA for value in range(20)]

class Source(menus.GroupByPageSource):
    async def format_page(self, menu, entry) -> Discord.Embed:
        joined = '\n'.join(f'{i}. <Test value={v.value}>' for i, v in enumerate(entry.items, start=1))
        body = Discord.Embed(title=f'**{entry.key}**\n{joined}\nPage', description=f'{menu.current_page + 1}/{self.get_max_pages()}')
        return body

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

    @commands.command()
    async def deals(self, ctx):
        pages = menus.MenuPages(source=Source(DATA, key=lambda t: t.key, per_page=12), clear_reactions_after=True)
        await pages.start(ctx)

def setup(bot):
    bot.add_cog(DealsMenu(bot))
lossness commented 4 years ago

You've enlightened me. In the example code, data is a list and in mine it's a dict. A fresh look at things really does wonders.

Harmon758 commented 4 years ago

When the files you're opening contain JSON objects, PRICE_DATA is a list of dictionaries. You're using each dictionary as a key for the DATA that Source is initialized with. GroupByPageSource, which Source subclasses, attempts to sort those entries and fails since dictionaries aren't sortable with each other.

This is not an issue with the extension. Again, for further help using the extension, you should join the official discord.py server.

lossness commented 4 years ago

Thank you.