0

The directory is changed to the folder with the JSON file after importing everything like discord, os, json, etc.

@client.command()
async def gift(ctx,*, member: discord.Member):
    with open('users.json', 'r') as f:
        users = json.load(users, f)

        await update_data(users, discord.member)
        await add_coin(users, discord.member, 1)

        with open('users.json', 'w') as f:
            users = json.dump(users, f)

async def update_data(users, user):
    if not user.id in users:
        users[user.id] = {}
        users[user.id]['coins'] = 0

async def add_coin(users, user, coin):
    users[user.id]['coins'] += coin
    await client.send(channel,'{} has {} coins'.format(user.id, coin))

When the command is run, the bot gives an error:

UnboundLocalError: local variable "users" referenced before assignment.

realr
  • 3,652
  • 6
  • 23
  • 34
jitter
  • 29
  • 1
  • 1
  • 8
  • Welcome to Stack Overflow! I see you have a problem with `users = json.load(users, f)`. This should be `users = json.load(f)`. Try it! See https://stackoverflow.com/questions/9264763/dont-understand-why-unboundlocalerror-occurs – Jaideep Shekhar Jan 11 '20 at 12:12
  • In the future, please show the full traceback of the error – OneCricketeer Jan 11 '20 at 17:43

1 Answers1

0

As Jaideep Shekhar answered, users is referenced in the call to json.load before it's assigned.
json.load only accepts a single positional argument, so you'll want to pass only the file pointer.

Harmon758
  • 5,084
  • 3
  • 22
  • 39