Skip to content

Commit 2278d52

Browse files
committed
✨ Add db system
1 parent ab7c545 commit 2278d52

File tree

12 files changed

+429
-3
lines changed

12 files changed

+429
-3
lines changed

pdm.lock

Lines changed: 174 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pyproject.toml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ dependencies = [
2121
"aiofile>=3.9.0",
2222
"sentry-sdk>=2.18.0",
2323
"aiocache[redis]>=0.12.3",
24+
"tortoise-orm[asyncpg]>=0.23.0",
25+
"aerich>=0.8.0",
2426
]
2527
requires-python = "==3.12.*"
2628
readme = "README.md"
@@ -78,6 +80,7 @@ docstring-code-line-length = "dynamic"
7880

7981
[tool.ruff.lint]
8082
select = ["ALL"]
83+
per-file-ignores = { "src/database/migrations/*"= ["INP001", "ARG001"] }
8184
extend-ignore = [
8285
"N999",
8386
"D104",
@@ -104,3 +107,8 @@ extend-ignore = [
104107
"PLR2004",
105108
"C901"
106109
]
110+
111+
[tool.aerich]
112+
tortoise_orm = "src.database.config.TORTOISE_ORM"
113+
location = "./src/database/migrations"
114+
src_folder = "./."

src/custom/__init__.py

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,23 @@
11
# Copyright (c) NiceBots.xyz
22
# SPDX-License-Identifier: MIT
33

4+
import contextlib
45
from logging import getLogger
56
from typing import TYPE_CHECKING, Any, override
67

78
import aiocache
89
import discord
9-
from discord import Message
10+
from discord import Interaction, Message, WebhookMessage
1011
from discord.ext import bridge
1112
from discord.ext.bridge import (
1213
BridgeExtContext,
1314
)
1415

1516
from src.i18n.classes import ExtensionTranslation, TranslationWrapper, apply_locale
1617

18+
if TYPE_CHECKING:
19+
from src.database.models import Guild, User
20+
1721
logger = getLogger("bot")
1822

1923

@@ -22,6 +26,8 @@ def __init__(self, bot: "Bot", interaction: discord.Interaction) -> None:
2226
self.translations: TranslationWrapper = TranslationWrapper({}, "en-US") # empty placeholder
2327
super().__init__(bot=bot, interaction=interaction)
2428
self.bot: Bot
29+
self.user_obj: User | None = None
30+
self.guild_obj: Guild | None = None
2531

2632
@override
2733
def __setattr__(self, key: Any, value: Any) -> None:
@@ -33,22 +39,42 @@ def __setattr__(self, key: Any, value: Any) -> None:
3339
super().__setattr__(key, value)
3440

3541

42+
async def remove_reaction(user: discord.User, message: discord.Message, emoji: str) -> None:
43+
await message.remove_reaction(emoji, user)
44+
45+
3646
class ExtContext(bridge.BridgeExtContext):
3747
def __init__(self, **kwargs: Any) -> None:
3848
self.translations: TranslationWrapper = TranslationWrapper({}, "en-US") # empty placeholder
3949
super().__init__(**kwargs)
4050
self.bot: Bot
51+
self.user_obj: User | None = None
52+
self.guild_obj: Guild | None = None
4153

4254
def load_translations(self) -> None:
4355
if hasattr(self.command, "translations") and self.command.translations: # pyright: ignore[reportUnknownArgumentType,reportOptionalMemberAccess,reportAttributeAccessIssue]
4456
locale: str | None = None
4557
if guild := self.guild:
4658
locale = guild.preferred_locale
4759
self.translations = apply_locale(
48-
self.command.translations,
60+
self.command.translations, # pyright: ignore [reportAttributeAccessIssue, reportOptionalMemberAccess, reportUnknownArgumentType]
4961
locale,
5062
)
5163

64+
@override
65+
async def defer(self, *args: Any, **kwargs: Any) -> None:
66+
await super().defer(*args, **kwargs)
67+
with contextlib.suppress(Exception):
68+
await self.message.add_reaction("🔄")
69+
70+
@override
71+
async def respond(self, *args: Any, **kwargs: Any) -> "Interaction | WebhookMessage | Message":
72+
r = await super().respond(*args, **kwargs)
73+
with contextlib.suppress(Exception):
74+
if self.me:
75+
await remove_reaction(self.me, self.message, "🔄")
76+
return r
77+
5278

5379
class Bot(bridge.Bot):
5480
def __init__(

src/database/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# Copyright (c) NiceBots

src/database/config/__init__.py

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
# Copyright (c) NiceBots
2+
# SPDX-License-Identifier: MIT
3+
4+
from logging import getLogger
5+
6+
import aerich
7+
from tortoise import Tortoise
8+
9+
from src.config import config
10+
11+
logger = getLogger("bot").getChild("database")
12+
13+
TORTOISE_ORM = {
14+
"connections": {"default": config["db"]["url"]},
15+
"apps": {
16+
"models": {
17+
"models": ["src.database.models", "aerich.models"],
18+
"default_connection": "default",
19+
}
20+
},
21+
}
22+
23+
24+
async def init() -> None:
25+
command = aerich.Command(
26+
TORTOISE_ORM,
27+
app="models",
28+
location="./src/database/migrations/",
29+
)
30+
await command.init()
31+
migrated = await command.upgrade(run_in_transaction=True) # pyright: ignore[reportUnknownVariableType]
32+
logger.success(f"Successfully migrated {migrated} migrations") # pyright: ignore [reportAttributeAccessIssue]
33+
await Tortoise.init(config=TORTOISE_ORM)
34+
35+
36+
async def shutdown() -> None:
37+
await Tortoise.close_connections()
38+
39+
40+
__all__ = ["init", "shutdown"]

0 commit comments

Comments
 (0)