0

I'm using TinyDB in a project to store information for a discord bot. Whenever a user updates their "rolls", the changed values get cached but the document is not updating to reflect the changes. This results in the data not being saved whenever the bot restarts.

The user data looks like this:

{
        "id": "some guid",
        "userId": discordId,
        "guildId": guildId,
        "data": {
            "rolls": {},
            "bonus": {}
        },
        "active": true,
        "first": "john",
        "last": "doe"
    }

I have a command that is supposed to add a key and value to the "rolls" dictionary. Here is the code:

handler.py

def save_dice(ctx, key, value):
    for x in ['+','-','*','/']:
         value = x.join(value.split(x))
         value = value.replace(x, f" {x} ")
    character = database.get_active_character(ctx.author.id, ctx.guild.id)
    character["data"]["rolls"][key] = value
    database.update_character(character["id"], {"data": character["data"]})
    return f"roll \"{key}\" set to \"{value}\""

database.py

from tinydb import TinyDB, Query
from difflib import SequenceMatcher
from pprint import pprint
from uuid import uuid4
import json
import os

script_dir = os.path.dirname(__file__) #<-- absolute dir the script is in
Row = Query()

...

# id: uuid
# guildId: discord guildid
# userId: discord userid
# data: dict containing character stuff
# active: bool
# first: first name
# last: last name
characterTable = TinyDB(os.path.join(script_dir, 'data/characters.json'))

...

def update_character(id, changedValues):
    userTable.update(changedValues, (Row.id == id))
  • Did you manage to work out what was going on? I have a similar problem that I update the TinyDB table, even assert the updated record matches the write, but then when that record is called the old value is still there. – its_broke_again Sep 22 '21 at 10:21
  • Nope, I've set aside the project for a bit because I'm stuck on this one. – Seth Alexander Sep 23 '21 at 11:17

0 Answers0