CRUD database for discord bots, using discord text channels to store data
Project description
Discord Database
A CRUD (Create Read Update Delete) database for python Discord bot developers. All data is stored in key-value pairs directly on discord in the form of text messages, in the text channel that you specify.
Installation
pip3 uninstall discord.py
pip3 install DiscordDatabase
Getting Started
Import libraries
import discord
from discord.ext import commands
from DiscordDatabase import DiscordDatabase
Retrieve the guild_id
of the server where you would like to store the data
DB_GUILD_ID = your_guild_id
Click here to see how to get guild_id
Initialize bot and database
bot = commands.Bot(command_prefix=">")
# OR
bot = discord.Client()
db = DiscordDatabase(bot, DB_GUILD_ID)
db functions can only be used when bot is ready
@bot.event
async def on_ready():
print("Bot is online")
database = await db.new("CATEGORY_NAME","CHANNEL_NAME")
...
bot.run("TOKEN")
Category and Channel will be created if they dont exist.
You can create as many databases needed as you want, with a unique channel or category name.
Acessing the database
Since the scope of database
object is limited inside on_ready()
we will use set()
, get()
and delete()
functions inside on_ready()
.
You can set the database
object to be a global class variable in you bot so you can use it anywhere you want.
Store data in the database
await database.set(KEY,VALUE)
Everything is stored as key and value pairs in the text channel you set earlier.
e.g.
await database.set("name","Ankush")
await database.set("name_list",["Ankush","Weeblet","ankooooSH"])
await database.set("age",18)
If a key already exists it will be updated with the new value
Get data from the database
value = await database.get(KEY)
returns None
if key doesnot exist
e.g.
name = await database.get("name")
# returns "Ankush"
names = await database.get("name_list")
# returns ["Ankush","Weeblet","ankooooSH"]
age = await database.get("age")
# returns 18
unknown = await database.get("phone_number")
# returns None because phone_number doesnot exist in database
Deleting data
delete()
returns the value of a key and deletes it.
await database.delete(KEY)
e.g.
name = await database.delete("name")
# returns name and deletes it
name = await database.delete("name")
#when run twice returns None
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
Built Distribution
File details
Details for the file DiscordDatabase-0.1.3.tar.gz
.
File metadata
- Download URL: DiscordDatabase-0.1.3.tar.gz
- Upload date:
- Size: 5.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.0 CPython/3.9.12
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 162afe92951f84aaa49b77c316b2067eadd8a7d2b9ba2f591f9930a8826f402c |
|
MD5 | cf63420677a54f73cfb764d4019c584b |
|
BLAKE2b-256 | 6c53a3fea52dd6d2d494f0231b279560f75147c79a6aee04812ba616cb733e6d |
File details
Details for the file DiscordDatabase-0.1.3-py3-none-any.whl
.
File metadata
- Download URL: DiscordDatabase-0.1.3-py3-none-any.whl
- Upload date:
- Size: 6.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.0 CPython/3.9.12
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | ed6d0b01be5dbffa31af02a75d0057b006bfa5c91e469327dd26244be93eb943 |
|
MD5 | 334189eb2d8cac6951d3f2c6e5a6c2f0 |
|
BLAKE2b-256 | f1f612abfb0be11bb3a88b1d46f7883add490f39d51862b15729bd1ae0e188c5 |