This repository has been archived on 2024-10-10. You can view files and clone it, but cannot push or open issues or pull requests.
TelegramBot/plugins/commands/upcoming.py

61 lines
1.8 KiB
Python

from datetime import datetime, timedelta
import pytz
from pyrogram import filters
from pyrogram.types import Message
from classes.garbage_entry import GarbageEntry
from classes.pyroclient import PyroClient
from modules import custom_filters
from modules.database_api import col_entries
@PyroClient.on_message(
~filters.scheduled & filters.private & filters.command(["upcoming"], prefixes=["/"]) & ~custom_filters.context # type: ignore
)
async def command_upcoming(app: PyroClient, message: Message):
user = await app.find_user(message.from_user)
if user.location is None:
await message.reply_text(
app._("location_empty", "messages", locale=user.locale)
)
return
date_min = (
datetime.now(user.location.timezone).replace(second=0, microsecond=0)
).replace(tzinfo=pytz.utc)
date_max = (
datetime.now(user.location.timezone).replace(second=0, microsecond=0)
+ timedelta(days=30)
).replace(tzinfo=pytz.utc)
entries = [
await GarbageEntry.from_record(entry)
async for entry in col_entries.find(
{
"locations": user.location.id,
"date": {"$gte": date_min, "$lte": date_max},
}
)
]
entries_text = "\n\n".join(
[
f"**{entry.date.strftime(app._('date', 'formats', locale=user.locale))}**:\n{app._(str(entry.garbage_type.value), 'garbage_types', locale=user.locale)}"
for entry in entries
]
)
if not entries:
await message.reply_text(
app._("upcoming_empty", "messages", locale=user.locale).format(
name=user.location.name
)
)
return
await message.reply_text(
app._("upcoming", "messages", locale=user.locale).format(entries=entries_text)
)