mirror of
https://github.com/Xevion/contest-assistant.git
synced 2025-12-09 00:06:41 -06:00
setup basic logging constants/handlers, write all table creation/update/basic fetching behavior sqlite statements
This commit is contained in:
@@ -1,11 +1,21 @@
|
|||||||
|
import logging
|
||||||
|
|
||||||
import discord as discord
|
import discord as discord
|
||||||
|
|
||||||
|
from contest import constants
|
||||||
|
from contest.db import ContestDatabase
|
||||||
|
|
||||||
|
logger = logging.getLogger(__file__)
|
||||||
|
logger.setLevel(constants.LOGGING_LEVEL)
|
||||||
|
|
||||||
|
|
||||||
class ContestClient(discord.Client):
|
class ContestClient(discord.Client):
|
||||||
def __init__(self, **options) -> None:
|
def __init__(self, **options) -> None:
|
||||||
super().__init__(**options)
|
super().__init__(**options)
|
||||||
|
self.db = ContestDatabase.create()
|
||||||
|
|
||||||
async def on_message(self) -> None:
|
async def on_message(self, message: discord.Message) -> None:
|
||||||
|
prefix = message.guild
|
||||||
pass
|
pass
|
||||||
|
|
||||||
async def on_raw_reaction_add(self, payload) -> None:
|
async def on_raw_reaction_add(self, payload) -> None:
|
||||||
|
|||||||
@@ -1 +1,4 @@
|
|||||||
|
import logging
|
||||||
|
|
||||||
DATABASE = 'database.db'
|
DATABASE = 'database.db'
|
||||||
|
LOGGING_LEVEL = logging.DEBUG
|
||||||
|
|||||||
@@ -1,16 +1,22 @@
|
|||||||
|
import logging
|
||||||
import sqlite3
|
import sqlite3
|
||||||
|
|
||||||
import aiosqlite
|
import aiosqlite
|
||||||
|
|
||||||
from contest import constants
|
from contest import constants
|
||||||
|
|
||||||
|
logger = logging.getLogger(__file__)
|
||||||
|
logger.setLevel(constants.LOGGING_LEVEL)
|
||||||
|
|
||||||
|
|
||||||
class ContestDatabase(object):
|
class ContestDatabase(object):
|
||||||
"""
|
"""
|
||||||
A handler class for a SQLite3 database used by the bot with Async support.
|
A handler class for a SQLite3 database used by the bot with Async support.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
def __init__(self, conn: aiosqlite.Connection) -> None:
|
def __init__(self, conn: aiosqlite.Connection) -> None:
|
||||||
self.db = conn
|
self.conn = conn
|
||||||
|
self.setup()
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
async def create(cls) -> 'ContestDatabase':
|
async def create(cls) -> 'ContestDatabase':
|
||||||
@@ -23,5 +29,37 @@ class ContestDatabase(object):
|
|||||||
await conn.commit()
|
await conn.commit()
|
||||||
return db
|
return db
|
||||||
|
|
||||||
async def is_setup(self):
|
async def setup(self) -> None:
|
||||||
pass
|
"""Sets up the tables for initial database creation"""
|
||||||
|
await self.conn.execute('''CREATE TABLE IF NOT EXISTS guild
|
||||||
|
(id INTEGER PRIMARY KEY,
|
||||||
|
prefix TEXT DEFAULT '$',
|
||||||
|
submission INTEGER NULLABLE)''')
|
||||||
|
|
||||||
|
async def setup_guild(self, guild_id: int) -> None:
|
||||||
|
"""Sets up a guild in the database."""
|
||||||
|
await self.conn.execute('''INSERT INTO guild (id) VALUES (?)''', [guild_id])
|
||||||
|
|
||||||
|
async def set_prefix(self, guild_id: int, new_prefix: str) -> None:
|
||||||
|
"""Updates the prefix for a specific guild in the database"""
|
||||||
|
await self.conn.execute('''UPDATE guild SET prefix = ? WHERE id = ?''', [new_prefix, guild_id])
|
||||||
|
|
||||||
|
async def is_setup(self, guild_id: int) -> bool:
|
||||||
|
"""Checks whether the bot is setup to complete submission channel related commands."""
|
||||||
|
cur = await self.conn.cursor()
|
||||||
|
try:
|
||||||
|
await cur.execute('''SELECT submission FROM guild WHERE id = ?''', [guild_id])
|
||||||
|
t = await cur.fetchone()
|
||||||
|
print(t)
|
||||||
|
return t['submission']
|
||||||
|
finally:
|
||||||
|
await cur.close()
|
||||||
|
|
||||||
|
async def get_prefix(self, guild_id: int) -> str:
|
||||||
|
"""Gets the prefix from a specific guild in the database."""
|
||||||
|
cur = await self.conn.cursor()
|
||||||
|
try:
|
||||||
|
await cur.execute('''SELECT prefix FROM guild WHERE id = ?''', [guild_id])
|
||||||
|
return (await cur.fetchone())['prefix']
|
||||||
|
finally:
|
||||||
|
await cur.close()
|
||||||
|
|||||||
Reference in New Issue
Block a user