Home > database >  Changing the value of a nested dictionary changes other nested dictionary values
Changing the value of a nested dictionary changes other nested dictionary values

Time:02-20

*sorry if the title doesn't make sense I'm not sure how to word it

So I started making a python discord bot in the library of Pycord. I wanted to make a bot that will work on multiple servers and each server will have different values. So, to do this I made a dictionary that will store all these values within a nested dictionary. However, when I tried to change the value of one nested dictionary, it changes the values in the other nested dictionaries.

Code:

testing_servers = [912361242985918464, 938245167880753202]
server_ids = {}
default_server_vals = {'beetle_game_started': False, 'beetle_message_id': None,
'beetle_message_channel': None, 'beetle_player_1': None, 'beetle_player_2': None, 'beetle_game_on': False, 'player1_list' : [], "player2_list":[]}

@bot.event
async def on_ready():
    print('logged in')
    for i in bot.guilds:
        global server_ids
        global default_server_vals
        server_ids[str(i.id)] = default_server_vals
    print(server_ids)
@bot.event
async def on_guild_join(guild):
    server_ids[str(guild.id)] = default_server_vals

@bot.slash_command(guild_ids=testing_servers, name="test", description="Test out bot latency")
async def test(ctx):
    await ctx.respond(f"Bot Latency: {bot.latency * 100}")


@bot.slash_command(guild_ids=testing_servers, name="eightball", description="Play 8ball with friends")
async def eightball(ctx, question):
    eightball_int = random.randint(1, 5)
    response = None
    if eightball_int == 1:
        response = "I don't quite know"
    if eightball_int == 2:
        response = "Well no."
    if eightball_int == 3:
        response = "Yes of course!"
    if eightball_int == 4:
        response = "Maybe it's best not to answer."
    if eightball_int == 5:
        response = "Bruh Moment."
    embed = discord.Embed(title="Eightball", description=f"""You asked: {question}
My response: {response}""", colour=discord.Colour.green())
    await ctx.respond(embed=embed)


@bot.slash_command(guild_ids=testing_servers, name="roll_dice", description="Roll a dice!")
async def roll_dice(ctx, sides: int):
    dice_int = random.randint(1, int(sides))
    embed = discord.Embed(title="Dice", colour=discord.Colour.green())
    embed.add_field(name="You rolled a:", value=str(dice_int))
    embed.add_field(name="Dice sides:", value=str(sides))
    await ctx.respond(embed=embed)


@bot.slash_command(guild_ids=testing_servers, name="beetle", description="2 Player game")
async def beetle(ctx):
    print(server_ids[str(ctx.guild.id)].get('beetle_game_on'), server_ids[str(ctx.guild.id)].get('beetle_game_started'))

    if server_ids[str(ctx.guild.id)].get('beetle_game_on') == False and server_ids[str(ctx.guild.id)].get('beetle_game_started') == False:
        await ctx.respond("Game starting! React to join.")
        game_start_embed = discord.Embed(title="React to join beetle game! (2 Players Only)",
                                         colour=discord.Colour.green())
        game_start_embed.add_field(name="GAME RULES", value="""There are two players. There is one dice! The first player to finish the beetle drawing wins. 
    Rolling a 1 – Body
    
    Rolling a 2 – Head
    
    Rolling a 3 – A leg
    
    Rolling a 4 – An eye
    
    Rolling a 5 – An antenna
    
    Rolling a 6 – The tail
    The first player to roll all 6 wins. However, the head and body must be drawn first to draw the other beetle parts.""")
        message = await ctx.send(embed=game_start_embed)
        await message.add_reaction("           
  • Related