# 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`:
```bash
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.
Raw data
{
"_id": null,
"home_page": "https://github.com/yourusername/discord_save",
"name": "discord-save",
"maintainer": null,
"docs_url": null,
"requires_python": ">=3.6",
"maintainer_email": null,
"keywords": null,
"author": "Your Name",
"author_email": "your.email@example.com",
"download_url": "https://files.pythonhosted.org/packages/ec/26/8324c005bc46ac2819727bbcfae7a4ef9d64bc10666d06407ff2480047ea/discord_save-0.1.0.tar.gz",
"platform": null,
"description": "# discord_save\r\n\r\n`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.\r\n\r\n## Features\r\n\r\n- **Persistent data storage**: Save user stats and other data in a file (e.g., JSON).\r\n- **Easy to use**: Simple functions to get, update, and reset user data.\r\n- **Customizable**: Easily adapt the structure to store different kinds of data.\r\n- **Error handling**: Ensures data is properly loaded and saved, with safety checks for missing or corrupted files.\r\n\r\n## Installation\r\n\r\nYou can install the library using `pip`:\r\n\r\n```bash\r\npip install discord_save\r\n\r\nOr you can manually clone this repository and install it:\r\n git clone https://github.com/yourusername/discord_save.git\r\n cd discord_save\r\n pip install .\r\n\r\nUsage\r\nHere is a simple example of how to integrate discord_save into your Discord bot:\r\n\r\n from discord.ext import commands\r\n from discord_save import SaveManager\r\n\r\n # Initialize bot and save manager\r\n bot = commands.Bot(command_prefix=\"!\")\r\n save_manager = SaveManager(\"save_data.json\")\r\n\r\n @bot.event\r\n async def on_ready():\r\n print(f\"Logged in as {bot.user}\")\r\n\r\n @bot.command()\r\n async def balance(ctx):\r\n user_id = str(ctx.author.id)\r\n user_data = save_manager.get_user(user_id)\r\n await ctx.send(f\"{ctx.author.name}, your balance is {user_data['balance']}.\")\r\n\r\n @bot.command()\r\n async def add_balance(ctx, amount: int):\r\n user_id = str(ctx.author.id)\r\n user_data = save_manager.get_user(user_id)\r\n new_balance = user_data[\"balance\"] + amount\r\n save_manager.update_user(user_id, balance=new_balance)\r\n save_manager.save() # Don't forget to save after updates\r\n await ctx.send(f\"{ctx.author.name}, your new balance is {new_balance}.\")\r\n\r\nAPI Reference\r\nSaveManager(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.\r\n\r\nget_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.\r\n\r\nupdate_user(user_id, **kwargs): Updates a user's stats with the specified keyword arguments.\r\n\r\nsave(): Saves all changes to the data file.\r\n\r\nreset_user(user_id): Resets a user's stats to their default values.\r\n\r\nContributing\r\nIf 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!\r\n\r\nFork the repository.\r\nClone your fork locally.\r\nCreate a branch (git checkout -b feature-name).\r\nMake your changes.\r\nCommit your changes (git commit -am 'Add feature').\r\nPush to your fork (git push origin feature-name).\r\nOpen a pull request.\r\nLicense\r\nThis project is licensed under the MIT License - see the LICENSE file for details.\r\n\r\nAcknowledgements\r\ndiscord.py - The library used to build the Discord bot.\r\nJSON - The format used for saving data.\r\n",
"bugtrack_url": null,
"license": null,
"summary": "A library for managing save files for Discord bots.",
"version": "0.1.0",
"project_urls": {
"Homepage": "https://github.com/yourusername/discord_save"
},
"split_keywords": [],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "772b8fd79e0a62aeb5771dbb65dc5defc5b313d94c72dbf673578ff373aa548d",
"md5": "fdf3e949f9abb6bb417720bb4aaf73ff",
"sha256": "132e76a85afe6215b11234d1f7c88051671f252100edc554380fd5596a397a04"
},
"downloads": -1,
"filename": "discord_save-0.1.0-py3-none-any.whl",
"has_sig": false,
"md5_digest": "fdf3e949f9abb6bb417720bb4aaf73ff",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": ">=3.6",
"size": 4474,
"upload_time": "2024-11-26T02:45:51",
"upload_time_iso_8601": "2024-11-26T02:45:51.620862Z",
"url": "https://files.pythonhosted.org/packages/77/2b/8fd79e0a62aeb5771dbb65dc5defc5b313d94c72dbf673578ff373aa548d/discord_save-0.1.0-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "ec268324c005bc46ac2819727bbcfae7a4ef9d64bc10666d06407ff2480047ea",
"md5": "a409b68db61a42e84199a3eb291374e4",
"sha256": "7a0a65151a89512381f7964084a8c1f94bf535a87eed930f6cb9e39c6ac0b9ed"
},
"downloads": -1,
"filename": "discord_save-0.1.0.tar.gz",
"has_sig": false,
"md5_digest": "a409b68db61a42e84199a3eb291374e4",
"packagetype": "sdist",
"python_version": "source",
"requires_python": ">=3.6",
"size": 3941,
"upload_time": "2024-11-26T02:45:52",
"upload_time_iso_8601": "2024-11-26T02:45:52.741429Z",
"url": "https://files.pythonhosted.org/packages/ec/26/8324c005bc46ac2819727bbcfae7a4ef9d64bc10666d06407ff2480047ea/discord_save-0.1.0.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2024-11-26 02:45:52",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "yourusername",
"github_project": "discord_save",
"github_not_found": true,
"lcname": "discord-save"
}