From 4471b1ce77d72c9a62bbb3e911485c7749bf769a Mon Sep 17 00:00:00 2001 From: Kirill Kirilenko Date: Wed, 20 Aug 2025 01:49:15 +0300 Subject: [PATCH] =?UTF-8?q?=D0=A0=D0=B5=D0=B0=D0=BB=D0=B8=D0=B7=D0=BE?= =?UTF-8?q?=D0=B2=D0=B0=D0=BD=D0=BE=20=D0=BE=D1=82=D1=81=D0=BB=D0=B5=D0=B6?= =?UTF-8?q?=D0=B8=D0=B2=D0=B0=D0=BD=D0=B8=D0=B5=20=D0=BC=D0=BE=D0=BB=D1=87?= =?UTF-8?q?=D1=83=D0=BD=D0=BE=D0=B2.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 3 ++ bot.py | 140 +++++++++++++++++++++++++++++++++++++++++++++++++++++ config.py | 39 +++++++++++++++ 3 files changed, 182 insertions(+) create mode 100644 .gitignore create mode 100644 bot.py create mode 100644 config.py diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..5b92d54 --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +.idea +.venv +db.json diff --git a/bot.py b/bot.py new file mode 100644 index 0000000..3900c86 --- /dev/null +++ b/bot.py @@ -0,0 +1,140 @@ +import calendar +import time + +from vkbottle import GroupTypes, GroupEventType +from vkbottle.bot import Bot, Message + +import config +from config import db_load, db_save, db_print + + +db_load() +db_save() +bot = Bot(config.DB['api_token']) + + +def posix_time(): + return calendar.timegm(time.gmtime()) + + +async def get_user_info(user_id): + info = await bot.api.users.get(user_ids=[user_id]) + if len(info) > 0: + return info[0] + return None + + +@bot.on.chat_message(text="!старт") +async def stats_handler(message: Message): + chat_id = str(message.group_id) + user_id = str(message.from_id) + + # TODO + if user_id != '5326732': + return + + if chat_id not in config.DB['chats']: + config.DB['chats'][chat_id] = {'users': {}} + + chat_users = await bot.api.groups.get_members(chat_id) + + for member in chat_users.items: + member_id = str(member) + if member_id not in config.DB['chats'][chat_id]['users']: + config.DB['chats'][chat_id]['users'][member_id] = { + 'last_message': 0, + 'messages_today': 0, + 'messages_month': 0 + } + + db_save() + + await message.answer('Готова к работе!') + + +@bot.on.chat_message(text="!статистика") +async def stats_handler(message: Message): + chat_id = str(message.group_id) + + chat_users = [] + for user_id in config.DB['chats'][chat_id]['users'].keys(): + flat_user = {**{'id': user_id}, **config.DB['chats'][chat_id]['users'][user_id]} + chat_users.append(flat_user) + + top = sorted(chat_users, key=lambda item: item['messages_today'], reverse=True) + response = '* Статистика за сегодня:\n' + i = 0 + while i < len(top): + if top[i]['messages_today'] == 0: + break + info = await get_user_info(top[i]['id']) + response += '{}. {} {} - {}\n'.format(i+1, info.first_name, info.last_name, top[i]['messages_today']) + i = i + 1 + + top = sorted(chat_users, key=lambda item: item['messages_month'], reverse=True) + response += '\n* Статистика за месяц:\n' + i = 0 + while i < len(top): + if top[i]['messages_month'] == 0: + break + info = await get_user_info(top[i]['id']) + response += '{}. {} {} - {}\n'.format(i+1, info.first_name, info.last_name, top[i]['messages_month']) + i = i + 1 + + await message.answer(response) + + +@bot.on.chat_message(text="!молчуны") +async def silent_handler(message: Message): + chat_id = str(message.group_id) + + chat_users = [] + for user_id in config.DB['chats'][chat_id]['users'].keys(): + flat_user = {**{'id': user_id}, **{'last_message': config.DB['chats'][chat_id]['users'][user_id]['last_message']}} + chat_users.append(flat_user) + top = sorted(chat_users, key=lambda item: item['last_message']) + + now = posix_time() + + response = '* Молчуны чата (не писали больше 2 недель):\n' + for i in range(0, len(top)): + if top[i]['last_message'] == 0: + info = await get_user_info(top[i]['id']) + response += '{}. {} {} - никогда\n'.format(i + 1, info.first_name, info.last_name) + else: + days_silent = round((now - top[i]['last_message']) / 3600 / 24) + if days_silent < 14: + break + info = await get_user_info(top[i]['id']) + response += '{}. {} {} - {} дней\n'.format(i+1, info.first_name, info.last_name, days_silent) + + await message.answer(response) + + +@bot.on.chat_message() +async def any_message_handler(message: Message): + chat_id = str(message.group_id) + user_id = str(message.from_id) + if chat_id not in config.DB['chats']: + config.DB['chats'][chat_id] = {'users': {}} + if user_id not in config.DB['chats'][chat_id]['users']: + config.DB['chats'][chat_id]['users'][user_id] = {} + + config.DB['chats'][chat_id]['users'][user_id]['last_message'] = posix_time() + config.DB['chats'][chat_id]['users'][user_id]['messages_today'] = ( + config.DB['chats'][chat_id]['users'][user_id].get('messages_today', 0) + 1) + config.DB['chats'][chat_id]['users'][user_id]['messages_month'] = ( + config.DB['chats'][chat_id]['users'][user_id].get('messages_month', 0) + 1) + + db_save() + + +# @bot.on.raw_event(GroupEventType.GROUP_JOIN, GroupTypes.GroupJoin) +# async def group_join_handler(event: GroupTypes.GroupJoin): +# chat_id = str(event.group_id) +# user_id = str(event.object.user_id) +# if user_id == bot.api.users.get() + + +print("Bot started.") +bot.run_forever() diff --git a/config.py b/config.py new file mode 100644 index 0000000..6be0cc5 --- /dev/null +++ b/config.py @@ -0,0 +1,39 @@ +import json + + +# db = { +# chats: { +# CHAT_ID: { +# users: { +# USER_ID: { +# messages_today: 10 +# messages_in_month: 1045 +# last_message: 1755627442 +# } +# } +# } +# }, +# api_token: "xxxxxxxx" +# } +DB = {} + + +def db_load(): + global DB + try: + file = open('db.json', 'r') + DB = json.load(file) + file.close() + except IOError: + DB = {'chats': {}, 'api_token': ''} + + +def db_save(): + global DB + with open('db.json', 'w') as file: + json.dump(DB, file, indent=4) + + +def db_print(): + global DB + print(DB)