80 lines
2.5 KiB
Python
80 lines
2.5 KiB
Python
import datetime
|
|
|
|
from asyncio import sleep
|
|
from vkbottle import API
|
|
|
|
import database
|
|
from messages import *
|
|
|
|
|
|
def reset_counters(reset_month: bool):
|
|
database.DB.reset_messages_today()
|
|
print('Дневные счетчики сброшены.')
|
|
|
|
if reset_month:
|
|
database.DB.reset_messages_month()
|
|
print('Месячные счетчики сброшены.')
|
|
|
|
|
|
async def check_birthdays(api: API):
|
|
chats = database.DB.get_chats()
|
|
for chat in chats:
|
|
if chat['active'] == 0:
|
|
continue
|
|
|
|
chat_id = chat['id']
|
|
members = await api.messages.get_conversation_members(peer_id=chat_id, extended=False, fields=['bdate'])
|
|
today = datetime.datetime.today()
|
|
|
|
for member in members.profiles:
|
|
if member.id < 0 or member.bdate is None:
|
|
continue
|
|
|
|
user = database.DB.get_user(chat_id, member.id)
|
|
if user['happy_birthday'] == 0:
|
|
continue
|
|
|
|
parts = member.bdate.split('.')
|
|
if len(parts) < 2:
|
|
continue
|
|
day = int(parts[0])
|
|
month = int(parts[1])
|
|
|
|
if day == today.day and month == today.month:
|
|
message = chat['birthday_message'] or MESSAGE_DEFAULT_BIRTHDAY
|
|
message = message.format(name=f'@id{member.id} ({member.first_name} {member.last_name})')
|
|
await api.messages.send(random_id=0, peer_id=chat_id, message=message)
|
|
|
|
|
|
async def wait_until(target_time: datetime.datetime):
|
|
now = datetime.datetime.now(target_time.tzinfo)
|
|
if now >= target_time:
|
|
return
|
|
|
|
delay_seconds = (target_time - now).total_seconds()
|
|
await sleep(delay_seconds)
|
|
|
|
|
|
async def daily_maintenance_task(api: API):
|
|
tz = datetime.timezone(datetime.timedelta(hours=3), name="MSK")
|
|
|
|
target_time = datetime.time(6, 0, 0, tzinfo=tz)
|
|
now = datetime.datetime.now(tz)
|
|
if now.hour > target_time.hour or now.hour == target_time.hour and now.minute > target_time.minute:
|
|
target_date = now.date() + datetime.timedelta(days=1)
|
|
else:
|
|
target_date = now.date()
|
|
target_datetime = datetime.datetime.combine(target_date, target_time)
|
|
|
|
while True:
|
|
await wait_until(target_datetime)
|
|
|
|
reset_counters(target_datetime.day == 1)
|
|
await check_birthdays(api)
|
|
|
|
target_datetime = target_datetime + datetime.timedelta(days=1)
|
|
|
|
|
|
async def startup_task(api: API):
|
|
me = (await api.groups.get_by_id()).groups[0]
|
|
print(f"Бот '{me.name}' (id={me.id}) запущен.")
|