Discord Bot Database issue

Question: When creating the character, everything goes normally and whatever needs to be sent to the database gets sent. However, when i try to create another character, it allows me to do so (the code shouldn’t have allowed it by checking if my user ID is already in the database)

Repl link: https://replit.com/@AkmalYahaya/RPGGame#main.py

@bot.command(name="create", help="Creates a character.")
async def create(ctx, name=None):
  #await bot.process_commands("create")
  user_id = ctx.message.author.id
  #if no name, use the author's
  if not name:
    name = ctx.message.author.name

  #creates characters dictionary if it doesn't exist
  if "characters" not in db.keys():
    db["characters"] = {}

  #only create a new character if user doesn't have one already
  if user_id not in db["characters"] or not db["characters"][user_id]:
    print("not in database")
    character = Character(**{
      "name": name,
      "hp": 16,
      "max_hp": 16,
      "attack": 2,
      "defense": 1,
      "mana": 0,
      "level": 1,
      "xp": 0,
      "gold": 0,
      "inventory": [],
      "mode": GameMode.ADVENTURE,
      "battling": None,
      "user_id": user_id
    })
    character.save_to_db()
    await ctx.message.reply(f"New level 1 character created: {name}. Enter '!status' to see your stats.")
  else:
    await ctx.message.reply("You have already created your character.")
1 Like

To those who had a similar problem, I fixed it by adding str to the user ID bit in line 4: user_id = str(ctx.message.author.id)

1 Like

That’s great, thank you for sharing this @EnderBox