Quantcast
Viewing all articles
Browse latest Browse all 14389

JSON causing multiple of the same key in a python dictionary for my Discord bot. What should I do? [closed]

I am coding a multipurpose Discord bot, but all my commands all terminate when the server they run in is not in my dict data. This is great, but it terminates even when the server is in that dict.

When I run /setup (the command to add your server to my dict), the entry gets added fine, but when I run it again, instead of resetting the existing entry to my template, it adds a new entry. Why is this? My code is below.

# to load the datadef load_data():  global data  with open("data.json", "r") as f:    data = json.load(f)#to savedef load_data():  global data  with open("data.json", "w") as f:    data = json.dump(f)# setup command@bot.tree.command(name="setup",                  description="Resets all settings, but can fix errors. ")@commands.has_permissions(manage_guild=True)async def setup(interaction: discord.Interaction):  load_data()  data[interaction.guild.id] = templates["guild"] save_data(data)  await interaction.response.send_message("All settings have been reset. ")# on message@bot.eventasync def on_message(message):  load_data()  author_id = message.author.id  guild_id = message.guild.id  try:    guild_data = data[guild_id]  except KeyError:    print("guild is not set up")    return  if message.author == bot.user:    return  try:    member = data[guild_id]["members"][author_id]  except KeyError:    member = templates["member"]  asyncio.run(leveling.on_message(self, message)) #run the on_message block in a cog  save_data(data)

And here's my template JSON:
{"guild":{"settings": {"leveling": {"notify_channel": "","notify_message": "","timeout": 0,"xp_by_message": 5    },"antiraid": {"bans_per_min": 999,"kicks_per_min": 999,"channel_delete_per_min": 999    }  },"members": {}},"member": {"leveling": {"level": 0,"xp": 0,"messages": 0,"last_message": 0    }  }}

Viewing all articles
Browse latest Browse all 14389

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>