首页 > 解决方案 > 在商店中放置页面(经济系统) discord.py

问题描述

抱歉,如果这段代码可能有点灾难性,但这是我目前所拥有的(我是这类事情的一个小新手,我来这里了解更多信息),我正在尝试将页面添加到商店仅在每个页面上可以看到 5 个项目(类似于 unbelievaboat),这些页面将通过添加更多项目来放大,这是我目前拥有的代码:

  @commands.command(aliases=['mall', 'store'])
  async def shop(self, ctx, cur_page=1):
    with open('json/servers/{guild}/Shop.json'.format(guild=ctx.guild.id)) as f:
      shop = json.load(f)
    
    with open('json/servers/{guild}/{guild}-Server.json'.format(guild=ctx.guild.id)) as f:
      data = json.load(f)

      currency = data["currency"]
  
    
    codename = shop.keys()
    codename = list(codename)[0:5]
    codenamelen = len(shop.keys())
    pagelen = -(-len(shop.keys())//3)
    for codename2 in codename:
      efectivo = shop[codename2]["PRICE"]
      price = re.sub(r"(?<!^)(?=(\d{3})+$)", r",", efectivo)
    if codenamelen > 5:
      codename = shop.keys()
      codename = list(codename)[0:5]
      text = ''.join(f'{"" if shop[codename]["EMOJI"] == "None" else shop[codename]["EMOJI"]} **{shop[codename]["NAME"]}** `ID: {codename}` ─ __{price}__ {currency}\n{shop[codename]["DESCRIPTION"]}\n\n' for codename in codename)
      embed = discord.Embed(description=f'¡Bienvenido a la tienda!\nUsa **`{ctx.prefix}buy <item>`** para comprar y **`{ctx.prefix}iteminfo <item>`** para mas información.\n\n{text}', color=0x007fff)
      embed.set_author(name="Tienda de {guild}".format(guild=ctx.guild.name), icon_url=ctx.guild.icon_url)
      embed.set_footer(text=f"Paginas: {cur_page}/{int(pagelen)}")
      msg = await ctx.reply(embed=embed, mention_author=False)

      await msg.add_reaction("⬅️")
      await msg.add_reaction("⏸️")
      await msg.add_reaction("➡️")

      def check(reaction, user):
        if not user.bot and user == ctx.message.author:
          return user == ctx.author and str(reaction.emoji) == '⬅️', '⏸️', '➡️'

      while True:
          try: 
            reaction, user = await self.bot.wait_for('reaction_add', timeout=300, check=check)
          except asyncio.TimeoutError:
              await msg.clear_reactions()
              break
          #print(reaction)
          #print(user)
          try:
            if reaction.emoji == '➡️' and user == ctx.message.author and cur_page != pagelen:
              cur_page += 1
              codename = shop.keys()
              codename = list(codename)[5:]
              text = ''.join(f'{"" if shop[codename]["EMOJI"] == "None" else shop[codename]["EMOJI"]} **{shop[codename]["NAME"]}** `ID: {codename}` ─ __{price}__ {currency}\n{shop[codename]["DESCRIPTION"]}\n\n' for codename in codename)
              await msg.remove_reaction(reaction, user)
              embed = discord.Embed(description=f'¡Bienvenido a la tienda!\nUsa **`{ctx.prefix}buy <item>`** para comprar\n**`{ctx.prefix}iteminfo <item>`** para mas información.\n\n{text}', color=0x007fff)
              embed.set_author(name="Tienda de {guild}".format(guild=ctx.guild.name), icon_url=ctx.guild.icon_url)
              embed.set_footer(text=f"Paginas: {cur_page}/{pagelen}")
              embed.set_thumbnail(url=self.bot.user.avatar_url)
              await msg.edit(embed=embed, mention_author=False)
            elif reaction.emoji == '⏸️' and user == ctx.message.author:
              await msg.clear_reactions()
              break
            elif reaction.emoji == '⬅️' and user == ctx.message.author and cur_page > 0:
              cur_page -= 1
              codename = shop.keys()
              codename = list(codename)[0:5]
              text = ''.join(f'{"" if shop[codename]["EMOJI"] == "None" else shop[codename]["EMOJI"]} **{shop[codename]["NAME"]}** `ID: {codename}` ─ __{price}__ {currency}\n{shop[codename]["DESCRIPTION"]}\n\n' for codename in codename)
              await msg.remove_reaction(reaction, user)
              embed = discord.Embed(description=f'¡Bienvenido a la tienda!\nUsa **`{ctx.prefix}buy <item>`** para comprar\n**`{ctx.prefix}iteminfo <item>`** para mas información.\n\n{text}', color=0x007fff)
              embed.set_author(name="Tienda de {guild}".format(guild=ctx.guild.name), icon_url=ctx.guild.icon_url)
              embed.set_thumbnail(url=self.bot.user.avatar_url)
              embed.set_footer(text=f"Paginas: {cur_page}/{pagelen}")
              await msg.edit(embed=embed, mention_author=False)
          except:
            pass
    else:
      text = ''.join(f'{"" if shop[codename]["EMOJI"] == "None" else shop[codename]["EMOJI"]} **{shop[codename]["NAME"]}** `ID: {codename[:5]}` ─ __{price}__ {currency}\n{shop[codename]["DESCRIPTION"]}\n\n' for codename in shop)
      embed = discord.Embed(description=f'¡Bienvenido a la tienda!\nUsa **`{ctx.prefix}buy <item>`** para comprar\n**`{ctx.prefix}iteminfo <item>`** para mas información.\n\n{text}', color=0x007fff)
      embed.set_author(name="Tienda de {guild}".format(guild=ctx.guild.name), icon_url=ctx.guild.icon_url)
      msg = await ctx.reply(embed=embed, mention_author=False)

标签: pythondiscord.py

解决方案


我建议使用已经为您完成所有这些工作的现有Menus扩展。分页示例是一个非常易于理解的示例,说明了您要完成的工作。正如您所发现的那样,自己制作这个有点麻烦,所以我真的不会这样做。


推荐阅读