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

60 lines
1.8 KiB
Python
Raw Permalink Normal View History

2023-08-27 23:43:16 +03:00
from datetime import datetime, timedelta, timezone
from pyrogram import filters
from pyrogram.types import Message
from classes.garbage_entry import GarbageEntry
from classes.pyroclient import PyroClient
2023-08-30 15:41:34 +03:00
from modules import custom_filters
2023-08-27 23:43:16 +03:00
from modules.database import col_entries
@PyroClient.on_message(
2023-08-30 15:41:34 +03:00
~filters.scheduled & filters.private & filters.command(["upcoming"], prefixes=["/"]) & ~custom_filters.context # type: ignore
2023-08-27 23:43:16 +03:00
)
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(
2023-09-25 00:47:09 +03:00
app._("location_empty", "messages", locale=user.locale)
2023-08-27 23:43:16 +03:00
)
return
date_min = (
2023-09-25 00:47:09 +03:00
datetime.now(user.location.timezone).replace(second=0, microsecond=0)
2023-08-27 23:43:16 +03:00
).replace(tzinfo=timezone.utc)
date_max = (
2023-09-25 00:47:09 +03:00
datetime.now(user.location.timezone).replace(second=0, microsecond=0)
2023-08-27 23:43:16 +03:00
+ timedelta(days=30)
).replace(tzinfo=timezone.utc)
2023-08-30 14:00:21 +03:00
entries = [
await GarbageEntry.from_record(entry)
async for entry in col_entries.find(
{
"locations": user.location.id,
"date": {"$gte": date_min, "$lte": date_max},
}
2023-08-27 23:43:16 +03:00
)
2023-08-30 14:00:21 +03:00
]
2023-08-27 23:43:16 +03:00
entries_text = "\n\n".join(
[
2023-09-06 23:26:44 +03:00
f"**{entry.date.strftime(app._('date', 'formats', locale=user.locale))}**:\n{app._(str(entry.garbage_type.value), 'garbage_types', locale=user.locale)}"
2023-08-27 23:43:16 +03:00
for entry in entries
]
)
if not entries:
await message.reply_text(
2023-08-29 17:32:37 +03:00
app._("upcoming_empty", "messages", locale=user.locale).format(
name=user.location.name
)
2023-08-27 23:43:16 +03:00
)
return
2023-08-29 17:32:37 +03:00
await message.reply_text(
app._("upcoming", "messages", locale=user.locale).format(entries=entries_text)
)