Skip to content

Commit

Permalink
fix: greatly reduce resource consumption
Browse files Browse the repository at this point in the history
  • Loading branch information
huenique committed Nov 8, 2021
1 parent 2e27fd2 commit 3f2d0f6
Show file tree
Hide file tree
Showing 17 changed files with 439 additions and 271 deletions.
3 changes: 2 additions & 1 deletion Procfile
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
worker: python -OO dayong
worker: python -OO dayong
clock: python -m dayong.tasks.aptasks
48 changes: 27 additions & 21 deletions dayong/abc.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,54 +3,60 @@
dayong.abc
~~~~~~~~~~
Object interfaces used within Dayong.
Interfaces used within Dayong.
"""

from abc import ABC, abstractmethod
from typing import Any

import tanjun
from sqlmodel.engine.result import ScalarResult

from dayong.core.configs import DayongConfig

class DBProto(ABC):
"""Protocol for a generic database interface."""

class Database(ABC):
"""Abstract base class of a database interface."""

@abstractmethod
async def create_table(self) -> None:
"""Create physical message tables for all the message table models stored in
`Any.metadata`.
async def connect(
self, config: DayongConfig = tanjun.injected(type=DayongConfig)
) -> None:
"""Create a database connection.
Args:
config (DayongConfig, optional): [description]. Defaults to
tanjun.injected(type=DayongConfig).
"""
raise NotImplementedError

@abstractmethod
async def add_row(self, table_model_object: Any) -> None:
async def create_table(self) -> None:
"""Create physical tables for all the table models stored in `Any.metadata`."""

@abstractmethod
async def add_row(self, table_model: Any) -> None:
"""Add a row to the message table.
Args:
table_model_object (Any): An instance of `dayong.models.Any` or one
of its subclasses.
table_model (Any): A subclass of SQLModel
"""
raise NotImplementedError

@abstractmethod
async def remove_row(self, table_model_object: Any) -> None:
async def remove_row(self, table_model: Any) -> None:
"""Remove a row from the message table.
Args:
table_model_object (Any): An instance of `dayong.models.Any` or one
of its subclasses.
table_model (Any): A subclass of SQLModel
"""
raise NotImplementedError

@abstractmethod
async def get_row(self, table_model_object: Any) -> ScalarResult[Any]:
"""Get data from the message table.
async def get_row(self, table_model: Any) -> ScalarResult[Any]:
"""Get row from the message table.
Args:
table_model_object (Any): Instance of a message table model.
table_model (Any): A subclass of SQLModel.
Returns:
ScalarResult: An `ScalarResult` object which contains a scalar value or
sequence of scalar values.
ScalarResult: A `ScalarResult` which contains a scalar value or sequence of
scalar values.
"""
raise NotImplementedError
12 changes: 6 additions & 6 deletions dayong/bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@
import hikari
import tanjun

from dayong.abc import DBProto
from dayong.configs import DayongConfig, DayongDynamicLoader
from dayong.operations import MessageDBImpl
from dayong.settings import BASE_DIR
from dayong.abc import Database
from dayong.core.configs import DayongConfig, DayongDynamicLoader
from dayong.core.settings import BASE_DIR
from dayong.operations import MessageDB


def run() -> None:
Expand All @@ -24,15 +24,15 @@ def run() -> None:
banner="dayong",
intents=hikari.Intents.ALL,
)
database = MessageDBImpl()
database = MessageDB()
(
tanjun.Client.from_gateway_bot(
bot, declare_global_commands=hikari.Snowflake(loaded_config.guild_id)
)
.load_modules(*Path(os.path.join(BASE_DIR, "components")).glob("*.py"))
.add_prefix(loaded_config.bot_prefix)
.set_type_dependency(DayongConfig, loaded_config)
.set_type_dependency(DBProto, database)
.set_type_dependency(Database, database)
.add_client_callback(tanjun.ClientCallbackNames.STARTING, database.connect)
)
bot.run()
4 changes: 2 additions & 2 deletions dayong/components/event_component.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import hikari
import tanjun

from dayong.configs import DayongConfig
from dayong.core.configs import DayongConfig

component = tanjun.Component()

Expand Down Expand Up @@ -49,7 +49,7 @@ async def greet_new_member(
Args:
event (hikari.MemberCreateEvent): Instance of `hikari.MemberCreateEvent`. This
is a registered type dependency and is injected by the client.
config (DayongConfig, optional): An instance of `dayong.configs.DayongConfig`.
config (DayongConfig, optional): An instance of `dayong.core.configs.DayongConfig`.
This is registered type dependency and is injected by the client. Defaults
to tanjun.injected(type=DayongConfig).
"""
Expand Down
6 changes: 3 additions & 3 deletions dayong/components/privilege_component.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
from pydantic import ValidationError
from sqlalchemy.exc import SQLAlchemyError

from dayong.abc import DBProto
from dayong.abc import Database
from dayong.models import AnonMessage

component = tanjun.Component()
Expand Down Expand Up @@ -46,15 +46,15 @@ async def ping_command(ctx: tanjun.abc.Context) -> None:
async def get_user_info(
ctx: tanjun.abc.Context,
str_id: str,
database: DBProto = tanjun.injected(type=DBProto),
database: Database = tanjun.injected(type=Database),
) -> None:
"""Reveal information on a user."
Args:
ctx (tanjun.abc.Context): Instance of `tanjun.abc.Context`.
str_id (str): This can be a hash or the object or the ID of an
existing user.
database (DBProto): Interface for a database message table. This is a
database (Database): Interface for a database message table. This is a
registered type dependency and is injected by the client.
"""
await ctx.respond(content="Fetching user information, please wait...")
Expand Down
10 changes: 5 additions & 5 deletions dayong/components/slash_component.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
from pydantic import ValidationError
from sqlalchemy.exc import SQLAlchemyError

from dayong.abc import DBProto
from dayong.configs import DayongConfig
from dayong.abc import Database
from dayong.core.configs import DayongConfig
from dayong.models import AnonMessage

component = tanjun.Component()
Expand All @@ -38,17 +38,17 @@ async def randomize_id(str_id: str) -> str:
async def anon_command(
ctx: tanjun.abc.SlashContext,
message: str,
database: DBProto = tanjun.injected(type=DBProto),
database: Database = tanjun.injected(type=Database),
config: DayongConfig = tanjun.injected(type=DayongConfig),
) -> None:
"""Allow a user or server member to send anonymous messages on Discord.
Args:
ctx (tanjun.abc.SlashContext): Interface of a context.
message (str): The message to anonymize.
database (DBProto): Interface for a database message table. This is a
database (Database): Interface for a database message table. This is a
registered type dependency and is injected by the client.
config (DayongConfig): An instance of `dayong.configs.DayongConfig`. Also a
config (DayongConfig): An instance of `dayong.core.configs.DayongConfig`. Also a
registered type dependency and is injected by the client.
"""
await ctx.defer()
Expand Down
Loading

0 comments on commit 3f2d0f6

Please sign in to comment.