Skip to content

Commit

Permalink
Merge pull request #76 from ksdfg/upsert-users
Browse files Browse the repository at this point in the history
feat(users): update username in DB if its changed
  • Loading branch information
ksdfg authored Oct 26, 2022
2 parents 223ca3a + a020560 commit 3556d13
Showing 1 changed file with 6 additions and 7 deletions.
13 changes: 6 additions & 7 deletions telebot/modules/db/users.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,21 +23,20 @@ def __repr__(self):
def add_user(user_id: int, username: str):
"""
Add/Update a user in the database
If the username has changed, that'll be updated
:param user_id: ID of the user we want to add
:param username: username of the user we want to add
:return:
"""
User(id=user_id, username=username).save()
User(id=user_id, username=username).update(username=username, upsert=True)


def get_user(username: str) -> Optional[int]:
"""
get the user ID of a user
:param username: username of the user who's ID we want to fetch
:return: ID of the user
:param username: username of the user whose ID we want to fetch
:return: ID of the user, if a user with that username exists in the database
"""
user = User.objects(username=username).first()
if user:
if user := User.objects(username=username).first():
return user.id
else:
return None
return None

0 comments on commit 3556d13

Please sign in to comment.