Merge pull request #2 from sgoudham/database

pull/3/head
Hamothy 3 years ago committed by GitHub
commit 6bbaefb4a9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -1,5 +1,6 @@
import logging import logging
import os import os
from pathlib import Path
from dotenv import load_dotenv from dotenv import load_dotenv
@ -9,10 +10,14 @@ load_dotenv()
# Constants # Constants
BOT_TOKEN = os.environ.get("BOT_TOKEN") BOT_TOKEN = os.environ.get("BOT_TOKEN")
BOT_PREFIX = "!" BOT_PREFIX = "!"
DB_FILE = Path("db", "charity.db")
DB_FILE.parent.mkdir(parents=True, exist_ok=True)
LOG_FILE = Path("logs", "discord.log")
LOG_FILE.parent.mkdir(parents=True, exist_ok=True)
# Set Up Logging # Set Up Logging
logger = logging.getLogger('discord') LOGGER = logging.getLogger("discord")
logger.setLevel(logging.DEBUG) LOGGER.setLevel(logging.DEBUG)
handler = logging.FileHandler(filename='discord.log', encoding='utf-8', mode='w') handler = logging.FileHandler(filename=LOG_FILE, encoding="utf-8", mode="w")
handler.setFormatter(logging.Formatter('%(asctime)s:%(levelname)s:%(name)s: %(message)s')) handler.setFormatter(logging.Formatter("%(asctime)s:%(levelname)s:%(name)s: %(message)s"))
logger.addHandler(handler) LOGGER.addHandler(handler)

@ -0,0 +1,46 @@
import sqlite3
import sys
from abc import ABC, abstractmethod
from sqlite3 import Connection
from exclamation_mark_charity import DB_FILE, LOGGER
class DatabaseInterface(ABC):
@abstractmethod
def connect(self):
pass
@abstractmethod
def insert(self):
pass
@abstractmethod
def create(self):
pass
class Sqlite(DatabaseInterface):
def __init__(self):
self.conn: Connection = self.connect()
def connect(self) -> Connection:
LOGGER.info(f"Connecting to {DB_FILE}...")
try:
conn = sqlite3.connect(DB_FILE)
conn.execute("PRAGMA foreign_keys = ON;")
except sqlite3.Error as err:
LOGGER.error(err)
sys.exit(err)
LOGGER.info(f"Connection to {DB_FILE} successful!")
return conn
def create(self):
pass
def insert(self):
pass

@ -0,0 +1,80 @@
PRAGMA foreign_keys = ON;
DROP TABLE IF EXISTS TeamTournament;
DROP TABLE IF EXISTS Team;
DROP TABLE IF EXISTS Tournament;
DROP TABLE IF EXISTS University;
DROP TABLE IF EXISTS Player;
CREATE TABLE IF NOT EXISTS University
(
pk_university_id INTEGER PRIMARY KEY,
name TEXT UNIQUE NOT NULL
);
CREATE TABLE IF NOT EXISTS Team
(
pk_team_id INTEGER PRIMARY KEY,
name TEXT UNIQUE NOT NULL,
discord_category_id TEXT,
discord_text_channel_id TEXT,
discord_voice_channel_id TEXT,
fk_tournament_id INTEGER,
fk_university_id INTEGER,
FOREIGN KEY (fk_university_id)
REFERENCES University (pk_university_id)
ON UPDATE CASCADE
ON DELETE CASCADE,
FOREIGN KEY (fk_tournament_id)
REFERENCES Tournament (pk_tournament_id)
ON UPDATE CASCADE
ON DELETE CASCADE
);
CREATE TABLE IF NOT EXISTS Player
(
pk_player_id INTEGER PRIMARY KEY,
battlenet_id TEXT UNIQUE NOT NULL,
discord_tag TEXT,
skill_rating TEXT,
roles TEXT CHECK (roles in ('TANK', 'DPS', 'SUPPORT', 'FLEX')),
preferred_hero TEXT,
fk_university_id INTEGER,
fk_team_id INTEGER,
FOREIGN KEY (fk_university_id)
REFERENCES University (pk_university_id)
ON UPDATE CASCADE
ON DELETE SET NULL,
FOREIGN KEY (fk_team_id)
REFERENCES Team (pk_team_id)
ON UPDATE CASCADE
ON DELETE SET NULL
);
CREATE TABLE IF NOT EXISTS Tournament
(
pk_tournament_id INTEGER PRIMARY KEY,
name TEXT UNIQUE NOT NULL,
money_raised INTEGER DEFAULT 0,
mvp INTEGER DEFAULT NULL,
FOREIGN KEY (mvp)
REFERENCES Player (pk_player_id)
ON UPDATE CASCADE
ON DELETE SET NULL
);
CREATE TABLE IF NOT EXISTS TeamTournament
(
pk_fk_tournament_id INTEGER NOT NULL,
pk_fk_team_id INTEGER NOT NULL,
tournament_status TEXT CHECK (tournament_status in ('IN', 'OUT')),
PRIMARY KEY (pk_fk_tournament_id, pk_fk_team_id),
FOREIGN KEY (pk_fk_tournament_id)
REFERENCES Tournament (pk_tournament_id)
ON UPDATE CASCADE
ON DELETE CASCADE,
FOREIGN KEY (pk_fk_team_id)
REFERENCES Team (pk_team_id)
ON UPDATE CASCADE
ON DELETE CASCADE
);

@ -1,12 +1,10 @@
import discord from discord import Intents
from discord.ext import commands from discord.ext import commands
from discord.ext.commands import Context from discord.ext.commands import Context
from exclamation_mark_charity import BOT_PREFIX, BOT_TOKEN from exclamation_mark_charity import BOT_PREFIX, BOT_TOKEN
intents = discord.Intents.default() bot = commands.Bot(command_prefix=BOT_PREFIX, intents=Intents.all())
intents.members = True
bot = commands.Bot(command_prefix=BOT_PREFIX, description="idk", intents=intents)
@bot.command() @bot.command()

Loading…
Cancel
Save