A library for managing save files for Discord bots.
Project description
discord_save
discord_save
is a Python library designed to simplify the process of saving and loading user data in Discord bots. It allows you to easily manage user stats such as balance, games played, and more, without worrying about data loss when the bot restarts.
Features
- Persistent data storage: Save user stats and other data in a file (e.g., JSON).
- Easy to use: Simple functions to get, update, and reset user data.
- Customizable: Easily adapt the structure to store different kinds of data.
- Error handling: Ensures data is properly loaded and saved, with safety checks for missing or corrupted files.
Installation
You can install the library using pip
:
pip install discord_save
Or you can manually clone this repository and install it:
git clone https://github.com/yourusername/discord_save.git
cd discord_save
pip install .
Usage
Here is a simple example of how to integrate discord_save into your Discord bot:
from discord.ext import commands
from discord_save import SaveManager
# Initialize bot and save manager
bot = commands.Bot(command_prefix="!")
save_manager = SaveManager("save_data.json")
@bot.event
async def on_ready():
print(f"Logged in as {bot.user}")
@bot.command()
async def balance(ctx):
user_id = str(ctx.author.id)
user_data = save_manager.get_user(user_id)
await ctx.send(f"{ctx.author.name}, your balance is {user_data['balance']}.")
@bot.command()
async def add_balance(ctx, amount: int):
user_id = str(ctx.author.id)
user_data = save_manager.get_user(user_id)
new_balance = user_data["balance"] + amount
save_manager.update_user(user_id, balance=new_balance)
save_manager.save() # Don't forget to save after updates
await ctx.send(f"{ctx.author.name}, your new balance is {new_balance}.")
API Reference
SaveManager(save_file='save_data.json'): Initializes the SaveManager with a specified file. If no file is specified, the default "save_data.json" will be used.
get_user(user_id): Retrieves the data for a specific user by their Discord user ID. If no data exists, it will create a new entry with default values.
update_user(user_id, **kwargs): Updates a user's stats with the specified keyword arguments.
save(): Saves all changes to the data file.
reset_user(user_id): Resets a user's stats to their default values.
Contributing
If you'd like to contribute to discord_save, feel free to fork the repository and submit a pull request. We welcome any improvements, bug fixes, or suggestions!
Fork the repository.
Clone your fork locally.
Create a branch (git checkout -b feature-name).
Make your changes.
Commit your changes (git commit -am 'Add feature').
Push to your fork (git push origin feature-name).
Open a pull request.
License
This project is licensed under the MIT License - see the LICENSE file for details.
Acknowledgements
discord.py - The library used to build the Discord bot.
JSON - The format used for saving data.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
discord_save-0.1.0.tar.gz
(3.9 kB
view details)
Built Distribution
File details
Details for the file discord_save-0.1.0.tar.gz
.
File metadata
- Download URL: discord_save-0.1.0.tar.gz
- Upload date:
- Size: 3.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.12.0
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 7a0a65151a89512381f7964084a8c1f94bf535a87eed930f6cb9e39c6ac0b9ed |
|
MD5 | a409b68db61a42e84199a3eb291374e4 |
|
BLAKE2b-256 | ec268324c005bc46ac2819727bbcfae7a4ef9d64bc10666d06407ff2480047ea |
File details
Details for the file discord_save-0.1.0-py3-none-any.whl
.
File metadata
- Download URL: discord_save-0.1.0-py3-none-any.whl
- Upload date:
- Size: 4.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.12.0
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 132e76a85afe6215b11234d1f7c88051671f252100edc554380fd5596a397a04 |
|
MD5 | fdf3e949f9abb6bb417720bb4aaf73ff |
|
BLAKE2b-256 | 772b8fd79e0a62aeb5771dbb65dc5defc5b313d94c72dbf673578ff373aa548d |