vk_chat_bot/bot.py

140 lines
4.5 KiB
Python

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()