diff --git a/.gitignore b/.gitignore index 43410472..2acd96dd 100644 --- a/.gitignore +++ b/.gitignore @@ -9,3 +9,10 @@ dist/ # Sphinx /docs/build/ + +# Royalpack +royalpack.cfg.toml + +# Telethon +bot.session +bot.session-journal diff --git a/poetry.lock b/poetry.lock index 696b5301..313d5310 100644 --- a/poetry.lock +++ b/poetry.lock @@ -134,6 +134,14 @@ python-versions = ">=2.6, !=3.0.*, !=3.1.*, !=3.2.*, !=3.3.*" [package.extras] test = ["ipaddress", "mock", "unittest2", "enum34", "pywin32", "wmi"] +[[package]] +name = "psycopg2" +version = "2.8.6" +description = "psycopg2 - Python-PostgreSQL Database Adapter" +category = "main" +optional = false +python-versions = ">=2.7,!=3.0.*,!=3.1.*,!=3.2.*,!=3.3.*" + [[package]] name = "pyaes" version = "1.6.1" @@ -301,7 +309,7 @@ multidict = ">=4.0" [metadata] lock-version = "1.1" python-versions = "^3.8" -content-hash = "18147d331322411007ead0880f9c523d0e2c9a66c194bbee8c633b4eab481e7c" +content-hash = "fca1f5bf509aeae1c6d05da579732826099606dba2b73210f39fef215a8859cb" [metadata.files] aiohttp = [ @@ -493,6 +501,23 @@ psutil = [ {file = "psutil-5.8.0-cp39-cp39-win_amd64.whl", hash = "sha256:f4634b033faf0d968bb9220dd1c793b897ab7f1189956e1aa9eae752527127d3"}, {file = "psutil-5.8.0.tar.gz", hash = "sha256:0c9ccb99ab76025f2f0bbecf341d4656e9c1351db8cc8a03ccd62e318ab4b5c6"}, ] +psycopg2 = [ + {file = "psycopg2-2.8.6-cp27-cp27m-win32.whl", hash = "sha256:068115e13c70dc5982dfc00c5d70437fe37c014c808acce119b5448361c03725"}, + {file = "psycopg2-2.8.6-cp27-cp27m-win_amd64.whl", hash = "sha256:d160744652e81c80627a909a0e808f3c6653a40af435744de037e3172cf277f5"}, + {file = "psycopg2-2.8.6-cp34-cp34m-win32.whl", hash = "sha256:b8cae8b2f022efa1f011cc753adb9cbadfa5a184431d09b273fb49b4167561ad"}, + {file = "psycopg2-2.8.6-cp34-cp34m-win_amd64.whl", hash = "sha256:f22ea9b67aea4f4a1718300908a2fb62b3e4276cf00bd829a97ab5894af42ea3"}, + {file = "psycopg2-2.8.6-cp35-cp35m-win32.whl", hash = "sha256:26e7fd115a6db75267b325de0fba089b911a4a12ebd3d0b5e7acb7028bc46821"}, + {file = "psycopg2-2.8.6-cp35-cp35m-win_amd64.whl", hash = "sha256:00195b5f6832dbf2876b8bf77f12bdce648224c89c880719c745b90515233301"}, + {file = "psycopg2-2.8.6-cp36-cp36m-win32.whl", hash = "sha256:a49833abfdede8985ba3f3ec641f771cca215479f41523e99dace96d5b8cce2a"}, + {file = "psycopg2-2.8.6-cp36-cp36m-win_amd64.whl", hash = "sha256:f974c96fca34ae9e4f49839ba6b78addf0346777b46c4da27a7bf54f48d3057d"}, + {file = "psycopg2-2.8.6-cp37-cp37m-win32.whl", hash = "sha256:6a3d9efb6f36f1fe6aa8dbb5af55e067db802502c55a9defa47c5a1dad41df84"}, + {file = "psycopg2-2.8.6-cp37-cp37m-win_amd64.whl", hash = "sha256:56fee7f818d032f802b8eed81ef0c1232b8b42390df189cab9cfa87573fe52c5"}, + {file = "psycopg2-2.8.6-cp38-cp38-win32.whl", hash = "sha256:ad2fe8a37be669082e61fb001c185ffb58867fdbb3e7a6b0b0d2ffe232353a3e"}, + {file = "psycopg2-2.8.6-cp38-cp38-win_amd64.whl", hash = "sha256:56007a226b8e95aa980ada7abdea6b40b75ce62a433bd27cec7a8178d57f4051"}, + {file = "psycopg2-2.8.6-cp39-cp39-win32.whl", hash = "sha256:2c93d4d16933fea5bbacbe1aaf8fa8c1348740b2e50b3735d1b0bf8154cbf0f3"}, + {file = "psycopg2-2.8.6-cp39-cp39-win_amd64.whl", hash = "sha256:d5062ae50b222da28253059880a871dc87e099c25cb68acf613d9d227413d6f7"}, + {file = "psycopg2-2.8.6.tar.gz", hash = "sha256:fb23f6c71107c37fd667cb4ea363ddeb936b348bbd6449278eb92c189699f543"}, +] pyaes = [ {file = "pyaes-1.6.1.tar.gz", hash = "sha256:02c1b1405c38d3c370b085fb952dd8bea3fadcee6411ad99f312cc129c536d8f"}, ] diff --git a/pyproject.toml b/pyproject.toml index cbd18e32..73dd08d7 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -20,6 +20,7 @@ royalnet = "~6.1.4" coloredlogs = "^15.0" aiohttp = "^3.7.4" royalnet-telethon = "~0.2.0" +psycopg2 = "^2.8.6" [tool.poetry.dev-dependencies] royalnet_console = "^0.3.0" diff --git a/royalpack/__main__.py b/royalpack/__main__.py index 68f7395a..1544230a 100644 --- a/royalpack/__main__.py +++ b/royalpack/__main__.py @@ -6,10 +6,11 @@ import asyncio import logging from . import commands +from .database import engine, base logging.basicConfig(level="DEBUG") -config = sc.Scroll.from_file(namespace="ROYALPACK", file_path=pathlib.Path("config.toml")) +config = sc.Scroll.from_file(namespace="ROYALPACK", file_path=pathlib.Path("royalpack.cfg.toml")) pda = rt.TelethonPDA( tg_api_id=config["tapi.id"], @@ -23,6 +24,12 @@ pda.register_partial(commands.cat, ["cat"]) pda.register_partial(commands.color, ["color"]) pda.register_partial(commands.ping, ["ping"]) pda.register_partial(commands.ship, ["ship"]) +pda.register_partial(commands.rage_show, ["rage"]) +pda.register_partial(commands.rage_add, ["rage"]) + + +_engine = engine.lazy_engine.evaluate() +base.Base.metadata.create_all(_engine) loop = asyncio.get_event_loop() diff --git a/royalpack/commands/__init__.py b/royalpack/commands/__init__.py index 17c8932e..9ee33ab7 100644 --- a/royalpack/commands/__init__.py +++ b/royalpack/commands/__init__.py @@ -4,3 +4,4 @@ from .cat import * from .color import * from .ping import * from .ship import * +from .rage import * diff --git a/royalpack/commands/rage.py b/royalpack/commands/rage.py new file mode 100644 index 00000000..4bbec94c --- /dev/null +++ b/royalpack/commands/rage.py @@ -0,0 +1,50 @@ +import royalnet.engineer as engi +from ..database.engine import lazy_Session +from ..database.tables import Rage +import sqlalchemy as s + + +@engi.PartialCommand.new(syntax="") +async def rage_show(*, _sentry: engi.Sentry, _msg: engi.Message, **__): + """ + A-N-G-E-R-Y! + Invia in chat qualcosa che ha fatto arrabbiare un membro anonimo della RYG. + """ + + Session = lazy_Session.evaluate() + with Session(future=True) as session: + + rage = session.execute( + s.select(Rage).order_by(s.func.random()) + ).scalar() + + if rage is None: + await _msg.reply(text=f"😐 Alla fine, non è che sei così arrabbiato...") + else: + await _msg.reply(text=f"😡 {rage.reason}") + + +@engi.PartialCommand.new(syntax="(?P.+)") +async def rage_add(*, _sentry: engi.Sentry, _msg: engi.Message, reason: str, **__): + """ + A-N-G-E-R-Y! + Aggiungi al database qualcosa che ti ha fatto arrabbiare tantissimo. + """ + Session = lazy_Session.evaluate() + with Session(future=True) as session: + + rage = Rage(reason=reason) + session.add(rage) + session.commit() + + count = session.execute( + s.select(s.func.count()).select_from(s.select(Rage).subquery()) + ).scalar() + + await _msg.reply(text=f"😡 G{'R' * count}!") + + +__all__ = ( + "rage_show", + "rage_add", +) diff --git a/royalpack/config.py b/royalpack/config.py new file mode 100644 index 00000000..3afcc559 --- /dev/null +++ b/royalpack/config.py @@ -0,0 +1,11 @@ +import pathlib +import royalnet.scrolls as s +import royalnet.lazy as l + + +lazy_config = l.Lazy(lambda: s.Scroll.from_file("ROYALPACK", pathlib.Path("royalpack.cfg.toml"))) + + +__all__ = ( + "lazy_config", +) diff --git a/royalpack/database/base.py b/royalpack/database/base.py new file mode 100644 index 00000000..13ada81e --- /dev/null +++ b/royalpack/database/base.py @@ -0,0 +1,10 @@ +from __future__ import annotations +import sqlalchemy.ext.declarative as sed + + +Base: sed.declarative_base = sed.declarative_base() + + +__all__ = ( + "Base", +) diff --git a/royalpack/database/engine.py b/royalpack/database/engine.py new file mode 100644 index 00000000..d9cee74c --- /dev/null +++ b/royalpack/database/engine.py @@ -0,0 +1,24 @@ +import sqlalchemy.orm +import royalnet.lazy + +from ..config import * + +# noinspection PyUnresolvedReferences +from . import tables + + +lazy_engine = royalnet.lazy.Lazy(lambda c: sqlalchemy.create_engine(c["database.uri"]), c=lazy_config) +""" +The uninitialized sqlalchemy engine. +""" + +lazy_Session = royalnet.lazy.Lazy(lambda e: sqlalchemy.orm.sessionmaker(bind=e), e=lazy_engine) +""" +The uninitialized Session. +""" + + +__all__ = ( + "lazy_engine", + "lazy_Session", +) diff --git a/royalpack/database/tables/__init__.py b/royalpack/database/tables/__init__.py new file mode 100644 index 00000000..08016860 --- /dev/null +++ b/royalpack/database/tables/__init__.py @@ -0,0 +1 @@ +from .rage import * diff --git a/royalpack/database/tables/_imports.py b/royalpack/database/tables/_imports.py new file mode 100644 index 00000000..254883ce --- /dev/null +++ b/royalpack/database/tables/_imports.py @@ -0,0 +1,19 @@ +from ..base import Base + +import royalnet.royaltyping as t +import royalnet.alchemist as a + +import sqlalchemy as s +import sqlalchemy.orm as o + +import datetime + + +__all__ = ( + "Base", + "t", + "a", + "s", + "o", + "datetime", +) diff --git a/royalpack/database/tables/rage.py b/royalpack/database/tables/rage.py new file mode 100644 index 00000000..4d573ad8 --- /dev/null +++ b/royalpack/database/tables/rage.py @@ -0,0 +1,15 @@ +from __future__ import annotations +from ._imports import * + + +class Rage(Base): + __tablename__ = "rage" + + id = s.Column(s.Integer, primary_key=True) + + reason = s.Column(s.Text, nullable=False) + + +__all__ = ( + "Rage", +)