python - 在商店中放置页面(经济系统) 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)
解决方案
推荐阅读
- node.js - 收到错误“必须包含图像文件或 image_id 字符串”,我错过了什么吗?ETSY 图片上传
- java - 在数据库中搜索名称并删除条目
- javascript - Postman:在环境变量的声明中使用请求名称
- java - 如何以编程方式使用户超时会话到期
- google-cloud-platform - Google Cloud Build 在单独项目中部署 Cloudfunction 的正确权限
- python - 为什么拟合函数上的误差太大,曲线似乎也从最大点数通过。如何减少该错误?
- python - 比较并查找 pandas 系列中缺失的字符串
- solr - Solr 可以索引包含本地计算机上文件的文件夹目录吗?
- c++ - 带有指向方法回调的指针的通用回调模板类
- laravel - Laravel 错误的登录重定向