3
3
import asyncio
4
4
import logging
5
5
import re
6
+ import sqlite3
6
7
import time
7
8
8
9
from collections .abc import Iterator
9
10
from pathlib import Path
10
11
from typing import Any , Optional
11
12
12
13
import aiohttp
13
- import asyncpg
14
14
import discord
15
15
import sentry_sdk
16
16
@@ -45,21 +45,26 @@ def __init__(self, bot: PINK, *, prefix: str):
45
45
self .prefix_re = mention_or_prefix_regex (bot .user .id , self .prefix ) # type: ignore
46
46
47
47
@classmethod
48
- def from_pg (cls , bot : PINK , data : asyncpg . Record ) -> Prefix :
48
+ def from_db (cls , bot : PINK , data : sqlite3 . Row ) -> Prefix :
49
49
return cls (bot , prefix = data ["prefix" ])
50
50
51
- async def write (self , ctx : Context ) -> None :
52
- await ctx .pg . fetchrow (
53
- "INSERT INTO prefixes (guild_id, prefix) VALUES ($1, $2 ) "
51
+ def write (self , ctx : Context ) -> None :
52
+ ctx .db . execute (
53
+ "INSERT INTO prefixes (guild_id, prefix) VALUES (?, ? ) "
54
54
"ON CONFLICT (guild_id) DO UPDATE "
55
55
"SET prefix = EXCLUDED.prefix" ,
56
- ctx .guild .id , # type: ignore
57
- self .prefix ,
56
+ (
57
+ ctx .guild .id , # type: ignore
58
+ self .prefix ,
59
+ ),
58
60
)
59
61
60
62
@staticmethod
61
- async def delete (ctx : Context ) -> None :
62
- await ctx .pg .fetchrow ("DELETE FROM prefixes WHERE guild_id = $1" , ctx .guild .id ) # type: ignore
63
+ def delete (ctx : Context ) -> None :
64
+ ctx .db .execute (
65
+ "DELETE FROM prefixes WHERE guild_id = ?" ,
66
+ (ctx .guild .id ,), # type: ignore
67
+ )
63
68
64
69
def __repr__ (self ) -> str :
65
70
return f"<{ type (self ).__name__ } prefix={ self .prefix } >"
@@ -70,38 +75,56 @@ def __init__(
70
75
self ,
71
76
* ,
72
77
session : aiohttp .ClientSession ,
73
- pg : asyncpg .Pool [asyncpg .Record ],
74
78
redis : Redis [bytes ],
75
79
version : Version ,
76
80
** kwargs : Any ,
77
81
) -> None :
78
82
super ().__init__ (** kwargs )
79
83
80
84
self .session = session
81
- self .pg = pg
82
85
self .redis = redis
83
86
self .version = version
84
87
85
88
self .prefixes : dict [int , Prefix ] = {}
86
89
self .owner_ids : set [int ] = set ()
87
90
91
+ def init_db (self ) -> None :
92
+ with Path ("schema.sql" ).open () as f :
93
+ db = self .db_cursor ()
94
+ db .executescript (f .read ())
95
+
96
+ def db_cursor (self ) -> sqlite3 .Cursor :
97
+ conn = sqlite3 .connect (settings .db .path )
98
+ conn .isolation_level = None
99
+ conn .execute ("PRAGMA journal_mode=wal" )
100
+ conn .execute ("PRAGMA foreign_keys=ON" )
101
+ conn .row_factory = sqlite3 .Row
102
+
103
+ return conn .cursor ()
104
+
88
105
# --- overloads ---
89
106
async def setup_hook (self ) -> None :
90
107
self .launched_at = time .monotonic ()
91
108
92
- await self ._load_prefixes ()
109
+ self .init_db ()
93
110
94
- await self . _load_cogs ()
95
-
96
- asyncio . gather (
111
+ await asyncio . gather (
112
+ self . _load_prefixes (),
113
+ self . _load_cogs (),
97
114
self ._fetch_owners (),
98
115
)
99
116
100
117
async def _load_prefixes (self ) -> None :
101
- self ._default_prefix_re = mention_or_prefix_regex (self .user .id , settings .bot .prefix ) # type: ignore
118
+ self ._default_prefix_re = mention_or_prefix_regex (
119
+ self .user .id , # type: ignore
120
+ settings .bot .prefix ,
121
+ )
122
+
123
+ db = self .db_cursor ()
124
+ db .execute ("SELECT guild_id, prefix FROM prefixes" )
102
125
103
- for guild in await self . pg . fetch ( "SELECT guild_id, prefix FROM prefixes" ):
104
- self .prefixes [guild ["guild_id" ]] = Prefix .from_pg (self , guild )
126
+ for guild in db . fetchall ( ):
127
+ self .prefixes [guild ["guild_id" ]] = Prefix .from_db (self , guild )
105
128
106
129
async def _fetch_owners (self ) -> None :
107
130
owners = settings .owners .ids .copy ()
0 commit comments