Home>

I'm not a genius but I'm writing 400+ lines of code for a tg bot. Everything was fine until I realized that I needed to add a user registration function.

And everything seems to be okay, if not for one problem, when writing using the open (users.py, 'a +') method, it writes the users and password at the end of the file and using open (users.py, 'w') does not I can because it erases past entries in the file the same as c client.py (it is needed to find out if the user is logged in or not, if not, then he does not get access)

here is the stripped down main.py

import telebot
from telebot import types
import time
from pyqiwip2p import QiwiP2P
from pyqiwip2p.types import QiwiCustomer, QiwiDatetime
import random
from settings import token as tk
import datetime
import users
import client
bot= telebot.TeleBot (tk.telebot)
@ bot.message_handler (commands= ['start'])
def start_messages (message):
    rmk= types.ReplyKeyboardMarkup (resize_keyboard= True, one_time_keyboard= True)
    rmk.add (types.KeyboardButton ('Enter'), types.KeyboardButton ('Create'))
    msg= bot.send_message (message.chat.id, "Login or register" + str (message.from_user.username) + '!', reply_markup= rmk)
    bot.register_next_step_handler (msg, reg_users)
def reg_users (message):
    if message.text== 'Login':
        msg= bot.send_message (message.chat.id, "Enter password" + str (message.from_user.username))
        l= str (message.from_user.username)
        print (l)
        bot.register_next_step_handler (msg, l_users)
    if message.text== 'Create':
        msg= bot.send_message (message.chat.id, "Create a password, and your login will be:" + str (message.from_user.username) + '')
        bot.register_next_step_handler (msg, r_users)
def l_users (message):
    p= str (message.text)
    e= True
    while e:
        l= str (message.from_user.username)
        print (l)
        if l in users.users:
            e= False
            if p== users.users [l]:
                s= open ('client.py', 'a +')
                s.write ("'" + l + "',")
                s.close ()
                bot.send_message (message.chat.id, "You are signed in" + str (message.from_user.username) + 'Here is a list of all commands: blah blah blah')
                break
            else:
                msg= bot.send_message (message.chat.id, "password is incorrect!" + str (message.from_user.username) + 'Try again /register')
                bot.register_next_step_handler (msg, l_users)
                break
def r_users (message):
    l= str (message.from_user.username)
    p= str (message.text)
    s= open ('users.py', 'a +')
    s.write ('\ n "' + l + '": "' + str (p) + '",')
    s.close ()
    msg= bot.send_message (message.chat.id, "Your account has been created, log in /register. Your login:" + str (message.from_user.username))
    bot.register_next_step_handler (msg, reg_users)
bot.polling (none_stop= True)

this is what is in users.py (I added myself to it myself to check if it works)

users= {
    "Adammuesly": "1234",
}

here is users.py (without my intervention)

users= {
    } "Adammuesly": "1234",

is written in client.py just like in users.py