class ExistsUserMiddleware(BaseMiddleware):
async def __call__(self, handler, event, data):
this_user = data.get("event_from_user")
if not this_user.is_bot:
get_user = get_userx(user_id=this_user.id)
user_id = this_user.id
update_userx(user_id=user_id, last_activity=get_unix())
user_login = this_user.username
user_name = clear_html(this_user.first_name)
user_surname = clear_html(this_user.last_name)
user_fullname = clear_html(this_user.first_name)
user_language = this_user.language_code
if user_login is None: user_login = ""
if user_name is None: user_name = ""
if user_surname is None: user_surname = ""
if user_fullname is None: user_fullname = ""
if user_language != "ru": user_language = "en"
if len(user_surname) >= 1: user_fullname += f" {user_surname}"
if get_user is None:
add_userx(user_id, user_login.lower(), user_name, user_surname, user_fullname)
bot = data.get('bot')
await bot.send_message(
chat_id=ADMIN_CHAT_ID,
text=ded(
f'''
🐘 Новый пользователь <a href="t.me/{user_login.lower()}">{user_id}</a>
'''
),
disable_web_page_preview=True
)
try:
message = data.get('event_update').message.text.split(' ')
except:
await preview_func(
bot=data.get('bot'),
user_id=user_id
)
return await handler(event, data)
if len(message) > 1:
try:
referal_id = int(message[1])
bot: Bot = data.get('bot')
except TypeError:
pass
else:
if get_userx(user_id=referal_id) and this_user.id != referal_id:
add_referalx(
referrer_id=this_user.id,
referred_id=referal_id,
)
else:
if user_name != get_user['user_name']:
update_userx(get_user['user_id'], user_name=user_name)
if user_surname != get_user['user_surname']:
update_userx(get_user['user_id'], user_surname=user_surname)
if user_fullname != get_user['user_fullname']:
update_userx(get_user['user_id'], user_fullname=user_fullname)
if user_login.lower() != get_user['user_login']:
update_userx(get_user['user_id'], user_login=user_login.lower())
data['user'] = get_userx(user_id=user_id)
return await handler(event, data)