TelegramBot/plugins/commands/upcoming.py

73 lines
2.2 KiB
Python
Raw Normal View History

2023-08-27 23:43:16 +03:00
from datetime import datetime, timedelta, timezone
2023-08-30 13:52:31 +03:00
from pymongo.errors import OperationFailure
2023-08-27 23:43:16 +03:00
from pyrogram import filters
from pyrogram.types import Message
from pytz import timezone as pytz_timezone
from classes.garbage_entry import GarbageEntry
from classes.pyroclient import PyroClient
from modules.database import col_entries
@PyroClient.on_message(
~filters.scheduled & filters.private & filters.command(["upcoming"], prefixes=["/"]) # 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(
2023-08-29 17:32:37 +03:00
app._("upcoming_empty_location", "messages", locale=user.locale)
2023-08-27 23:43:16 +03:00
)
return
date_min = (
datetime.now(pytz_timezone(user.location.timezone)).replace(
second=0, microsecond=0
)
).replace(tzinfo=timezone.utc)
date_max = (
datetime.now(pytz_timezone(user.location.timezone)).replace(
second=0, microsecond=0
)
+ timedelta(days=30)
).replace(tzinfo=timezone.utc)
2023-08-30 13:52:31 +03:00
try:
entries = [
await GarbageEntry.from_record(entry)
async for entry in col_entries.find(
{
"locations": {"$in": user.location.id},
2023-08-30 13:52:31 +03:00
"date": {"$gte": date_min, "$lte": date_max},
}
)
]
except OperationFailure:
await message.reply_text(
app._("upcoming_empty", "messages", locale=user.locale).format(
name=user.location.name
)
2023-08-27 23:43:16 +03:00
)
2023-08-30 13:52:31 +03:00
return
2023-08-27 23:43:16 +03:00
entries_text = "\n\n".join(
[
f"**{entry.date.strftime(app._('date', 'formats', locale=user.locale))}**:\n{app._(str(entry.garbage_type.value), 'garbage_types')}"
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)
)