create data models, create and retrieve function skeletons
This commit is contained in:
parent
d27f3cad8a
commit
ae1a56342c
113
bot/database.py
113
bot/database.py
|
@ -1,52 +1,95 @@
|
|||
import sqlite3
|
||||
import discord
|
||||
from model import WatchGuild, WatchChannel, WatchUser, WatchMessage
|
||||
|
||||
|
||||
class DatabaseManager:
|
||||
'''
|
||||
DatabaseManager manages all database operations
|
||||
|
||||
Contains a single databse connection object.
|
||||
|
||||
Attributes:
|
||||
name: name of the database file.
|
||||
conn: sqlite3 connection object to the database file.
|
||||
'''
|
||||
|
||||
def __init__(self, name: str):
|
||||
self.conn = sqlite3.connect(name)
|
||||
'''
|
||||
Create a DatabaseManager
|
||||
|
||||
Initializes a DatabaseManager and a sqlite3 database connection.
|
||||
If the database file does not exist, create one and the required tables
|
||||
name: name of the sqlite file
|
||||
'''
|
||||
self.name = name
|
||||
self.conn = sqlite3.connect(self.name)
|
||||
self._setup_database()
|
||||
|
||||
def _setup_database(self):
|
||||
with self.conn:
|
||||
cur = self.conn.executescript('''
|
||||
BEGIN;
|
||||
CREATE TABLE IF NOT EXISTS guilds(
|
||||
id INTEGER PRIMARY KEY,
|
||||
name TEXT);
|
||||
CREATE TABLE IF NOT EXISTS watched_channels(
|
||||
id INTEGER PRIMARY KEY,
|
||||
name TEXT);
|
||||
COMMIT;
|
||||
''')
|
||||
script = '''BEGIN;
|
||||
CREATE TABLE IF NOT EXISTS guilds(
|
||||
guild_id INTEGER PRIMARY KEY,
|
||||
name TEXT,
|
||||
join_date TEXT);
|
||||
CREATE TABLE IF NOT EXISTS channels(
|
||||
channel_id INTEGER PRIMARY KEY,
|
||||
name TEXT,
|
||||
register_date TEXT,
|
||||
guild_id INTEGER,
|
||||
FOREIGN KEY (guild_id) REFERENCES guilds(guild_id));
|
||||
CREATE TABLE IF NOT EXISTS users(
|
||||
user_id INTEGER PRIMARY KEY,
|
||||
name TEXT);
|
||||
CREATE TABLE IF NOT EXISTS messages(
|
||||
message_id INTEGER PRIMARY KEY,
|
||||
contents TEXT,
|
||||
published_date TEXT,
|
||||
user_id INTEGER,
|
||||
channel_id INTEGER,
|
||||
guild_id INTEGER,
|
||||
FOREIGN KEY (user_id) REFERENCES users(user_id),
|
||||
FOREIGN KEY (channel_id) REFERENCES channels(channel_id),
|
||||
FOREIGN KEY (guild_id) REFERENCES guilds(guild_id));
|
||||
COMMIT;'''
|
||||
|
||||
def create_guild(self, guild_id: int, guild_name: str):
|
||||
with self.conn:
|
||||
self.conn.execute('''INSERT INTO guilds
|
||||
VALUES(?, ?);''',
|
||||
(guild_id, guild_name))
|
||||
self.conn.executescript(script)
|
||||
|
||||
def get_all_guilds(self):
|
||||
def create_guild(self, guild: WatchGuild):
|
||||
'''Insert a new guild into the database'''
|
||||
query = '''INSERT INTO guilds VALUES(?, ?, datetime('now'));'''
|
||||
with self.conn:
|
||||
cur = self.conn.execute('''SELECT id, name FROM guilds;''')
|
||||
self.conn.execute(query, (guild.id, guild.name))
|
||||
|
||||
def get_guild(self, id: int = None, name: str = None) -> list[WatchGuild]:
|
||||
'''Access guilds stored in the Database.
|
||||
|
||||
Query will be filtered by the passed parameters,
|
||||
returning records whose field values match those passed in.
|
||||
'''
|
||||
query = '''SELECT guild_id, name, join_date FROM guilds;'''
|
||||
with self.conn:
|
||||
cur = self.conn.execute(query)
|
||||
return cur.fetchall()
|
||||
|
||||
def get_all_watched_channels(self):
|
||||
with self.conn:
|
||||
curs = self.conn.execute('''SELECT id, name
|
||||
FROM watched_channels;''')
|
||||
return curs.fetchall()
|
||||
|
||||
def get_guild_watched_channels(self, guild_id: int):
|
||||
with self.conn:
|
||||
curs = self.conn.execute('''SELECT id, name
|
||||
FROM watched_channels
|
||||
WHERE id=?;''',
|
||||
(guild_id,))
|
||||
return curs.fetchall()
|
||||
|
||||
def write_message(self, message: discord.Message):
|
||||
def create_channel(self, channel: WatchChannel):
|
||||
pass
|
||||
|
||||
def get_messages(self, channel_id: int):
|
||||
def get_channel(self, **kwargs):
|
||||
query = '''SELECT channel_id, name,
|
||||
register_date, guild_id FROM channels;'''
|
||||
with self.conn:
|
||||
curs = self.conn.execute(query)
|
||||
return curs.fetchall()
|
||||
|
||||
def create_user(self, user: WatchUser):
|
||||
pass
|
||||
|
||||
def get_user(self, **kwargs):
|
||||
pass
|
||||
|
||||
def create_message(self, message: WatchMessage):
|
||||
pass
|
||||
|
||||
def get_message(self, **kwargs):
|
||||
pass
|
||||
|
|
|
@ -0,0 +1,62 @@
|
|||
from dataclasses import dataclass
|
||||
from datetime import datetime
|
||||
|
||||
|
||||
@dataclass
|
||||
class WatchGuild:
|
||||
'''WatchGuild represents a Discord guild as stored in the database.
|
||||
|
||||
Attributes:
|
||||
id: Guild ID as given by Discord
|
||||
name: Name of the guild at the time of adding the bot
|
||||
'''
|
||||
id: int
|
||||
name: str
|
||||
join_date: datetime
|
||||
|
||||
|
||||
@dataclass
|
||||
class WatchChannel:
|
||||
'''WatchChannel represents a Discord channel being watched for new messages
|
||||
|
||||
Attributes:
|
||||
id: Channel ID as given by Discord
|
||||
name: Name of the channel at the time of registration
|
||||
register_date: Datetime object when the channel was registered
|
||||
guild: WatchGuild object of which the channel belongs to
|
||||
'''
|
||||
id: int
|
||||
name: str
|
||||
register_date: datetime
|
||||
guild: WatchGuild
|
||||
|
||||
|
||||
@dataclass
|
||||
class WatchUser:
|
||||
'''WatchUser represents a Discord user who has sent a message in a watched channel.
|
||||
Attributes:
|
||||
id: User ID as given by Discord
|
||||
name: Nickname in the server at the time of latest message recorded
|
||||
'''
|
||||
id: int
|
||||
name: str
|
||||
|
||||
|
||||
@dataclass
|
||||
class WatchMessage:
|
||||
'''WatchMessage represents a Discord message sent in a watched channel.
|
||||
|
||||
Attributes:
|
||||
id: Message ID as given by Discord.
|
||||
contents: Raw contents of the message.
|
||||
published_date: Datetime object representing when the message was sent.
|
||||
author: WatchUser who sent the message.
|
||||
channel: WatchChannel in which the message was sent.
|
||||
guild: WatchGuild in which the message was sent.
|
||||
'''
|
||||
id: int
|
||||
contents: str
|
||||
published_date: datetime
|
||||
author: WatchUser
|
||||
channel: WatchChannel
|
||||
guild: WatchGuild
|
Loading…
Reference in New Issue