Skip to main content

Encrypted data storage with a format based on Discord

Project description

DiscordDataStorage

Summary

A 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.

Documentation

discord_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 data
member_template - the template to use when retrieving member data
server_template - the template to use when retrieving server data
bot_template - the template to use when retrieving bot data
storage_location - where to store the data files
If a relative storage_location is given, it is joined with home.
salt - the salt to use when creating user hashes

discord_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 read
user_id - the user id of the data to read
Raises:
FileNotFoundError - No data was found for the parameters, and no template exists for it
cryptography.fernet.InvalidToken - An invalid key was given when this object was constructed
Returns:
The retrieved data

discord_data_storage.DataAccessor.write(data, server_id="", user_id="")
Writes the data to the given parameters.
Parameters:
data - the data to write
server_id - the server id of the data to write
user_id - the user id of the data to write

discord_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 delete
user_id - the user id of the data to delete

discord_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 check
user_id - the user id of the data to check
Returns:
True if the data exists, False if not

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

discord_data_storage-0.1.0.tar.gz (4.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

discord_data_storage-0.1.0-py3-none-any.whl (5.5 kB view details)

Uploaded Python 3

File details

Details for the file discord_data_storage-0.1.0.tar.gz.

File metadata

  • Download URL: discord_data_storage-0.1.0.tar.gz
  • Upload date:
  • Size: 4.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.10.6

File hashes

Hashes for discord_data_storage-0.1.0.tar.gz
Algorithm Hash digest
SHA256 6b4e4c7e7d156653edfea6b80bd7f0a68851e62b822bb63777fed5f874f6e2c4
MD5 cfbd4100dc2398b4f2780d50b1a103c1
BLAKE2b-256 83e625bc11784202daefaf9fa203343c2f01e49c61b2e51b88db7f75050cb666

See more details on using hashes here.

File details

Details for the file discord_data_storage-0.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for discord_data_storage-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 4757d1e11cd2fc8ab6ef93ef03b47e7c389d7a4353290ba36416955685fcf511
MD5 ee253bc6b31b774b3d7a9405b7ae0bbe
BLAKE2b-256 b9bf441b9cf055354126da38b0537d308a61260af6d595ce55e807956cb486d9

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page