'My discord python bot's shop cause it to freeze
I have been making a discord economy bot and it has a shop. When I try and buy an item from my shop in the bot, the bot does not respond with anything and I won't recognise any other commands. The bot also gives me an error in the command prompt. This error is in the picture. Any help given is great.
https://i.stack.imgur.com/SsBwA.png
My code:
mainshop = [{"name":"Airbus A320","price":400,"description":"test description"}]
@client.command()
async def shop(ctx):
em = discord.Embed(title = "Dealership")
for item in mainshop:
name = item["name"]
price = item["price"]
desc = item["description"]
em.add_field(name = name, value = f"${price} | {desc}")
await ctx.send(embed =em)
@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 item does not exisit!")
return
if res [1]==2:
await ctx.send(f"You do not have enough money!")
return
await ctx.send(f"you just bought {amount} {item}")
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("bank.json","w") as f:
json.dump(users,f)
await update_bank(user,cost*-1,"Wallet")
return [True,"Worked"]```
Solution 1:[1]
you have a typing mistake in buy_this function.
item_name - item_name.lower()
You need to put '=' instead of '-', like this:
item_name = item_name.lower()
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Omer |
