Unknown_Shell
Unknown_Shell

Reputation: 115

Discord.py Economy command

I'm making an economy command for my bot. The error I believe is in the open_account() function. When you run the balance command it sees that I do not have an account and writes into the JSON file as it should but does not send the embed. I checked the embed and it works fine. It's something before calling the embed. Also, the beg command does not add the amount you make to the JSON file. I've spent days trying to understand what the problem is with no luck. Anyone can help me figure out where I messed up?

@client.command()
async def balance(ctx):await open_account(ctx.author)

    users = await get_bank_data()


    wallet_amt = users[str(user.id)]["wallet"]
    bank_amt = users[str(user.id)]["bank"]

    embed=discord.Embed(title="{}s balance:".format(member.name), color=0xe20303)
    embed.add_field(name="Wallet:", value=wallet_amt, inline=False)
    embed.add_field(name="Bank:", value=bank_amt, inline=False)

    await ctx.send(embed=embed)

async def open_account(user):
    users = await get_bank_data()

    if str(user.id) in users:
        return False
    else:
        users[str(user.id)] = {}
        users[str(user.id)]["wallet"] = 0
        users[str(user.id)]["bank"] = 0

    with open("bank.json", "w") as f:
        json.dump(users, f)
    return True

async def get_bank_data():
    with open("bank.json", "r") as f:
        users = json.load(f)
    return users


@client.command()
async def beg(ctx):

    users = await get_bank_data()
    user = ctx.author
    earnings = random.randrange(2000)

    if earnings == 0:
        await ctx.send(f"How unlucky... You didn't get anything...")

    elif earnings > 50:
        await ctx.send(f"Nice you got ${earnings} from a cool dude")

    elif earnings > 100:
        await ctx.send(f"Someone felt nice and gave you ${earnings}")

    elif earnings > 500:
        await ctx.send(f"You seem to have a way with people! Someone gave you ${earnings}")

    elif earnings > 800:
        await ctx.send(f"What a lucky day!! Someone gave you ${earnings}")

    elif earnings > 1500:
        await ctx.send(f"A rich man passed by you and felt bad. So ha gave you ${earnings}")

    elif earnings > 2000:
        await ctx.send(f"A shady man walked up to you and said 'I know how tough it can be out here' before giving you ${earnings}")


    users[srt(user.id)]["wallet"] += earnings

    with open("bank.json", "r") as f:
        users = json.load(f)

Upvotes: 0

Views: 5039

Answers (5)

Justyn
Justyn

Reputation: 85

First you need to remove await and async from get_bank_data & open_account
Second you need to give user = ctx.author into balance
Third you need to repair your typo in beg command users[srt(user.id)]["wallet"] += earnings into users[str(user.id)]["wallet"] += earnings
Fourth you need to use

with open("bank.json", "w") as f:
    json.dump(users, f)

instead

with open("bank.json", "r") as f:
    users = json.load(f)

And that's it. It should work, if not you can write under this Answer and we can edit it.

E:
Also I used embed=discord.Embed(title=f"{ctx.author.name}s balance:", color=0xe20303) in balance

Upvotes: 1

Thanatos
Thanatos

Reputation: 1

Not sure if you solved the problem yet, but the problem lies within the balance command, define the variable user as ctx.author just like you did in the beg command. Then you have an typo within where you add the earnings to the account, all you have to do is change that to str. That’s all I see is wrong with the code.

Upvotes: 0

Skillz Gamin
Skillz Gamin

Reputation: 51

In the third last line, you misspelled str as srt

Upvotes: 0

a helper
a helper

Reputation: 1

Also, at the beginning try spacing the async def balance(ctx):await open_account(ctx.author) the await open_account should be on a different line.

Upvotes: 0

TippTop
TippTop

Reputation: 1

Functions open_account and get_bank_data are no asynchronous functions. Try to remove the async/await before them.

Upvotes: 0

Related Questions