Skip to main content

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

discord.py has been discontinued use py-cord instead.

pip3 uninstall discord.py
pip3 install py-cord
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 = 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

database.set(KEY,VALUE)

Everything is stored as key and value pairs in the text channel you set earlier.

e.g.

database.set("name","Ankush")

database.set("name_list",["Ankush","Weeblet","ankooooSH"])

database.set("age",18)

If a key already exists it will be updated with the new value

Get data from the database

value = database.get(KEY)

returns None if key doesnot exist

e.g.

name = database.get("name")
# returns "Ankush"

names = database.get("name_list") 
# returns ["Ankush","Weeblet","ankooooSH"]

age = database.get("age")
# returns 18

unknown = 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.

database.delete(KEY)

e.g.

name = database.delete("name")
# returns name and deletes it

name = database.delete("name")
#when run twice returns None

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

DiscordDatabase-0.1.2.tar.gz (5.3 kB view details)

Uploaded Source

Built Distribution

DiscordDatabase-0.1.2-py3-none-any.whl (6.2 kB view details)

Uploaded Python 3

File details

Details for the file DiscordDatabase-0.1.2.tar.gz.

File metadata

  • Download URL: DiscordDatabase-0.1.2.tar.gz
  • Upload date:
  • Size: 5.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.2 importlib_metadata/4.8.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.9.7

File hashes

Hashes for DiscordDatabase-0.1.2.tar.gz
Algorithm Hash digest
SHA256 7a5e5f973bc1796622cbeae1089e3e614527323d18e1e466e7ad68e53351b097
MD5 41385ca1f9b94d31d708e8c13e5a1aef
BLAKE2b-256 d8e1819bd16521dcfc46609ad71e39317a8e543db60cbcd0a9ecd8d5ffdd3a2d

See more details on using hashes here.

File details

Details for the file DiscordDatabase-0.1.2-py3-none-any.whl.

File metadata

  • Download URL: DiscordDatabase-0.1.2-py3-none-any.whl
  • Upload date:
  • Size: 6.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.2 importlib_metadata/4.8.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.9.7

File hashes

Hashes for DiscordDatabase-0.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 50715b0b1980562d5afaecfa34fecfe17031b8722b0560deee9e22946cb1b68c
MD5 64cd3e81fa3b68db29cc35d356e7d87a
BLAKE2b-256 0bba5efa7b75bd159107870de4f4755b937891bb396b93bafce91cefc67ebe9f

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page