First time using replit database

i don’t know why my database from replit doesn’t seems working. What am i doing wrong?

ERROR    discord.app_commands.tree Ignoring exception in command 'create'
Traceback (most recent call last):
  File "/home/runner/RP-Utilities-Beta/.pythonlibs/lib/python3.10/site-packages/discord/app_commands/commands.py", line 827, in _do_call
    return await self._callback(self.binding, interaction, **params)  # type: ignore
  File "/home/runner/RP-Utilities-Beta/RP-Utilities-Beta/cogs/CharactersCog.py", line 132, in _character_default_create_slash
    response = "Error, the character with this prompt already exist." if await self.client.database.register_default_character(user_id=user, name=name, prompt_prefix=prompt, image=url) else "Character created."
  File "/home/runner/RP-Utilities-Beta/RP-Utilities-Beta/rpu_database.py", line 375, in register_default_character
    if not db['characters']:
  File "/home/runner/RP-Utilities-Beta/.pythonlibs/lib/python3.10/site-packages/replit/database/database.py", line 470, in __getitem__
    raw_val = self.get_raw(key)
  File "/home/runner/RP-Utilities-Beta/.pythonlibs/lib/python3.10/site-packages/replit/database/database.py", line 510, in get_raw
    raise KeyError(key)
KeyError: 'characters'

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/home/runner/RP-Utilities-Beta/.pythonlibs/lib/python3.10/site-packages/discord/app_commands/tree.py", line 1248, in _call
    await command._invoke_with_namespace(interaction, namespace)
  File "/home/runner/RP-Utilities-Beta/.pythonlibs/lib/python3.10/site-packages/discord/app_commands/commands.py", line 853, in _invoke_with_namespace
    return await self._do_call(interaction, transformed_values)
  File "/home/runner/RP-Utilities-Beta/.pythonlibs/lib/python3.10/site-packages/discord/app_commands/commands.py", line 846, in _do_call
    raise CommandInvokeError(self, e) from e
discord.app_commands.errors.CommandInvokeError: Command 'create' raised an exception: KeyError: 'characters'

my replit: https://replit.com/@HirukaRogue/RP-Utilities-Beta

1 Like

Hi @HirukaRogue !
It appears that ‘characters’ is not a key in the db.
Is characters a variable or a name of the key?

1 Like

According to the error, you haven’t set characters in your database.

Reason is in your ‘database wrapper’ code:

if not db['characters']:
  db['characters'] = {
    user_id: {
      'characters': list()
    }
  }

Is this code supposed to set characters when it doesn’t exist? If so, it should be this:

if 'characters' not in db:
  db['characters'] = {
    user_id: {
      'characters': list()
    }
  }
1 Like

uhmm, it’s suppose to be a database part, i don’t know how database from reeplit work

oh i see, time to test it out

and…:

ERROR    discord.app_commands.tree Ignoring exception in command 'create'
Traceback (most recent call last):
  File "/home/runner/RP-Utilities-Beta/.pythonlibs/lib/python3.10/site-packages/discord/app_commands/commands.py", line 827, in _do_call
    return await self._callback(self.binding, interaction, **params)  # type: ignore
  File "/home/runner/RP-Utilities-Beta/RP-Utilities-Beta/cogs/CharactersCog.py", line 132, in _character_default_create_slash
    response = "Error, the character with this prompt already exist." if await self.client.database.register_default_character(user_id=user, name=name, prompt_prefix=prompt, image=url) else "Character created."
  File "/home/runner/RP-Utilities-Beta/RP-Utilities-Beta/rpu_database.py", line 387, in register_default_character
    database = db['characters'][user_id] if db['characters'] else None
  File "/home/runner/RP-Utilities-Beta/.pythonlibs/lib/python3.10/site-packages/replit/database/database.py", line 339, in __getitem__
    return self.value[k]
KeyError: 1130268405333753857

Replace that with this:

database = db['characters'][user_id] if user_id in db['characters'] else None
2 Likes

There are many places in your code where you need to use if 'x' not in db instead of if db['x'].
In some places, you can use the db.get(key) method which returns None if the key is not in the database.

2 Likes

perfect, also i attempt to fix it by substituting db[‘characters’] to pivot, but the same error happens
before:db['characters'] = db['characters'] | database
after: db['characters'] = pivot | database

ERROR    discord.app_commands.tree Ignoring exception in command 'create'
Traceback (most recent call last):
  File "/home/runner/RP-Utilities-Beta/.pythonlibs/lib/python3.10/site-packages/discord/app_commands/commands.py", line 827, in _do_call
    return await self._callback(self.binding, interaction, **params)  # type: ignore
  File "/home/runner/RP-Utilities-Beta/RP-Utilities-Beta/cogs/CharactersCog.py", line 132, in _character_default_create_slash
    response = "Error, the character with this prompt already exist." if await self.client.database.register_default_character(user_id=user, name=name, prompt_prefix=prompt, image=url) else "Character created."
  File "/home/runner/RP-Utilities-Beta/RP-Utilities-Beta/rpu_database.py", line 394, in register_default_character
    db['characters'] = pivot | database
TypeError: unsupported operand type(s) for |: 'ObservedDict' and 'dict'

the same error happens the both ways

ReplitDB doesn’t return dicts, it returns observed ones. I’m not sure how that operator works, but assuming it works with dicts, this would work:

db['characters'] = pivot.value | database

okay, this one is difficult to understand:

ERROR    discord.app_commands.tree Ignoring exception in command 'create'
Traceback (most recent call last):
  File "/home/runner/RP-Utilities-Beta/.pythonlibs/lib/python3.10/site-packages/discord/app_commands/commands.py", line 827, in _do_call
    return await self._callback(self.binding, interaction, **params)  # type: ignore
  File "/home/runner/RP-Utilities-Beta/RP-Utilities-Beta/cogs/CharactersCog.py", line 132, in _character_default_create_slash
    response = "Error, the character with this prompt already exist." if await self.client.database.register_default_character(user_id=user, name=name, prompt_prefix=prompt, image=url) else "Character created."
  File "/home/runner/RP-Utilities-Beta/RP-Utilities-Beta/rpu_database.py", line 405, in register_default_character
    db['characters'][user_id]['characters'] = char_list
  File "/home/runner/RP-Utilities-Beta/.pythonlibs/lib/python3.10/site-packages/replit/database/database.py", line 339, in __getitem__
    return self.value[k]
KeyError: 1130268405333753857

It shows the KeyError, but i have no idea what that error means

I think I see it, look a few lines up:

I’m guessing user_id 1130268405333753857 isn’t in the DB, so you need to make sure it is before you run that code.

strange, it seems guaranteed by the code, but let me check

okay so, printing the value of db[‘characters’] i got this:

{'1130268405333753857': ObservedDict(value={'characters': ObservedList(value=[])})}

curious, it has the user_id at the same time it shows error… strange

This is a string, but…

is an int. That would be your issue. Here’s a way around that:

db['characters'][str(user_id)]['characters'] = char_list

okay so i never knew that replit db converts stuffs into string, time to test out

It doesn’t usually, so you probably set the user_id as a string once, and that’s how it got saved.

1 Like

strange pyuthon problem, why it didn’t returned false instead of returning Error?

ERROR    discord.ext.commands.bot Ignoring exception in command character default delete
Traceback (most recent call last):
  File "/home/runner/RP-Utilities-Beta/.pythonlibs/lib/python3.10/site-packages/discord/app_commands/commands.py", line 827, in _do_call
    return await self._callback(self.binding, interaction, **params)  # type: ignore
  File "/home/runner/RP-Utilities-Beta/RP-Utilities-Beta/cogs/CharactersCog.py", line 232, in _character_default_delete
    result = await ctx.bot.database.delete_default_character(
  File "/home/runner/RP-Utilities-Beta/RP-Utilities-Beta/rpu_database.py", line 452, in delete_default_character
    if name in i['name'] or prompt_prefix in i['prompt_prefix']:
TypeError: 'in <string>' requires string as left operand, not NoneType
name = None
prompt_prefix = 'test:'

That’s why. None cannot be passed to that method, you could set it to "" or '' instead though.

okay so thanks, this one helped me. Also how do i drop a table from replit db? I need to drop a thing