2023-08-27 23:43:16 +03:00
|
|
|
import logging
|
2024-05-31 00:23:07 +03:00
|
|
|
from datetime import datetime
|
2023-08-27 23:43:16 +03:00
|
|
|
|
2024-05-31 00:23:07 +03:00
|
|
|
import pytz
|
2023-08-27 23:43:16 +03:00
|
|
|
from convopyro import listen_message
|
|
|
|
from pyrogram import filters
|
|
|
|
from pyrogram.types import ForceReply, Message, ReplyKeyboardRemove
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
|
|
|
@PyroClient.on_message(
|
2023-08-30 15:41:34 +03:00
|
|
|
~filters.scheduled & filters.private & filters.command(["set_time"], prefixes=["/"]) & ~custom_filters.context # type: ignore
|
2023-08-27 23:43:16 +03:00
|
|
|
)
|
|
|
|
async def command_set_time(app: PyroClient, message: Message):
|
|
|
|
user = await app.find_user(message.from_user)
|
|
|
|
|
2023-09-25 00:47:09 +03:00
|
|
|
if user.location is None:
|
|
|
|
await message.reply_text(
|
|
|
|
app._("location_empty", "messages", locale=user.locale)
|
|
|
|
)
|
|
|
|
return
|
|
|
|
|
2023-08-27 23:43:16 +03:00
|
|
|
await message.reply_text(
|
2023-08-30 12:01:59 +03:00
|
|
|
app._("set_time", "messages", locale=user.locale),
|
|
|
|
reply_markup=ForceReply(
|
|
|
|
placeholder=app._("set_time", "force_replies", locale=user.locale)
|
|
|
|
),
|
2023-08-27 23:43:16 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
while True:
|
2023-08-30 15:41:34 +03:00
|
|
|
app.contexts.append(message.from_user.id)
|
2023-08-27 23:43:16 +03:00
|
|
|
answer = await listen_message(app, message.chat.id, 300)
|
2023-08-30 15:41:34 +03:00
|
|
|
app.contexts.remove(message.from_user.id)
|
2023-08-27 23:43:16 +03:00
|
|
|
|
|
|
|
if answer is None or answer.text == "/cancel":
|
2023-08-29 17:32:37 +03:00
|
|
|
await message.reply_text(
|
|
|
|
app._("cancelled", "messages", locale=user.locale),
|
|
|
|
reply_markup=ReplyKeyboardRemove(),
|
|
|
|
)
|
2023-08-27 23:43:16 +03:00
|
|
|
return
|
|
|
|
|
|
|
|
try:
|
|
|
|
datetime.strptime(answer.text, "%H:%M")
|
|
|
|
except ValueError:
|
|
|
|
await answer.reply_text(
|
2023-08-30 12:01:59 +03:00
|
|
|
app._("set_time_invalid", "messages", locale=user.locale).format(
|
|
|
|
cancel_notice=app._("cancel", "messages", locale=user.locale)
|
|
|
|
)
|
2023-08-27 23:43:16 +03:00
|
|
|
)
|
|
|
|
continue
|
|
|
|
|
|
|
|
break
|
|
|
|
|
2024-05-31 00:23:07 +03:00
|
|
|
# Time we got from the user
|
|
|
|
parsed_time = datetime.strptime(answer.text, "%H:%M")
|
2023-10-15 23:21:10 +03:00
|
|
|
|
2024-05-31 00:23:07 +03:00
|
|
|
# Datetime user means in their timezone
|
|
|
|
user_time = datetime.now(user.location.timezone).replace(
|
|
|
|
hour=parsed_time.hour, minute=parsed_time.minute, second=0, microsecond=0
|
2023-09-25 00:47:09 +03:00
|
|
|
)
|
2023-10-15 23:21:10 +03:00
|
|
|
|
2024-05-31 00:23:07 +03:00
|
|
|
# Datetime in user's timezone moved to UTC timezone
|
|
|
|
utc_time = user_time.astimezone(pytz.utc)
|
2023-08-27 23:43:16 +03:00
|
|
|
|
2024-05-31 00:23:07 +03:00
|
|
|
await user.update_time(hour=utc_time.hour, minute=utc_time.minute)
|
2023-08-27 23:43:16 +03:00
|
|
|
|
|
|
|
logger.info(
|
2024-05-30 18:47:23 +03:00
|
|
|
"User %s has selected notification time of %s (%s UTC)",
|
2023-08-27 23:43:16 +03:00
|
|
|
user.id,
|
|
|
|
user_time.strftime("%H:%M"),
|
2024-05-31 00:23:07 +03:00
|
|
|
utc_time.strftime("%H:%M"),
|
2023-08-27 23:43:16 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
await answer.reply_text(
|
2023-08-30 12:01:59 +03:00
|
|
|
app._("set_time_finished", "messages", locale=user.locale).format(
|
|
|
|
offset=user.offset,
|
2024-05-31 00:23:07 +03:00
|
|
|
time=user_time.strftime(app._("time", "formats", locale=user.locale)),
|
2024-05-30 18:47:23 +03:00
|
|
|
toggle_notice=(
|
|
|
|
"" if user.enabled else app._("toggle", "messages", locale=user.locale)
|
|
|
|
),
|
2023-08-30 12:01:59 +03:00
|
|
|
),
|
2023-08-27 23:43:16 +03:00
|
|
|
reply_markup=ReplyKeyboardRemove(),
|
|
|
|
)
|