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/set_offset.py

83 lines
2.6 KiB
Python
Raw Permalink Normal View History

2023-08-27 23:43:16 +03:00
import logging
from datetime import datetime
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-09-25 00:47:09 +03:00
from modules.utils import from_utc
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_offset"], prefixes=["/"]) & ~custom_filters.context # type: ignore
2023-08-27 23:43:16 +03:00
)
async def command_set_offset(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_offset", "messages", locale=user.locale),
reply_markup=ForceReply(
placeholder=app._("set_offset", "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:
num = int(answer.text)
if num < 0 or num > 7:
raise ValueError(
"Offset bust not be less than 0 and greater than 7 days."
)
except (ValueError, TypeError):
await answer.reply_text(
2023-08-30 12:01:59 +03:00
app._("set_offset_invalid", "messages", locale=user.locale).format(
cancel_notice=app._("cancel", "messages", locale=user.locale)
)
2023-08-27 23:43:16 +03:00
)
continue
break
offset = int(answer.text)
await user.update_offset(offset)
logger.info("User %s has set offset to %s", user.id, offset)
2023-09-25 00:47:09 +03:00
garbage_time = from_utc(
datetime(1970, 1, 1, user.time_hour, user.time_minute),
None if user.location is None else user.location.timezone.zone,
2023-08-27 23:43:16 +03:00
).strftime(app._("time", "formats"))
await answer.reply_text(
2023-08-30 12:01:59 +03:00
app._("set_offset_finished", "messages", locale=user.locale).format(
offset=offset,
time=garbage_time,
toggle_notice=""
if user.enabled
else app._("toggle", "messages", locale=user.locale),
),
2023-08-27 23:43:16 +03:00
reply_markup=ReplyKeyboardRemove(),
)