Welcome!

By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!
  • Guest, before posting your code please take these rules into consideration:
    • It is required to use our BBCode feature to display your code. While within the editor click < / > or >_ and place your code within the BB Code prompt. This helps others with finding a solution by making it easier to read and easier to copy.
    • You can also use markdown to share your code. When using markdown your code will be automatically converted to BBCode. For help with markdown check out the markdown guide.
    • Don't share a wall of code. All we want is the problem area, the code related to your issue.


    To learn more about how to use our BBCode feature, please click here.

    Thank you, Code Forum.

json Duplicate object key

Bitgamer

New Coder
Hello,


I have an error with "Json and, Python" and The Error Is that "Python" Writes data into a "Json DB" but when updating it is not updated but a new entry is made

[CODE lang="python" title="Python Code"]import discord
from discord.ext import commands
import json
import os

token = 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX'
c = commands.Bot(command_prefix=')')
os.chdir(r'cogs/')

@c.event
async def on_ready():
print('Bot is ready!')

@c.event
async def on_member_join(member):
with open('users.json', 'r') as f:
users = json.load(f)

await update_data(users, member)

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

@c.event
async def on_message(message):
with open('users.json', 'r') as f:
users = json.load(f)

await update_data(users, message.author)
await add_experience(users, message.author , 5)
await level_up(users, message.author, message.channel)

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

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

async def add_experience(users, user, exp):
users[user.id]['experience'] += exp

async def level_up(users, user, channel):
experience = users[user.id]['experience']
lvl_start = users[user.id]['level']
lvl_end = int(experience ** (1/4))

if lvl_start < lvl_end:
await c.send_message(channel, '{} ist jetzt Level {}'.format(user.mention, lvl_end))
users[user.id]['level'] = lvl_end
c.run(token)[/CODE]





[CODE lang="json" title="Json DB" highlight="4-5"]{
"408692795180711946": {"experience": 5, "level": 1,
"874248407114448947": {"experience": 5, "level": 1,
"854953715735199746": {"experience": 5, "level": 1,
"854953715735199746": {"experience": 5, "level": 1,
}[/CODE]
 

New Threads

Buy us a coffee!

Back
Top Bottom