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
Metadata
Release files for DiscordDatabase 0.1.3
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| DiscordDatabase-0.1.3.tar.gz | 5.4 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| DiscordDatabase-0.1.3-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 11.6 kB
Release files / DiscordDatabase-0.1.3.tar.gz
| Download URL | DiscordDatabase-0.1.3.tar.gz |
|---|---|
| Size | 5.4 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
162afe92951f84aaa49b77c316b2067eadd8a7d2b9ba2f591f9930a8826f402c
|
|
BLAKE2b-256 checksum How to use checksums |
6c53a3fea52dd6d2d494f0231b279560f75147c79a6aee04812ba616cb733e6d
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/4.0.0 CPython/3.9.12
|
Release files / DiscordDatabase-0.1.3-py3-none-any.whl
| Download URL | DiscordDatabase-0.1.3-py3-none-any.whl |
|---|---|
| Size | 6.3 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
ed6d0b01be5dbffa31af02a75d0057b006bfa5c91e469327dd26244be93eb943
|
|
BLAKE2b-256 checksum How to use checksums |
f1f612abfb0be11bb3a88b1d46f7883add490f39d51862b15729bd1ae0e188c5
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/4.0.0 CPython/3.9.12
|