diff --git a/poetry.lock b/poetry.lock index e7a0d83c..6814c8c4 100644 --- a/poetry.lock +++ b/poetry.lock @@ -146,7 +146,7 @@ python-versions = ">=2.7,!=3.0.*,!=3.1.*,!=3.2.*,!=3.3.*" name = "pyaes" version = "1.6.1" description = "Pure-Python Implementation of the AES block-cipher and common modes of operation" -category = "main" +category = "dev" optional = false python-versions = "*" @@ -154,7 +154,7 @@ python-versions = "*" name = "pyasn1" version = "0.4.8" description = "ASN.1 types and codecs" -category = "main" +category = "dev" optional = false python-versions = "*" @@ -183,7 +183,7 @@ python-versions = "*" [[package]] name = "royalnet" -version = "6.2.8" +version = "6.2.10" description = "A multipurpose bot framework" category = "main" optional = false @@ -197,7 +197,7 @@ toml = ">=0.10.1,<0.11.0" [[package]] name = "royalnet-console" -version = "2.0.0" +version = "2.0.1" description = "A terminal-based frontend for the royalnet.engineer module." category = "dev" optional = false @@ -211,21 +211,22 @@ royalnet = ">=6.2.0,<6.3.0" [[package]] name = "royalnet-telethon" -version = "0.2.0" +version = "0.3.1" description = "A Telethon-based frontend for the royalnet.engineer module." -category = "main" +category = "dev" optional = false python-versions = ">=3.8,<4.0" [package.dependencies] -royalnet = ">=6.1.2,<7.0.0" +click = ">=7.1.2,<8.0.0" +royalnet = ">=6.2.10,<6.3.0" Telethon = ">=1.21.1,<2.0.0" [[package]] name = "rsa" version = "4.7.2" description = "Pure-Python RSA implementation" -category = "main" +category = "dev" optional = false python-versions = ">=3.5, <4" @@ -267,7 +268,7 @@ sqlcipher = ["sqlcipher3-binary"] name = "telethon" version = "1.21.1" description = "Full-featured Telegram client library for Python 3" -category = "main" +category = "dev" optional = false python-versions = ">=3.5" @@ -309,7 +310,7 @@ multidict = ">=4.0" [metadata] lock-version = "1.1" python-versions = "^3.8" -content-hash = "eef3a751692dcb5a9cdd93efaf828e87a98b6e90d0588ae363b33967250e4aed" +content-hash = "22fc870d53f6152b719e9e0fad1ff1d77961e68a52a4cb09765e203dd4c72ece" [metadata.files] aiohttp = [ @@ -566,16 +567,16 @@ pyreadline = [ {file = "pyreadline-2.1.zip", hash = "sha256:4530592fc2e85b25b1a9f79664433da09237c1a270e4d78ea5aa3a2c7229e2d1"}, ] royalnet = [ - {file = "royalnet-6.2.8-py3-none-any.whl", hash = "sha256:ac45f86fd938e038dad18875b4729745107921fdba8059ad4f5d701f7aecda0e"}, - {file = "royalnet-6.2.8.tar.gz", hash = "sha256:e19d53e3d469b4dc58f4f75af3001773661df20382614d63250585e60257635d"}, + {file = "royalnet-6.2.10-py3-none-any.whl", hash = "sha256:794d2f2c5d319a1649cf97355d7ad07c0ad36df4d96b4ad534eb1dc1c4a39caa"}, + {file = "royalnet-6.2.10.tar.gz", hash = "sha256:1a3f87b77fccce8df7571fcd0d8fbc5837b9f173a6d15c5e1e3e679a377b8ba5"}, ] royalnet-console = [ - {file = "royalnet-console-2.0.0.tar.gz", hash = "sha256:43242071eecf4e2ce260e65feb45ae8c4f8658404114b5e3ba2d814f2de4d587"}, - {file = "royalnet_console-2.0.0-py3-none-any.whl", hash = "sha256:2f7a0aadcc9eba0787da3e931907ec2863651e4c487ea11857cb5474d073c4ca"}, + {file = "royalnet-console-2.0.1.tar.gz", hash = "sha256:33b054157447bbf8bac7f711548395d028ca677b16624108e932ec94c428f46c"}, + {file = "royalnet_console-2.0.1-py3-none-any.whl", hash = "sha256:de262fa8fa9072fc90cc8cf4e891b950bbfd25a9918571a63f609d81c8f435c5"}, ] royalnet-telethon = [ - {file = "royalnet-telethon-0.2.0.tar.gz", hash = "sha256:1c3a2e7f3cb231b4692264037f448113af39c1686c63e2e9cd8ae7f1887d2bfe"}, - {file = "royalnet_telethon-0.2.0-py3-none-any.whl", hash = "sha256:1ed81bbf56933291ae1856fdf5b378726c54db99321e4ff36575a3f5d14ef4a7"}, + {file = "royalnet-telethon-0.3.1.tar.gz", hash = "sha256:2d6b9e364d09da3357822abeeab12663f6097248f4489909d0b20030b5791b0a"}, + {file = "royalnet_telethon-0.3.1-py3-none-any.whl", hash = "sha256:24953539fb947d60aff05b558753a4e96a8ddd0b1aa8aca744d06995ef56f1b1"}, ] rsa = [ {file = "rsa-4.7.2-py3-none-any.whl", hash = "sha256:78f9a9bf4e7be0c5ded4583326e7461e3a3c5aae24073648b4bdfa797d78c9d2"}, diff --git a/pyproject.toml b/pyproject.toml index e74e9686..08d91e67 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -16,13 +16,13 @@ classifiers = [ [tool.poetry.dependencies] python = "^3.8" -royalnet = "~6.2.1" +royalnet = "~6.2.10" coloredlogs = "^15.0" aiohttp = "^3.7.4" -royalnet-telethon = "~0.2.0" psycopg2 = "^2.8.6" [tool.poetry.dev-dependencies] +royalnet_telethon = "~0.3.0" royalnet_console = "^2.0.0" [build-system] diff --git a/royalpack/__main__.py b/royalpack/__main__.py index 6e920925..46875419 100644 --- a/royalpack/__main__.py +++ b/royalpack/__main__.py @@ -1,13 +1,14 @@ import royalnet.engineer as engi import royalnet.scrolls as sc import royalnet_console as rc +import royalnet_telethon as rt import pathlib -import logging +import coloredlogs from . import commands from .database import engine, base -logging.basicConfig(level="DEBUG") +coloredlogs.install(level="DEBUG", isatty=True) config = sc.Scroll.from_file(namespace="ROYALPACK", file_path=pathlib.Path("royalpack.cfg.toml")) @@ -15,19 +16,32 @@ engine_ = engine.lazy_engine.evaluate() base.Base.metadata.create_all(engine_) pda = engi.PDA(implementations=[ - rc.ConsolePDAImplementation(name="1", extensions=[ - engi.SQLAlchemyExtension(engine=engine_) - ]) + # rc.ConsolePDAImplementation( + # name="1", + # extensions=[ + # engi.SQLAlchemyExtension(engine=engine_), + # ] + # ), + rt.TelethonPDAImplementation( + name="1", + extensions=[ + engi.SQLAlchemyExtension(engine=engine_), + ], + tg_api_id=config["telegram.api.id"], + tg_api_hash=config["telegram.api.hash"], + bot_username=config["telegram.bot.username"], + bot_token=config["telegram.bot.token"], + ) ]) -pda.implementations["console.1"].register_partialcommand(commands.ahnonlosoio, ["ahnonlosoio"]) -# pda.implementations["console.1"].register_partialcommand(commands.answer, ["answer"]) -# pda.implementations["console.1"].register_partialcommand(commands.cat, ["cat"]) -# pda.implementations["console.1"].register_partialcommand(commands.color, ["color"]) -# pda.implementations["console.1"].register_partialcommand(commands.ping, ["ping"]) -# pda.implementations["console.1"].register_partialcommand(commands.ship, ["ship"]) -# pda.implementations["console.1"].register_partialcommand(commands.rage_show, ["rage"]) -# pda.implementations["console.1"].register_partialcommand(commands.rage_add, ["rage"]) +pda.implementations["telethon.1"].register_partialcommand(commands.ahnonlosoio, ["ahnonlosoio"]) +pda.implementations["telethon.1"].register_partialcommand(commands.answer, ["answer"]) +pda.implementations["telethon.1"].register_partialcommand(commands.cat, ["cat"]) +pda.implementations["telethon.1"].register_partialcommand(commands.color, ["color"]) +pda.implementations["telethon.1"].register_partialcommand(commands.ping, ["ping"]) +pda.implementations["telethon.1"].register_partialcommand(commands.ship, ["ship"]) +pda.implementations["telethon.1"].register_partialcommand(commands.rage_show, ["rage"]) +pda.implementations["telethon.1"].register_partialcommand(commands.rage_add, ["rage"]) pda.run() diff --git a/royalpack/commands/rage.py b/royalpack/commands/rage.py index 4bbec94c..ceee5e13 100644 --- a/royalpack/commands/rage.py +++ b/royalpack/commands/rage.py @@ -1,47 +1,42 @@ import royalnet.engineer as engi -from ..database.engine import lazy_Session from ..database.tables import Rage import sqlalchemy as s +import sqlalchemy.orm as so @engi.PartialCommand.new(syntax="") -async def rage_show(*, _sentry: engi.Sentry, _msg: engi.Message, **__): +async def rage_show(*, _sentry: engi.Sentry, _msg: engi.Message, _session: so.Session, **__): """ 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() - 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}") + 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, **__): +async def rage_add(*, _sentry: engi.Sentry, _msg: engi.Message, _session: so.Session, 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() + rage = Rage(reason=reason) + _session.add(rage) + _session.commit() - count = session.execute( - s.select(s.func.count()).select_from(s.select(Rage).subquery()) - ).scalar() + count = _session.execute( + s.select(s.func.count()).select_from(s.select(Rage).subquery()) + ).scalar() - await _msg.reply(text=f"😡 G{'R' * count}!") + await _msg.reply(text=f"😡 G{'R' * count}!") __all__ = ( diff --git a/royalpack/database/engine.py b/royalpack/database/engine.py index d9cee74c..0dfd9cd8 100644 --- a/royalpack/database/engine.py +++ b/royalpack/database/engine.py @@ -12,13 +12,6 @@ lazy_engine = royalnet.lazy.Lazy(lambda c: sqlalchemy.create_engine(c["database. 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", )