Package: discord-data-storage
DiscordDataStorageSummaryA simple data storage module I made for when creating Discord bots.Uses the cryptography module for encryption. Although this is designed for Discord, it isn't dependent on any Discord specific features or modules, so it could be used for any data storage.When reading or writing data, it uses server ids and user ids, which can be omitted. If neither is passed, it returns a global data file, useful for data that might be meant for the entire bot. Passing only the server id can be used to store data for a server itself, such as configuring roles or channels. Passing only the user id can be used for storing data to a user that is shared across all servers, such as bot preferences. Passing both can be used to store user data specific to a server, such as levels.This also supports templates for each of the four combinations. If a non-dictionary type is passed, it is used as a default when data doesn't exist. If a dictionary is passed, it goes over each key and sets the value to match the template if it doesn't exist in the data, also recursively calling itself for any dictionary values. Note that templates are not applied to existing data. They are applied before returning the data when reading, where they will theoretically get written later. As such, if a new template is given to the constructor and then later reverted, any data that wasn't read and then written with the new template will not return with the new template after it is reverted.Documentationdiscord_data_storage.DataAccessor(key, user_template=None, member_template=None, server_template=None, bot_template=None, storage_location="DiscordDataStorage", salt=b"")The main class used to read and write data.Parameters:key - The fernet key to use for encrypting data.user_template - the template to use when retrieving user datamember_template - the template to use when retrieving member dataserver_template - the template to use when retrieving server databot_template - the template to use when retrieving bot datastorage_location - where to store the data filesIf a relative storage_location is given, it is joined with home.salt - the salt to use when creating user hashesdiscord_data_storage.DataAccessor.read(server_id="", user_id="")Reads the data matching the given parameters.Parameters:server_id - the server id of the data to readuser_id - the user id of the data to readRaises:FileNotFoundError - No data was found for the parameters, and no template exists for itcryptography.fernet.InvalidToken - An invalid key was given when this object was constructedReturns:The retrieved datadiscord_data_storage.DataAccessor.write(data, server_id="", user_id="")Writes the data to the given parameters.Parameters:data - the data to writeserver_id - the server id of the data to writeuser_id - the user id of the data to writediscord_data_storage.DataAccessor.delete(server_id="", user_id="")Deletes the data matching the given parameters if it exists.Parameters:server_id - the server id of the data to deleteuser_id - the user id of the data to deletediscord_data_storage.DataAccessor.data_exists(server_id="", user_id="")Checks if data exists for the given parameters.Parameters:server_id - the server id of the data to checkuser_id - the user id of the data to checkReturns:True if the data exists, False if not