Исправлено добавление в БД левых пользователей, которые VK может вернуть в списке profiles.
При выбрасывании исключения в daily_maintenance_task работа продолжается.
This commit is contained in:
parent
827a8bdf3f
commit
ecfc83d5ad
1 changed files with 25 additions and 16 deletions
41
tasks.py
41
tasks.py
|
|
@ -1,4 +1,5 @@
|
|||
import datetime
|
||||
import traceback
|
||||
|
||||
from asyncio import sleep
|
||||
from vkbottle import API
|
||||
|
|
@ -26,24 +27,29 @@ async def check_birthdays(api: API):
|
|||
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:
|
||||
for item in members.items:
|
||||
user_id = item.member_id
|
||||
if user_id < 0:
|
||||
continue
|
||||
user = database.create_user_if_not_exists(chat_id, user_id)
|
||||
|
||||
user = database.create_user_if_not_exists(chat_id, member.id)
|
||||
if user['happy_birthday'] == 0:
|
||||
continue
|
||||
for profile in members.profiles:
|
||||
if profile.id == user_id:
|
||||
if profile.bdate is None or user['happy_birthday'] == 0:
|
||||
break
|
||||
|
||||
parts = member.bdate.split('.')
|
||||
if len(parts) < 2:
|
||||
continue
|
||||
day = int(parts[0])
|
||||
month = int(parts[1])
|
||||
parts = profile.bdate.split('.')
|
||||
if len(parts) < 2:
|
||||
break
|
||||
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)
|
||||
if day == today.day and month == today.month:
|
||||
message = chat['birthday_message'] or MESSAGE_DEFAULT_BIRTHDAY
|
||||
message = message.format(name=f'@id{user_id} ({profile.first_name} {profile.last_name})')
|
||||
await api.messages.send(random_id=0, peer_id=chat_id, message=message)
|
||||
|
||||
break
|
||||
|
||||
|
||||
async def wait_until(target_time: datetime.datetime):
|
||||
|
|
@ -69,8 +75,11 @@ async def daily_maintenance_task(api: API):
|
|||
while True:
|
||||
await wait_until(target_datetime)
|
||||
|
||||
reset_counters(target_datetime.day == 1)
|
||||
await check_birthdays(api)
|
||||
try:
|
||||
reset_counters(target_datetime.day == 1)
|
||||
await check_birthdays(api)
|
||||
except Exception:
|
||||
print(traceback.format_exc())
|
||||
|
||||
target_datetime = target_datetime + datetime.timedelta(days=1)
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue