Skip to main content

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

Metadata

Release files for discord-data-storage 0.1.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for discord-data-storage 0.1.0
File Size Uploaded
discord_data_storage-0.1.0.tar.gz 4.8 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for discord-data-storage 0.1.0
File Interpreter ABI Platform
discord_data_storage-0.1.0-py3-none-any.whl Python 3 none any Details

Total release size: 10.3 kB

Release files / discord_data_storage-0.1.0.tar.gz

Download URL discord_data_storage-0.1.0.tar.gz
Size 4.8 kB
Tags Source
SHA-256 checksum
How to use checksums
6b4e4c7e7d156653edfea6b80bd7f0a68851e62b822bb63777fed5f874f6e2c4
BLAKE2b-256 checksum
How to use checksums
83e625bc11784202daefaf9fa203343c2f01e49c61b2e51b88db7f75050cb666
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.10.6

Release files / discord_data_storage-0.1.0-py3-none-any.whl

Download URL discord_data_storage-0.1.0-py3-none-any.whl
Size 5.5 kB
Tags Python 3
SHA-256 checksum
How to use checksums
4757d1e11cd2fc8ab6ef93ef03b47e7c389d7a4353290ba36416955685fcf511
BLAKE2b-256 checksum
How to use checksums
b9bf441b9cf055354126da38b0537d308a61260af6d595ce55e807956cb486d9
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.10.6

Release history Release notifications | RSS feed

This release

0.1.0 This release

2 release files

0.0.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page