diff --git a/exclamation_mark_charity/__init__.py b/exclamation_mark_charity/__init__.py index 0e09845..c66639f 100644 --- a/exclamation_mark_charity/__init__.py +++ b/exclamation_mark_charity/__init__.py @@ -1,5 +1,6 @@ import logging import os +from pathlib import Path from dotenv import load_dotenv @@ -9,10 +10,14 @@ load_dotenv() # Constants BOT_TOKEN = os.environ.get("BOT_TOKEN") 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 -logger = logging.getLogger('discord') -logger.setLevel(logging.DEBUG) -handler = logging.FileHandler(filename='discord.log', encoding='utf-8', mode='w') -handler.setFormatter(logging.Formatter('%(asctime)s:%(levelname)s:%(name)s: %(message)s')) -logger.addHandler(handler) +LOGGER = logging.getLogger("discord") +LOGGER.setLevel(logging.DEBUG) +handler = logging.FileHandler(filename=LOG_FILE, encoding="utf-8", mode="w") +handler.setFormatter(logging.Formatter("%(asctime)s:%(levelname)s:%(name)s: %(message)s")) +LOGGER.addHandler(handler) diff --git a/exclamation_mark_charity/database.py b/exclamation_mark_charity/database.py new file mode 100644 index 0000000..ceb9102 --- /dev/null +++ b/exclamation_mark_charity/database.py @@ -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 diff --git a/exclamation_mark_charity/db/charity.db b/exclamation_mark_charity/db/charity.db new file mode 100644 index 0000000..67b38ed Binary files /dev/null and b/exclamation_mark_charity/db/charity.db differ diff --git a/exclamation_mark_charity/db/setup.sql b/exclamation_mark_charity/db/setup.sql new file mode 100644 index 0000000..5d420d0 --- /dev/null +++ b/exclamation_mark_charity/db/setup.sql @@ -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 +); \ No newline at end of file diff --git a/exclamation_mark_charity/runner.py b/exclamation_mark_charity/runner.py index b5f531e..003f24a 100644 --- a/exclamation_mark_charity/runner.py +++ b/exclamation_mark_charity/runner.py @@ -1,12 +1,10 @@ -import discord +from discord import Intents from discord.ext import commands from discord.ext.commands import Context from exclamation_mark_charity import BOT_PREFIX, BOT_TOKEN -intents = discord.Intents.default() -intents.members = True -bot = commands.Bot(command_prefix=BOT_PREFIX, description="idk", intents=intents) +bot = commands.Bot(command_prefix=BOT_PREFIX, intents=Intents.all()) @bot.command()