M
M
merchfck2022-02-11 23:10:51
Python
merchfck, 2022-02-11 23:10:51

How to pull a nickname from a function and bind it to a person?

In general, I make nicknames for the participants in the conversation, create a niki function that describes the course of action and my sqlite3 database.
If the message in the conversation contains /nickname (nickname), then the nickname is registered and made individual. It is necessary to make sure that after writing a nickname, the nickname is assigned to this particular person, to no other, that is, to his id. Then it is necessary that this nickname can be used in further commands, I will give an example below the main code.

db = sqlite3.connect("niki.db")
    sql = db.cursor()

    sql.execute("""CREATE TABLE IF NOT EXISTS users (
        nickname TEXT
    )""")
    db.commit()

    def niki():
        user_nickname011 = event.object.message['text'] #тут я убираю /ник, чтобы остался сам никнейм для обозначения
        user_nickname01 = user_nickname011.split()
        fragment_da = '/ник'
        new_words001 = []
        for word in user_nickname01:
            if fragment_da not in word:
                new_words001.append(word)
        user_nickname = ' '.join(new_words001)
        sql.execute(f"SELECT nickname FROM users WHERE nickname = '{user_nickname}'")
        if sql.fetchone() is None:
            sql.execute(f"INSERT INTO users VALUES (?)", (user_nickname,))
            db.commit()
            vk_session.method('messages.send', {'chat_id':id, "message": "зареган", 'random_id' : 0})
        else:
            vk_session.method('messages.send', {'chat_id':id, "message": "такой ник уже есть", 'random_id' : 0})

    if "/ник" in msg: #сама регистрация никнейма
        niki()

#пример использования ника
if user_id == id2 and "/тест" in msg: #id2 = это id привязанные к участникам беседы
       vk_session.method('messages.send', {'chat_id':id, "message" : f"{user_nickname}", 'random_id' : 0}) #тут мне пишет, что нет такой переменной

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question