0

So I'm new to python, and I wanted to make an economy bot. I followed some tutorials, but my program does not work, it says "users" isn't defined, even though I think I defined it.

My code:

import discord
from discord.ext import commands
import json
client = commands.Bot(command_prefix='`')


@client.event
async def on_ready():
    print("We have logged into this server as {0.user}".format(client))


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

    users = await get_bank_data() #Here's where I think I defined it

    purse_amt = users[str(user.id)]["purse"]
    bank_amt = users[str(user.id)]["bank"]
    em = discord.Embed(title=f"{ctx.author.name}'s balance", color=discord.color.purple())
    em.add.field(name="Purse", value=purse_amt)
    em.add.field(name="Bank", value=bank_amt)
    await ctx.send(embed=em)


async def open_account(user):
    await get_bank_data()

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

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


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


client.run(token)

Edit: Here is my error message, I'm not sure what all of it means, but I think the main thing it's saying is that "users" is not defined:

Ignoring exception in command balance:
Traceback (most recent call last):
  File "C:\Users\Cyfix", line 85, in wrapped
    ret = await coro(*args, **kwargs)
  File "C:\Users\Cyfix", line 14, in balance
    await open_account(ctx.author)
  File "C:\Users\Cyfix", line 29, in open_account
    if str(user.id) in users:
NameError: name 'users' is not defined

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "C:\Users\Cyfix", line 939, in invoke
    await ctx.command.invoke(ctx)
  File "C:\Users\Cyfix", line 863, in invoke
    await injected(*ctx.args, **ctx.kwargs)
  File "C:\Users\Cyfix", line 94, in wrapped
    raise CommandInvokeError(exc) from exc
discord.ext.commands.errors.CommandInvokeError: Command raised an exception: NameError: name 'users' is not defined
Cyfix
  • 3
  • 4
  • Where does it say it isn't defined? It's certainly not defined in `open_account` and it's possibly not defined in `get_bank_data` (I'm not exactly sure about this one). – mkrieger1 Aug 17 '21 at 21:01
  • 1
    Please update your question with the full error traceback. – quamrana Aug 17 '21 at 21:01
  • 1
    Does this answer your question? [Short description of the scoping rules?](https://stackoverflow.com/questions/291978/short-description-of-the-scoping-rules) – mkrieger1 Aug 17 '21 at 21:04

1 Answers1

2

You meant to keep hold of the users returned:

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

    if str(user.id) in users:
        return False
    ...
quamrana
  • 37,849
  • 12
  • 53
  • 71