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))