尝试创建分页不和谐嵌入功能

时间:2021-03-24 18:59:44

标签: python discord.py pagination

我正在尝试为我的 discord.py 机器人创建一个分页嵌入。我找到了可以为您执行此操作的模块,但它们似乎只能将嵌入内容发送到服务器通道。我想将分页嵌入发送到 DM 频道。虽然嵌入没有正确加载(反应按钮出现,但嵌入没有) 非常感谢有人指出我哪里出错了。非常感谢。

@commands.command(
        name='adminsell',
        description="Sell resources to the admin shop",
        #usage='<code> [value]',
    )
    @commands.has_role("Server Player")
    @commands.has_permissions(send_messages=True)
    async def adminSell(self, ctx):
       
        
        
        pages = []
        guild_id = ctx.guild.id
        user = ctx.author
        channel = await user.create_dm()
        shopName = 'the admin shop'

        sql = "SELECT shop_name FROM shops WHERE shop_name_lower = '%s' AND guild_id = %s " % (shopName, guild_id)
        check = await conn.query(sql)
        shop_display_name = check[0][0]
        
        sql = "SELECT * FROM shop_product WHERE shop_name_lower = '%s' AND guild_id = %s AND type = 'Sell' OR type = 'Sell and Buy'" % (shopName, guild_id)
        allProducts = await conn.query(sql)
        
        j=0
        p=1
        list_length=len(allProducts)
        embed = discord.Embed(
            title=f"{shop_display_name}` will **Buy** the following products: (page {p} of {math.ceil(list_length/6)})",
           
        )
        for i in allProducts:
            
            product = i
            
            print(i)

            product_id = product[0]
            
            product_name = product[3]
            product_desc = product[4]
            product_price = product[6]
            
            if j > 5:
                p+=1

                j=0
                embed.set_thumbnail(url=ctx.guild.icon_url)
                pages.append(embed)
               
                embed = discord.Embed(
                    title=f"{shop_display_name}` will **Buy** the following products: (page {p} of {math.ceil(list_length/6)})",
                )


            embed.add_field(
                name=f"Product Id: **{product_id}**",
                value=f"**Product: *{product_name}***\nDescription: *{product_desc}*\nPrice: *{product_price}*\n --------------",
                inline=False
            )
            

            j+=1
        
        embed.set_thumbnail(url=ctx.guild.icon_url)
        pages.append(embed)
        print("the embeds: ",pages)
       
        buttons = [u"\u23EA", u"\u25C0", u"\u25B6", u"\u23E9"]
        current = 0
        msg = await channel.send(pages[current])

        for button in buttons:
            await msg.add_reaction(button)
        
        while True:
        
            try:
                    reaction, user = await self.bot.wait_for(
                        "reaction_add",
                        timeout=60,
                        check=lambda reaction, user: user.id == ctx.author.id
                        and reaction.emoji in buttons
                        #and isinstance(reaction.channel, discord.channel.DMChannel),
                    )

            except asyncio.TimeoutError:
                embed = pages[current]
                embed.set_footer(text="Timed Out!")
                await msg.clear_reactions()
                
            else:
                previous_page = current

                if reaction.emoji == u"\u23EA":
                    current = 0
                
                elif reaction.emoji == u"\u25C0":
                    if current > 0:
                        current -= 1
                
                elif reaction.emoji == u"\u25B6":
                    if current < len(pages)-1:
                        current  += 1
                elif reaction.emoji == u"\u23E9":
                    current = len(pages)-1
                
                #for button in buttons:
                    #await msg.remove_reaction(button, ctx.author)

                if current != previous_page:
                    await msg.edit(embed = pages[current])


enter image description here

1 个答案:

答案 0 :(得分:0)

嵌入必须作为仅关键字参数发送

msg = await channel.send(embed=pages[current])