-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtracker_db.py
More file actions
64 lines (56 loc) · 2.44 KB
/
tracker_db.py
File metadata and controls
64 lines (56 loc) · 2.44 KB
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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
from typing import Any
import aiosqlite
class TrackerDatabase:
def __init__(self, path: str) -> None:
self.path = path
async def init(self) -> None:
async with aiosqlite.connect(self.path) as db:
await db.execute(
"""
CREATE TABLE IF NOT EXISTS tracked_messages (
guild_id TEXT PRIMARY KEY,
channel_id TEXT NOT NULL,
message_id TEXT NOT NULL,
created_at TEXT NOT NULL DEFAULT CURRENT_TIMESTAMP,
updated_at TEXT NOT NULL DEFAULT CURRENT_TIMESTAMP
)
"""
)
await db.commit()
async def get_tracked_message(self, guild_id: str) -> dict[str, Any] | None:
async with aiosqlite.connect(self.path) as db:
db.row_factory = aiosqlite.Row
async with db.execute(
"SELECT guild_id, channel_id, message_id FROM tracked_messages WHERE guild_id = ?",
(guild_id,),
) as cursor:
row = await cursor.fetchone()
if row is None:
return None
return dict(row)
async def list_tracked_messages(self) -> list[dict[str, Any]]:
async with aiosqlite.connect(self.path) as db:
db.row_factory = aiosqlite.Row
async with db.execute(
"SELECT guild_id, channel_id, message_id FROM tracked_messages ORDER BY guild_id"
) as cursor:
rows = await cursor.fetchall()
return [dict(row) for row in rows]
async def upsert_tracked_message(self, guild_id: str, channel_id: str, message_id: str) -> None:
async with aiosqlite.connect(self.path) as db:
await db.execute(
"""
INSERT INTO tracked_messages (guild_id, channel_id, message_id)
VALUES (?, ?, ?)
ON CONFLICT(guild_id) DO UPDATE SET
channel_id = excluded.channel_id,
message_id = excluded.message_id,
updated_at = CURRENT_TIMESTAMP
""",
(guild_id, channel_id, message_id),
)
await db.commit()
async def delete_tracked_message(self, guild_id: str) -> None:
async with aiosqlite.connect(self.path) as db:
await db.execute("DELETE FROM tracked_messages WHERE guild_id = ?", (guild_id,))
await db.commit()