Skip to content

Commit 1764aaf

Browse files
committed
feat!: piccolo
1 parent 9a4e7b1 commit 1764aaf

File tree

7 files changed

+51
-94
lines changed

7 files changed

+51
-94
lines changed

botbase/db/__init__.py

-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,2 @@
11
from .blacklist import *
22
from .commands import *
3-
from .metadata import *

botbase/db/blacklist.py

+8-16
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,15 @@
1-
from ormar import BigInteger, Model, String
2-
3-
from .metadata import BaseMeta
1+
from piccolo.table import Table
2+
from piccolo.columns import BigInt, Text
43

54

65
__all__ = ("BlacklistGuild", "BlacklistUser")
76

87

9-
class BlacklistGuild(Model):
10-
class Meta(BaseMeta):
11-
tablename = "blacklist_guilds"
12-
13-
# pyright: reportGeneralTypeIssues=false
14-
id: int = BigInteger(primary_key=True, autoincrement=False)
15-
reason: str = String(max_length=255, default="Unknown reason.")
16-
8+
class BlacklistGuild(Table):
9+
id = BigInt(primary_key=True)
10+
reason = Text(default="Unknown reason.")
1711

18-
class BlacklistUser(Model):
19-
class Meta(BaseMeta):
20-
tablename = "blacklist_users"
2112

22-
id: int = BigInteger(primary_key=True, autoincrement=False)
23-
reason: str = String(max_length=255, default="Unknown reason.")
13+
class BlacklistUser(Table):
14+
id = BigInt(primary_key=True)
15+
reason = Text(default="Unknown reason.")

botbase/db/commands.py

+11-19
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,18 @@
1-
from ormar import BigInteger, Integer, Model, String
2-
from sqlalchemy import PrimaryKeyConstraint
1+
from logging import getLogger
32

4-
from .metadata import BaseMeta
3+
from piccolo.table import Table
4+
from piccolo.columns import BigInt, Text, Integer
55

6+
log = getLogger(__name__)
67
__all__ = ("CommandLog",)
78

89

9-
class CommandLog(Model):
10-
class Meta(BaseMeta):
11-
tablename = "commands"
12-
constraints = [PrimaryKeyConstraint("command", "guild", "channel", "member")]
10+
class CommandLog(Table):
11+
command = Text(primary_key=True)
12+
guild = BigInt()
13+
channel = BigInt()
14+
member = BigInt()
15+
amount = Integer(default=1)
1316

14-
# pyright: reportGeneralTypeIssues=false
15-
command: str = String(max_length=255, primary_key=True)
16-
guild: int = BigInteger()
17-
channel: int = BigInteger()
18-
member: int = BigInteger()
19-
amount: int = BigInteger(default=1)
2017

21-
22-
# ormar :(
23-
CommandLog.command.primary_key = True
24-
CommandLog.guild.primary_key = True
25-
CommandLog.channel.primary_key = True
26-
CommandLog.member.primary_key = True
18+
log.critical("Make sure to set command, guild, channel, member UNIQUE.")

botbase/db/metadata.py

-23
This file was deleted.

botbase/exts/blacklist.py

+16-12
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
from __future__ import annotations
22

33
from logging import getLogger
4-
from typing import TYPE_CHECKING
54

65
from nextcord import Embed, Guild, slash_command
76
from nextcord.ext.application_checks import is_owner
@@ -11,10 +10,6 @@
1110
from ..models import CogBase
1211
from ..wraps import MyInter
1312

14-
if TYPE_CHECKING:
15-
from ..botbase import BotBase
16-
17-
1813
_log = getLogger(__name__)
1914

2015

@@ -35,24 +30,32 @@ async def add(
3530

3631
if guild:
3732
self.guilds.add(obj)
38-
await BlacklistGuild.objects.create(id=obj, reason=reason)
33+
await BlacklistGuild(
34+
{BlacklistGuild.id: obj, BlacklistGuild.reason: reason}
35+
).save()
3936
else:
4037
self.users.add(obj)
41-
await BlacklistUser.objects.create(id=obj, reason=reason)
38+
await BlacklistUser(
39+
{BlacklistUser.id: obj, BlacklistUser.reason: reason}
40+
).save()
4241

4342
async def remove(self, obj: int, guild: bool = False) -> None:
4443
assert isinstance(obj, int)
4544

4645
if guild:
4746
self.guilds.discard(obj)
48-
await BlacklistGuild.objects.delete(id=obj)
47+
await BlacklistGuild.delete().where(BlacklistGuild.id == obj)
4948
else:
5049
self.users.discard(obj)
51-
await BlacklistUser.objects.delete(id=obj)
50+
await BlacklistUser.delete().where(BlacklistGuild.id == obj)
5251

5352
async def load(self) -> None:
54-
self.guilds = set(await BlacklistGuild.objects.values_list("id", flatten=True))
55-
self.users = set(await BlacklistUser.objects.values_list("id", flatten=True))
53+
self.guilds = set(
54+
item["id"] for item in await BlacklistGuild.select(BlacklistGuild.id)
55+
)
56+
self.users = set(
57+
item["id"] for item in await BlacklistUser.select(BlacklistUser.id)
58+
)
5659

5760

5861
class BlacklistCog(CogBase[BotBase]):
@@ -62,7 +65,8 @@ def __init__(self, bot: BotBase) -> None:
6265
self.blacklist = Blacklist()
6366
bot.loop.create_task(self.load_blacklist())
6467
self.old_process_application_commands = bot.process_application_commands
65-
bot.process_application_commands = self.check # type: ignore # MyInter + Interaction
68+
# MyInter + Interaction
69+
bot.process_application_commands = self.check # type: ignore
6670

6771
if bot.guild_ids:
6872
self.blacklist_.guild_ids_to_rollout.update(bot.guild_ids)

botbase/exts/log_commands.py

+10-20
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,11 @@
11
from __future__ import annotations
22

3-
from typing import TYPE_CHECKING
4-
5-
from ormar import NoMatch
6-
73
from ..botbase import BotBase
8-
from ..db import CommandLog, database
4+
from ..db import CommandLog
95
from ..models import CogBase
106
from ..wraps import MyInter
117

128

13-
if TYPE_CHECKING:
14-
from ..botbase import BotBase
15-
16-
179
class CommandLogging(CogBase[BotBase]):
1810
@CogBase.listener()
1911
async def on_application_command_completion(self, inter: MyInter):
@@ -26,20 +18,18 @@ async def on_application_command_completion(self, inter: MyInter):
2618
channel = inter.channel.id if inter.guild is not None else None
2719
member = inter.user.id
2820

29-
# Composite PK not in ormar ruins the whole SQL statement.
30-
await database.execute(
21+
# Incrementing amount without funky sub queries is not possible.
22+
await CommandLog.raw(
3123
"""
32-
INSERT INTO commands (command, guild, channel, member, amount)
33-
VALUES (:command, :guild, :channel, :member, 1)
24+
INSERT INTO command_log (command, guild, channel, member, amount)
25+
VALUES ({}, {}, {}, {}, 1)
3426
ON CONFLICT (command, guild, channel, member)
35-
DO UPDATE SET amount = commands.amount + 1
27+
DO UPDATE SET amount = command_log.amount + 1;
3628
""",
37-
{
38-
"command": cmd,
39-
"guild": guild,
40-
"channel": channel,
41-
"member": member,
42-
},
29+
cmd,
30+
guild,
31+
channel,
32+
member,
4333
)
4434

4535

pyproject.toml

+6-3
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,18 @@ readme = "README.md"
1010

1111
[tool.poetry.dependencies]
1212
python = "^3.8"
13-
asyncpg = { version = ">=0.25,<0.28", optional = true }
1413
nextcord = "^2.0.0"
1514
nextcord-ext-menus = "^1.5.2"
1615
psutil = "^5.9.0"
17-
ormar = { version = ">=0.11.0,<0.13.0", optional = true, extras = ["postgres"] }
1816
python-dotenv = ">=0.16.0,<1.1.0"
17+
piccolo = { version = "^0.111.1", optional = true, extras = [
18+
"orjson",
19+
"uvloop",
20+
"postgres",
21+
] }
1922

2023
[tool.poetry.extras]
21-
db = ["asyncpg", "ormar"]
24+
db = ["piccolo"]
2225

2326
[tool.poetry.group.dev.dependencies]
2427
black = ">=22.3,<24.0"

0 commit comments

Comments
 (0)