2023-05-23 16:14:50 +03:00
|
|
|
import contextlib
|
2023-04-01 15:25:41 +03:00
|
|
|
import logging
|
2023-08-18 21:43:09 +03:00
|
|
|
from argparse import ArgumentParser
|
2023-04-01 15:25:41 +03:00
|
|
|
from os import getpid
|
2023-08-18 21:43:09 +03:00
|
|
|
from pathlib import Path
|
2023-04-01 15:25:41 +03:00
|
|
|
|
2023-08-18 21:43:09 +03:00
|
|
|
from libbot import sync
|
2023-06-26 13:50:21 +03:00
|
|
|
from libbot.pyrogram.classes import PyroClient
|
|
|
|
|
2023-08-18 21:43:09 +03:00
|
|
|
from modules.migrator import migrate_database
|
2023-05-26 17:32:56 +03:00
|
|
|
from modules.scheduler import scheduler
|
2023-05-23 16:14:50 +03:00
|
|
|
|
2023-05-14 23:09:04 +03:00
|
|
|
# Uncomment this and the line below client declaration
|
|
|
|
# in order to use context manager in your commands.
|
2023-06-01 16:53:47 +03:00
|
|
|
# Don't forget to disable PyroClient workers if you want
|
|
|
|
# to use convopyro, because workers create more event loops.
|
2023-05-14 23:09:04 +03:00
|
|
|
# from convopyro import Conversation
|
|
|
|
|
2023-04-01 15:25:41 +03:00
|
|
|
|
|
|
|
logging.basicConfig(
|
|
|
|
level=logging.INFO,
|
|
|
|
format="%(name)s.%(funcName)s | %(levelname)s | %(message)s",
|
|
|
|
datefmt="[%X]",
|
|
|
|
)
|
|
|
|
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
2023-08-18 21:43:09 +03:00
|
|
|
parser = ArgumentParser(
|
|
|
|
prog="__name__",
|
|
|
|
description="__description__",
|
|
|
|
)
|
|
|
|
|
|
|
|
# Remove if no database is being used
|
|
|
|
parser.add_argument("--migrate", action="store_true")
|
|
|
|
|
|
|
|
args = parser.parse_args()
|
|
|
|
|
2023-05-23 16:14:50 +03:00
|
|
|
with contextlib.suppress(ImportError):
|
2023-04-01 15:25:41 +03:00
|
|
|
import uvloop
|
|
|
|
|
|
|
|
uvloop.install()
|
|
|
|
|
|
|
|
|
2023-05-14 22:27:07 +03:00
|
|
|
def main():
|
2023-08-18 21:43:09 +03:00
|
|
|
client = PyroClient(
|
|
|
|
scheduler=scheduler, commands_source=sync.json_read(Path("commands.json"))
|
|
|
|
)
|
2023-05-14 23:09:04 +03:00
|
|
|
# Conversation(client)
|
2023-04-01 15:25:41 +03:00
|
|
|
|
2023-08-18 21:43:09 +03:00
|
|
|
# Remove if no database is being used
|
|
|
|
if args.migrate:
|
|
|
|
migrate_database()
|
|
|
|
|
2023-04-01 15:25:41 +03:00
|
|
|
try:
|
2023-05-14 22:27:07 +03:00
|
|
|
client.run()
|
2023-04-01 15:25:41 +03:00
|
|
|
except KeyboardInterrupt:
|
2023-05-23 16:14:50 +03:00
|
|
|
logger.warning("Forcefully shutting down with PID %s...", getpid())
|
2023-04-01 15:25:41 +03:00
|
|
|
finally:
|
2023-06-26 13:50:21 +03:00
|
|
|
if client.scheduler is not None:
|
|
|
|
client.scheduler.shutdown()
|
2023-05-14 22:27:07 +03:00
|
|
|
exit()
|
2023-04-01 15:25:41 +03:00
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
2023-05-14 22:29:23 +03:00
|
|
|
main()
|