import vk_api, json
from vk_api.longpoll import VkLongPoll, VkEventType
from time import sleep, time
from threading import Thread
from os.path import abspath as tdir
vk_session = vk_api.VkApi(token = '6666666')
longpoll = VkLongPoll(vk_session)
class User():
def __init__(self, id, money, ruby, ruderalis, vip):
self.id = id
self.money = money
self.ruby = ruby
self.ruderalis = ruderalis
self.vip = vip
def check_registration(id):
members = vk_session.method('groups.getMembers', {'group_id' : 5464757650})['items']
return (id in members)
def save_bd(users):
lines = []
for user in users:
lines.append(f'"id" : {user.id}, "money" : {user.money}, "ruby" : {user.ruby}, "ruderalis" : {user.ruderalis}, "vip" : {user.vip}')
lines = '\n'.join(lines)
with open(f'{tdir(__file__)}/data.txt'.replace('\\', '/').replace('main.py/', ''), 'w', encoding = 'utf-8') as file:
file.write(lines)
file.close()
def read_bd():
users = []
with open(f'{tdir(__file__)}/data.txt'.replace('\\', '/').replace('main.py/', ''), 'r', encoding = 'utf-8') as file:
lines = [x.replace('\n', '') for x in file.readlines()]
file.close()
for line in lines:
line = eval('{' + line + '}')
if line != '{}':
users.append(User(id = line['id'], money = line['money'], ruby = line['ruby'], ruderalis = line['ruderalis'], vip = line['vip']))
return users
def get_keyboard(buts): # функция создания клавиатур
nb = []
color = ''
for i in range(len(buts)):
nb.append([])
for k in range(len(buts[i])):
nb[i].append(None)
for i in range(len(buts)):
for k in range(len(buts[i])):
text = buts[i][k][0]
color = {'зеленый' : 'positive', 'красный' : 'negative', 'синий' : 'primary', 'белый' : 'secondary'}[buts[i][k][1]]
nb[i][k] = {"action": {"type": "text", "payload": "{\"button\": \"" + "1" + "\"}", "label": f"{text}"}, "color": f"{color}"}
first_keyboard = {'one_time': False, 'buttons': nb}
first_keyboard = json.dumps(first_keyboard, ensure_ascii=False).encode('utf-8')
first_keyboard = str(first_keyboard.decode('utf-8'))
return first_keyboard
def sender(id, text, key):
vk_session.method('messages.send', {'user_id' : id, 'message' : text, 'random_id' : 0, 'keyboard' : key, 'dont_parse_links' : 1})
clear_key = get_keyboard([])
users = read_bd()
for event in longpoll.listen():
if event.type == VkEventType.MESSAGE_NEW:
if event.to_me:
id = event.user_id
msg = event.text.lower()
if msg == 'начать':
if check_registration(id):
flag = 0
for user in users:
if user.id == id:
flag = 1
break
if not(flag): # если новый пользователь, то переходим в меню регистрации
users.append( User(id = id, money = 0, cash = 0, ruderalis = 1, vip = 0, ruby = 0) )
sender(id, 'Привет', menu_key)
elif flag: # если пользователь старый, то выходим в главное меню
for user in users:
if user.id == id:
sender(id, 'Привет_2', menu_key)
else:
sender(id, 'Вы не подписаны на группу!\nДля того, чтобы пользоваться ботом, необходимо подписаться на сообщество!', clear_key)
save_bd(users)