Mika1784
Mika1784

Reputation: 11

Discord.py give command

i am trying to make a point system with a give command, like if a user completes a challenge a mod can give the user points, i already made a function for it to write the users ID and 0 points into a .json when they join (first box) but i can't seem to get the give command correctly(second box), and no errors show up, any help would be appreciated, thanks in advance :)

async def update_data(users, user): #
    if not user.id in users: #i called this in the member join event
      users[user.id] = {}
      users[user.id]['points'] = 0

this function is supposed to add points:

async def add_points(ctx, users, user, pts): #I wrote that function at the beginning of the script
      users[user.id]['points'] += pts

and this is the give command:

@client.command(pass_context = True)
    async def give(ctx, member, amount):
      with open('users.json', 'r') as f:
        users =  json.load(f)
        await add_points(users, member, amount) 
      with open('users.json', 'w') as f:                      
        json.dump('users', 'f') 
      await ctx.send(f'given {member} {amount} programming points')

(sorry for the lack of info)

Upvotes: 1

Views: 622

Answers (1)

StarbuckBarista
StarbuckBarista

Reputation: 1318

If users is a list, you can’t just index it like a dictionary.

for user in users:
    if user[“id”] == member.id:
        user[“points”] += AMOUNT

You should have some form of ID with their user id.

This is all assuming your JSON file looks like this.

{“points”: []}

Upvotes: 1

Related Questions