【发布时间】:2021-10-01 23:54:21
【问题描述】:
我正在使用的代码:
@client.command()
async def buy(ctx,item,amount = 1):
await open_account(ctx.author)
res = await buy_this(ctx.author,item,amount)
if not res[0]:
if res[1]==1:
await ctx.send("That Object isn't there!")
return
if res[1]==2:
await ctx.send(f"You don't have enough money in your wallet to buy {amount} {item}")
return
if item in mainshop:
name = item["name"].lower()
p = item["price"]
cost = p*amount
return
if cost == cost:
embed = discord.Embed(color = 0xff0000,description = f"{ctx.author.mention} bought {amount} {item} for `{cost}` coins")
embed.set_author(icon_url = bot_pfp,name = f"Succesful {item} purchase.")
embed.set_footer(text ="Thank-You for your purchase")
await ctx.send(embed=embed)
详情:
它没有给我错误,它确实为我购买了该项目......但它没有发送嵌入......如果我删除价格变量但我想保留它,嵌入工作。我尝试了很多东西,但似乎没有用。我会为每种类型的名称制作不同的 if 语句,但这需要太多的工作,而且很难。
这是buy_this 函数:
async def buy_this(user,item_name,amount):
item_name = item_name.lower()
name_ = None
for item in mainshop:
name = item["name"].lower()
if name == item_name:
name_ = name
price = item["price"]
break
if name_ == None:
return [False,1]
cost = price*amount
users = await get_bank_data()
bal = await update_bank(user)
if bal[0]<cost:
return [False,2]
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
users[str(user.id)]["bag"][index]["amount"] = new_amt
t = 1
break
index+=1
if t == None:
obj = {"item":item_name , "amount" : amount}
users[str(user.id)]["bag"].append(obj)
except:
obj = {"item":item_name , "amount" : amount}
users[str(user.id)]["bag"] = [obj]
with open("mainbank.json","w") as f:
json.dump(users,f)
await update_bank(user,cost*-1,"wallet")
return [True,"Worked"]
【问题讨论】:
-
每个 if 块中都有一个 return,也许这就是我猜的原因......
标签: python discord discord.py