Skip to content

jfechete/DiscordDataStorage

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

16 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages