J
J
Jalal Nasirov2021-06-05 10:39:57
Python
Jalal Nasirov, 2021-06-05 10:39:57

Bot vk_api does not work with the database.?

def event1():#ЦИКЛ 1 В ФУНКЦИИ ЧТОБЫ ВЫЙТИ ИЗ ВЛОЖ ЦИКЛОВ
    for event in longpoll.listen():
        if event.type == VkEventType.MESSAGE_NEW:
            if event.to_me:
                message = event.text.lower()
                id = event.user_id
                vk_session.method('messages.send', {'peer_id' : id, 'message' : 'Нажми на кнопоку', 'keyboard' : start_key, 'random_id' : 0})
                while True:
                    id = event.user_id
                    sql.execute("SELECT * FROM users WHERE id = ?", (id,))
                    # ПРОВЕРКА ЕСТЬ ЛИ id в БД
                    user = sql.fetchone()
                    if user == None:# ЕСЛИ НЕТ
                        for event in longpoll.listen():
                            if event.type == VkEventType.MESSAGE_NEW:
                                if event.to_me:
                                    message = event.text.lower()
                                    id = event.user_id
                                    if message == 'начать':
                                        vk_session.method('messages.send', {'peer_id' : id, 'message' : 'Вы ещё не зарегистрированы\n\nНапишите свой никнейм', 'random_id' : 0})
                                        break
                                    else:
                                        vk_session.method('messages.send', {'peer_id' : id, 'message' : 'Вы ещё не зарегистрированы\n\nНапишите свой никнейм', 'random_id' : 0})
                                        break
                        for event in longpoll.listen():
                            if event.type == VkEventType.MESSAGE_NEW:
                                if event.to_me:
                                    message = event.text.lower()
                                    id = event.user_id
                                    nik = message
                                    vk_session.method('messages.send', {'peer_id' : id, 'message' : 'Ваш профиль:\nID — '+str(id)+'\nНик — '+nik, 'random_id' : 0})
                                    user = (id, nik)
                                    sql.execute("INSERT INTO Users VALUES(?, ?);", user)
                                    db.commit()
                                    break
                    elif user != None:# ЕСЛИ ЕСТЬ, ТО ВЫХОДИМ ИЗ ВСЕХ ЦИКЛОВ И ПЕРЕХОДИМ НА ЦИКЛ 2
                        vk_session.method('messages.send', {'peer_id' : id, 'message' : 'Вы зарегистрированы', 'keyboard' : menu_key, 'random_id' : 0})
                        return

a = event1() # ЦИКЛ 1 ФУНКЦИИ


for event in longpoll.listen(): # ЦИКЛ 2
    if event.type == VkEventType.MESSAGE_NEW:
       if event.to_me:
          message = event.text.lower()
          id = event.user_id
          if message == 'меню':
              # ...
              # здесь чел должен оказаться если он есть в БД

If the first person writes to the bot and reaches cycle 2, then another person who is not in the database, who wrote to the bot, will also end up in cycle 2.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
W
WolfInChains, 2021-06-05
@Best_Loops

Because you are using an infinite loop which is blocking the thread. It is not at all clear why he is here, if you can do without him.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question