Skip to content
This repository has been archived by the owner on Jun 10, 2022. It is now read-only.

CRUD database for python discord bot developers that stores data on discord text channels

License

Notifications You must be signed in to change notification settings

Creatrix-Net/DiscordDatabase

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

45 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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
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

About

CRUD database for python discord bot developers that stores data on discord text channels

Resources

License

Stars

Watchers

Forks

Languages

  • Python 95.1%
  • Shell 4.9%