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