From 7ac6cd9bd13c1548d194ad7ab8c426d4a026b2ef Mon Sep 17 00:00:00 2001 From: Kirill Kirilenko Date: Sun, 21 Sep 2025 18:41:52 +0300 Subject: [PATCH] =?UTF-8?q?=D0=A3=D0=B4=D0=B0=D0=BB=D0=B5=D0=BD=D0=B8?= =?UTF-8?q?=D0=B5=20=D0=BF=D0=BE=D0=BB=D1=8C=D0=B7=D0=BE=D0=B2=D0=B0=D1=82?= =?UTF-8?q?=D0=B5=D0=BB=D1=8F=20=D0=B8=D0=B7=20=D0=91=D0=94=20=D0=BF=D1=80?= =?UTF-8?q?=D0=B8=20=D0=B5=D0=B3=D0=BE=20=D0=B8=D1=81=D0=BA=D0=BB=D1=8E?= =?UTF-8?q?=D1=87=D0=B5=D0=BD=D0=B8=D0=B8=20=D0=B8=D0=B7=20=D0=B1=D0=B5?= =?UTF-8?q?=D1=81=D0=B5=D0=B4=D1=8B.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- database.py | 12 ++++++++++++ handlers/action.py | 16 ++++++++++++++-- tasks.py | 25 ++++++++++++++++++++++++- 3 files changed, 50 insertions(+), 3 deletions(-) diff --git a/database.py b/database.py index 095c07e..d5ce2b6 100644 --- a/database.py +++ b/database.py @@ -50,6 +50,10 @@ class Database: f" WHERE id = {chat_id}", kwargs) self.conn.commit() + def chat_delete(self, chat_id: int): + self.cursor.execute("DELETE FROM chats WHERE id = ?", (chat_id,)) + self.conn.commit() + def get_user(self, chat_id: int, user_id: int): self.cursor.execute("SELECT * FROM users WHERE chat_id = ? AND user_id = ?", (chat_id, user_id)) return self.cursor.fetchone() @@ -85,6 +89,10 @@ class Database: f" WHERE chat_id = {chat_id} AND user_id = {user_id}", kwargs) self.conn.commit() + def delete_user(self, chat_id: int, user_id: int): + self.cursor.execute("DELETE FROM users WHERE chat_id = ? AND user_id = ?", (chat_id, user_id)) + self.conn.commit() + def get_top_messages_today(self, chat_id: int): self.cursor.execute(""" SELECT user_id, messages_today AS value FROM users @@ -123,6 +131,10 @@ class Database: """, (chat_id,)) return self.cursor.fetchall() + def delete_users(self, chat_id: int): + self.cursor.execute("DELETE FROM users WHERE chat_id = ?", (chat_id,)) + self.conn.commit() + def reset_messages_today(self): self.cursor.execute("UPDATE users SET messages_today = 0") self.conn.commit() diff --git a/handlers/action.py b/handlers/action.py index ba4b142..4510add 100644 --- a/handlers/action.py +++ b/handlers/action.py @@ -20,11 +20,11 @@ async def user_join_handler(message: Message): user_id = message.action.member_id first_join = (user_id != message.from_id) - user_info = (await message.ctx_api.users.get(user_ids=[user_id]))[0] - if user_id < 0: return + user_info = (await message.ctx_api.users.get(user_ids=[user_id]))[0] + if first_join: response = chat['greeting_join'] or MESSAGE_DEFAULT_GREETING_JOIN else: @@ -32,3 +32,15 @@ async def user_join_handler(message: Message): response = response.format(name=f'@id{user_id} ({user_info.first_name})') await message.answer(response) + + +@labeler.chat_message(action=['chat_kick_user']) +async def user_leave_handler(message: Message): + chat_id = message.peer_id + chat = database.create_chat_if_not_exists(chat_id) + if chat['active'] == 0: + return + + user_id = message.action.member_id + if user_id != message.from_id: + database.DB.delete_user(chat_id, user_id) diff --git a/tasks.py b/tasks.py index 7452c55..c5c88a0 100644 --- a/tasks.py +++ b/tasks.py @@ -2,7 +2,7 @@ import datetime import traceback from asyncio import sleep -from vkbottle import API, bold +from vkbottle import API, bold, VKAPIError import database from handlers.user import format_rating @@ -99,6 +99,29 @@ async def daily_maintenance_task(api: API): target_datetime = target_datetime + datetime.timedelta(days=1) +async def cleanup_users(api: API): + for chat in database.DB.get_chats(): + chat_id = chat['id'] + try: + members = await api.messages.get_conversation_members(peer_id=chat_id, extended=False) + except VKAPIError as e: + continue + + for user in database.DB.get_users(chat_id): + user_id = user['user_id'] + found = False + for profile in members.profiles: + if profile.id == user_id: + found = True + break + + if not found: + database.DB.delete_user(chat_id, user_id) + print(f'Удален пользователь id={user_id}') + + async def startup_task(api: API): me = (await api.groups.get_by_id()).groups[0] print(f"Бот '{me.name}' (id={me.id}) запущен.") + + await cleanup_users(api)