【发布时间】:2021-05-11 04:14:46
【问题描述】:
我正在编写一个包含物品、库存、货币等的经济机器人,但是现在当我在库存中出售我的物品时,它仍然存在于嵌入中,但我希望当我出售我的物品和编号时库存为 0 即不再显示在库存嵌入中,可以说是从 Json 文件中删除
我的销售命令代码:
@client.command()
async def sell(ctx,item,amount = 1):
await open_account(ctx.author)
res = await sell_this(ctx.author,item,amount)
em1 = discord.Embed(title=f"{ctx.author.name}",
description="Das item konnte nicht in deinem Inventar gefunden werden",
color=0xe67e22)
em1.set_thumbnail(url=ctx.author.avatar_url)
em2 = discord.Embed(title=f"{ctx.author.name}",
description=f"Du hast keine {amount} {item} in deinem inventar",
color=0xe67e22)
em2.set_thumbnail(url=ctx.author.avatar_url)
em3 = discord.Embed(title=f"{ctx.author.name}",
description=f"Du hast das Item: **{item}** nicht in deinem Inventar",
color=0xe67e22)
em3.set_thumbnail(url=ctx.author.avatar_url)
em4 = discord.Embed(title=f"{ctx.author.name}",
description=f"Du hast {amount} {item} gekauft",
color=0xe67e22)
em4.set_thumbnail(url=ctx.author.avatar_url)
if not res[0]:
if res[1]==1:
await ctx.send(embed=em1)
return
if res[1]==2:
await ctx.send(embed=em2)
return
if res[1]==3:
await ctx.send(embed=em3)
return
await ctx.send(embed=em4)
async def sell_this(user,item_name,amount,price = None):
item_name = item_name.lower()
name_ = None
for item in mainshop:
name = item["name"].lower()
if name == item_name:
name_ = name
if price==None:
price = 0.9* item["price"]
break
if name_ == None:
return [False,1]
cost = price*amount
users = await get_bank_data()
bal = await update_bank(user)
try:
index = 0
t = None
for thing in users[str(user.id)]["bag"]:
n = thing["item"]
if n == item_name:
old_amt = thing["amount"]
new_amt = old_amt - amount
if new_amt < 0:
return [False,2]
users[str(user.id)]["bag"][index]["amount"] = new_amt
t = 1
break
index+=1
if t == None:
return [False,3]
except:
return [False,3]
with open("Bank.json","w") as f:
json.dump(users,f)
await update_bank(user,cost,"wallet")
return [True,"Worked"]
希望有人能帮助我
【问题讨论】:
标签: json discord discord.py