Skip to content

Commit

Permalink
add optional
Browse files Browse the repository at this point in the history
  • Loading branch information
feyhoa committed Nov 12, 2023
1 parent 100d6e5 commit 5cbb7eb
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 20 deletions.
25 changes: 12 additions & 13 deletions bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -261,20 +261,21 @@ def send_audio_answer(update: Update, context: CallbackContext):

def add_admin(update: Update, context: CallbackContext):
user = init_user(update.effective_user)
if user.is_admin:
if not user.is_admin:
return

if len(context.args):
db_id = context.args[0]
if len(context.args):
db_id = context.args[0]

if db_id.isdigit() and 6 <= len(db_id) <= 10:
create_admin(int(db_id))
update.message.reply_text(f"Выданы права администратора пользователю с id: {db_id}")
else:
update.message.reply_text("Некорректно введён id пользователя!")

set_last_usage(update.effective_user)
if db_id.isdigit() and 6 <= len(db_id) <= 10:
create_admin(int(db_id))
update.message.reply_text(f"Выданы права администратора пользователю с id: {db_id}")
else:
update.message.reply_text("Не введён id пользователя!")
update.message.reply_text("Некорректно введён id пользователя!")

set_last_usage(update.effective_user)
else:
update.message.reply_text("Не введён id пользователя!")


def start_question_conversation(update: Update, context: CallbackContext):
Expand Down Expand Up @@ -370,11 +371,9 @@ def run(self):
token_try = self.work_queue.get()
self.process(token_try)

# TODO переделать на получение пользователя
if ADMIN.isdigit():
create_admin(int(ADMIN))
finally:

pass

@staticmethod
Expand Down
6 changes: 3 additions & 3 deletions db.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import datetime
import logging
from typing import List, Union
from typing import List, Optional
from string import punctuation
from collections import Counter
import nltk
Expand Down Expand Up @@ -129,7 +129,7 @@ def __str__(self) -> str:
)


def get_user(user_id: int) -> Union[User, None]:
def get_user(user_id: int) -> Optional[User]:
try:
return User.objects.get({'id': user_id})
except User.DoesNotExist:
Expand Down Expand Up @@ -176,7 +176,7 @@ def init_user(user) -> User:
is_bot=user.is_bot,
is_admin=False,
username=user.username,
language_code='ru',
language_code=user.language_code,
).save()


Expand Down
8 changes: 4 additions & 4 deletions questions_db.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from datetime import datetime
from typing import Union
from typing import Optional

from pymodm import fields, MongoModel

Expand Down Expand Up @@ -33,18 +33,18 @@ def init_question(user: User, text):
)


def get_question(quest_id) -> Union[Question, None]:
def get_question(quest_id) -> Optional[Question]:
try:
return Question.objects.get({'_id': quest_id})
except Question.DoesNotExist:
return None


def list_questions() -> Union[list[Question], None]:
def list_questions() -> list:
try:
return Question.objects.get({'answered': False})
except Question.DoesNotExist:
return None
return []


class Answer(MongoModel):
Expand Down

0 comments on commit 5cbb7eb

Please sign in to comment.