Initial commit

This commit is contained in:
2023-08-27 22:43:16 +02:00
commit 502ed0406e
40 changed files with 1854 additions and 0 deletions

13
modules/custom_filters.py Normal file
View File

@@ -0,0 +1,13 @@
"""Custom message filters"""
from pyrogram import filters
from pyrogram.types import Message
from classes.pyroclient import PyroClient
async def _owner_func(_, __: PyroClient, message: Message):
return False if message.from_user is None else __.owner == message.from_user.id
owner = filters.create(_owner_func)

28
modules/database.py Normal file
View File

@@ -0,0 +1,28 @@
"""Module that provides all database collections"""
from typing import Any, Mapping
from async_pymongo import AsyncClient, AsyncCollection, AsyncDatabase
from libbot.sync import config_get
db_config: Mapping[str, Any] = config_get("database")
if db_config["user"] is not None and db_config["password"] is not None:
con_string = "mongodb://{0}:{1}@{2}:{3}/{4}".format(
db_config["user"],
db_config["password"],
db_config["host"],
db_config["port"],
db_config["name"],
)
else:
con_string = "mongodb://{0}:{1}/{2}".format(
db_config["host"], db_config["port"], db_config["name"]
)
db_client = AsyncClient(con_string)
db: AsyncDatabase = db_client.get_database(name=db_config["name"])
col_users: AsyncCollection = db.get_collection("users")
col_entries: AsyncCollection = db.get_collection("entries")
col_locations: AsyncCollection = db.get_collection("locations")

22
modules/migrator.py Normal file
View File

@@ -0,0 +1,22 @@
from typing import Any, Mapping
from libbot.sync import config_get
from mongodb_migrations.cli import MigrationManager
from mongodb_migrations.config import Configuration
def migrate_database() -> None:
"""Apply migrations from folder `migrations/` to the database"""
db_config: Mapping[str, Any] = config_get("database")
manager_config = Configuration(
{
"mongo_host": db_config["host"],
"mongo_port": db_config["port"],
"mongo_database": db_config["name"],
"mongo_username": db_config["user"],
"mongo_password": db_config["password"],
}
)
manager = MigrationManager(manager_config)
manager.run()

72
modules/reminder.py Normal file
View File

@@ -0,0 +1,72 @@
import logging
from datetime import datetime, timedelta, timezone
from libbot.pyrogram.classes import PyroClient
from pytz import timezone as pytz_timezone
from classes.enums import GarbageType
from classes.location import Location
from classes.pyrouser import PyroUser
from modules.database import col_entries, col_users
logger = logging.getLogger(__name__)
async def remind(app: PyroClient) -> None:
now = datetime.now()
users = await col_users.find(
{"time_hour": now.hour, "time_minute": now.minute}
).to_list()
for user_db in users:
user = PyroUser(**user_db)
logger.info("Processing %s...", user.id)
if not user.enabled or user.location is None:
continue
try:
location: Location = await app.get_location(user.location.id) # type: ignore
except ValueError:
continue
user_date = (
datetime.now(pytz_timezone(location.timezone)).replace(
second=0, microsecond=0
)
+ timedelta(days=user.offset)
).replace(tzinfo=timezone.utc)
entries = await col_entries.find(
{
"location": {"$in": location.id},
"date": user_date.replace(hour=0, minute=0),
}
).to_list()
logger.info("Entries of %s for %s: %s", user.id, user_date, entries)
for entry in entries:
try:
garbage_type = app._(
str(GarbageType(entry["garbage_type"]).value),
"garbage_types",
locale=user.locale,
)
garbage_date = datetime.strftime(
entry["date"], app._("date", "formats", locale=user.locale)
)
await app.send_message(
user.id,
"**Garbage Collection**\n\nType: {type}\nDate: {date}\n\nDon't forget to prepare your bin for collection!".format(
type=garbage_type, date=garbage_date
),
)
except Exception as exc:
logger.warning(
"Could not send a notification to %s due to %s", user.id, exc
)
continue

3
modules/scheduler.py Normal file
View File

@@ -0,0 +1,3 @@
from apscheduler.schedulers.asyncio import AsyncIOScheduler
scheduler = AsyncIOScheduler()

0
modules/utils.py Normal file
View File