fix inactive users removal

This commit is contained in:
bakatrouble 2019-03-17 15:07:38 +03:00
parent 28586ed19b
commit cbf244ca45

14
main.py
View File

@ -184,6 +184,8 @@ def _process_media_group(bot: Bot, messages: List[Message]):
elif hasattr(message, 'audio') and message.audio: elif hasattr(message, 'audio') and message.audio:
media_group.append(InputMediaAudio(message.audio.file_id, caption=caption, parse_mode='html')) media_group.append(InputMediaAudio(message.audio.file_id, caption=caption, parse_mode='html'))
remove_uids = []
for uid, user in users.items(): for uid, user in users.items():
sleep(.02) sleep(.02)
@ -199,8 +201,7 @@ def _process_media_group(bot: Bot, messages: List[Message]):
user.messages_forward[r.message_id] = conn.root.counter user.messages_forward[r.message_id] = conn.root.counter
user.messages_reverse[conn.root.counter] = r.message_id user.messages_reverse[conn.root.counter] = r.message_id
except Unauthorized: except Unauthorized:
user = _remove_user(uid) remove_uids.append(uid)
commit()
bot.send_message(MANAGEMENT_CHAT, f'<a href="tg://user?id={uid}">{user.name}</a> был удален ' bot.send_message(MANAGEMENT_CHAT, f'<a href="tg://user?id={uid}">{user.name}</a> был удален '
f'из-за блокировки бота', parse_mode='html') f'из-за блокировки бота', parse_mode='html')
except Exception: except Exception:
@ -208,6 +209,8 @@ def _process_media_group(bot: Bot, messages: List[Message]):
sentry_sdk.capture_exception() sentry_sdk.capture_exception()
conn.root.counter += len(messages) conn.root.counter += len(messages)
commit() commit()
for uid in remove_uids:
_remove_user(uid)
def _process_message(bot: Bot, m: Message): def _process_message(bot: Bot, m: Message):
@ -228,6 +231,8 @@ def _process_message(bot: Bot, m: Message):
if m.reply_to_message and m.reply_to_message.message_id in users[current_chat].messages_forward: if m.reply_to_message and m.reply_to_message.message_id in users[current_chat].messages_forward:
reply_to_message_internal_id = users[current_chat].messages_forward[m.reply_to_message.message_id] reply_to_message_internal_id = users[current_chat].messages_forward[m.reply_to_message.message_id]
remove_uids = []
for uid, user in users.items(): for uid, user in users.items():
sleep(.02) sleep(.02)
@ -285,8 +290,7 @@ def _process_message(bot: Bot, m: Message):
user.messages_forward[r.message_id] = conn.root.counter user.messages_forward[r.message_id] = conn.root.counter
user.messages_reverse[conn.root.counter] = r.message_id user.messages_reverse[conn.root.counter] = r.message_id
except Unauthorized: except Unauthorized:
user = _remove_user(uid) remove_uids.append(uid)
commit()
bot.send_message(MANAGEMENT_CHAT, f'<a href="tg://user?id={uid}">{user.name}</a> был удален ' bot.send_message(MANAGEMENT_CHAT, f'<a href="tg://user?id={uid}">{user.name}</a> был удален '
f'из-за блокировки бота', parse_mode='html') f'из-за блокировки бота', parse_mode='html')
except Exception: except Exception:
@ -294,6 +298,8 @@ def _process_message(bot: Bot, m: Message):
sentry_sdk.capture_exception() sentry_sdk.capture_exception()
conn.root.counter += 1 conn.root.counter += 1
commit() commit()
for uid in remove_uids:
_remove_user(uid)
def task_queue(u: Updater): def task_queue(u: Updater):