forked from LAION-AI/Open-Assistant
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathschemas.py
27 lines (20 loc) · 824 Bytes
/
schemas.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
"""Database schemas."""
import typing as t
from aiosqlite import Connection, Row
from pydantic import BaseModel
class GuildSettings(BaseModel):
"""Guild settings."""
guild_id: int
log_channel_id: int | None
@classmethod
def parse_obj(cls, obj: Row) -> "GuildSettings":
"""Deserialize a Row object from aiosqlite into a GuildSettings object."""
return cls(guild_id=obj[0], log_channel_id=obj[1])
@classmethod
async def from_db(cls, conn: Connection, guild_id: int) -> t.Optional["GuildSettings"]:
async with conn.cursor() as cursor:
await cursor.execute("SELECT * FROM guild_settings WHERE guild_id = ?", (guild_id,))
row = await cursor.fetchone()
if row is None:
return None
return cls.parse_obj(row)