diff --git a/.gitignore b/.gitignore index 13fbe3e..792f7d9 100644 --- a/.gitignore +++ b/.gitignore @@ -139,3 +139,4 @@ TODO.txt sql_tester.py conf outputs +dakiny.db \ No newline at end of file diff --git a/create_database.py b/create_database.py new file mode 100644 index 0000000..2441e73 --- /dev/null +++ b/create_database.py @@ -0,0 +1,15 @@ +""" +Small script to create the database if it doesn't already exist. +""" + +import sqlite3 +from os.path import exists + +if not exists("dakiny.db"): + with sqlite3.connect("dakiny.db") as con: + cur = con.cursor() + + with open("database.sql", mode="r", encoding="utf-8") as file: + database_script = file.read() + cur.executescript(database_script) + con.commit() diff --git a/dakiny_bot.py b/dakiny_bot.py index 56df5f6..5aad6aa 100644 --- a/dakiny_bot.py +++ b/dakiny_bot.py @@ -6,6 +6,7 @@ import nextcord from hydra.core.config_store import ConfigStore from nextcord.ext import commands +import create_database from config import DakinyConfig cs = ConfigStore.instance() @@ -38,6 +39,7 @@ def main(cfg: DakinyConfig): async def on_ready(): """When Discord is connected""" assert bot.user is not None + create_database # pylint: disable=pointless-statement print(f"{bot.user.name} has connected to Discord!") # Run Discord bot diff --git a/database.sql b/database.sql new file mode 100644 index 0000000..024f015 --- /dev/null +++ b/database.sql @@ -0,0 +1,27 @@ +-- CREATE DATABASE dakiny; +-- use dakiny; + +CREATE TABLE ElectionInformation( + ElectionCycle varchar(30) not null unique, + ElectionChannel bigint unsigned, + PRIMARY KEY (ElectionCycle) +); + +CREATE TABLE CouncilMembers( + ElectionCycle varchar(30) not null unique, + MemberId bigint unsigned not null, + Elected boolean DEFAULT false, + HighCouncilElect boolean DEFAULT false, + DiscordManager boolean DEFAULT false, + ClanLeader boolean DEFAULT false, + FOREIGN KEY (ElectionCycle) REFERENCES ElectionInformation(ElectionCycle), + PRIMARY KEY (ElectionCycle, MemberId) +); + +CREATE TABLE Votes( + ElectionCycle varchar(30) not null unique, + VoterId bigint unsigned not null, + VotedForId bigint unsigned not null, + FOREIGN KEY (ElectionCycle) REFERENCES ElectionInformation(ElectionCycle), + PRIMARY KEY (ElectionCycle, VoterId) +); \ No newline at end of file