diff --git a/handlers/admin.py b/handlers/admin.py index ef0a5fb..b2f7cd1 100644 --- a/handlers/admin.py +++ b/handlers/admin.py @@ -18,14 +18,16 @@ class ChangeAccessState(StatesGroup): @router.message(F.text == "/admin") async def admin_kb(message: Message): - if models.check_admin(message.from_user.id) is True: + models.LogBase.add(message.from_user.id, message.from_user.username, "/admin") + if models.Users.check_admin(message.from_user.id) is True: await message.answer("Админ меню", reply_markup=kb.owners_kb) @router.message(F.text == "/give_vip") async def change_access(message: Message, state: FSMContext): + models.LogBase.add(message.from_user.id, message.from_user.username, "/give_vip") telegram_id = message.from_user.id - if models.check_admin(telegram_id) is True: + if models.Users.check_admin(telegram_id) is True: await message.answer("Введите айди пользователя") await state.set_state(ChangeAccessState.get_user_id_state) else: @@ -36,14 +38,15 @@ async def change_access(message: Message, state: FSMContext): async def change_user_access(message: Message, state: FSMContext): telegram_id = message.text telegram_id = int(telegram_id) - models.change_access(telegram_id, 1) + models.Users.change_access(telegram_id, 1) await message.answer("Вы успешно поменяли группу пользователя, на Vip") await state.clear() @router.message(F.text == "/kill") async def kill_process(message: Message): - if models.check_admin(message.from_user.id) is True: + models.LogBase.add(message.from_user.id, message.from_user.username, "/kill") + if models.Users.check_admin(message.from_user.id) is True: await message.answer("Отключаюсь(..") sys.exit() else: @@ -52,7 +55,8 @@ async def kill_process(message: Message): @router.message(F.text == "/add_admin") async def add_admin(message: Message, state: FSMContext): - if models.check_admin(message.from_user.id) is True: + models.LogBase.add(message.from_user.id, message.from_user.username, "/add_admin") + if models.Users.check_admin(message.from_user.id) is True: await message.answer("Введите айди пользователя") await state.set_state(ChangeAccessState.add_admin_id_state) else: @@ -62,14 +66,15 @@ async def add_admin(message: Message, state: FSMContext): @router.message(ChangeAccessState.add_admin_id_state, F.text) async def add_admin_state(message: Message, state: FSMContext): telegram_id = int(message.text) - models.change_access(telegram_id, "Admin") + models.Users.change_access(telegram_id, 2) await message.answer("Вы выдали админ доступ пользователю") await state.clear() @router.message(F.text == "/delete_admin") async def delete_admin(message: Message, state: FSMContext): - if models.check_admin(message.from_user.id) is True: + models.LogBase.add(message.from_user.id, message.from_user.username, "delete_admin") + if models.Users.check_admin(message.from_user.id) is True: await message.answer("Введите айди пользователя") await state.set_state(ChangeAccessState.delete_admin_state) else: @@ -78,21 +83,22 @@ async def delete_admin(message: Message, state: FSMContext): @router.message(ChangeAccessState.delete_admin_state, F.text) async def delete_admin_state(message: Message, state: FSMContext): - telegram_id = message.text - models.change_access(telegram_id, "User") + telegram_id = int(message.text) + models.Users.change_access(telegram_id, 0) await message.answer("Вы удалили админ доступ у пользователя") await state.clear() @router.message(F.text == "/admin_list") async def admin_list(message: Message): - owners = models.session.query(models.Users).where(models.Users.user_group == 3) - admins = models.session.query(models.Users).where(models.Users.user_group == 2) - bot_message = 'Список Админов:\n' + models.LogBase.add(message.from_user.id, message.from_user.username, "admin_list") + owners = models.session.query(models.Users).where(models.Users.group_id == 3) + admins = models.session.query(models.Users).where(models.Users.group_id == 2) + bot_message = 'Список Админов:\n\n' for admin in owners: - x = f"@{admin.nickname}: {admin.telegram_id} Owner\n" + x = f"@{admin.username}: {admin.telegram_id} Owner\n" bot_message += x for admin in admins: - x = f"@{admin.nickname}: {admin.telegram_id} Admin\n" + x = f"@{admin.username}: {admin.telegram_id} Admin\n" bot_message += x await message.answer(bot_message) diff --git a/handlers/client.py b/handlers/client.py index aa3b9a9..64cd5f7 100644 --- a/handlers/client.py +++ b/handlers/client.py @@ -1,13 +1,10 @@ from aiogram import F -from aiogram.fsm.state import StatesGroup, State from aiogram.types import Message, CallbackQuery import steammarket as sm from data import json_support, case_translation import keyboards as kb from aiogram import Router -from aiogram.fsm.context import FSMContext import models -from data.config import cases as constcases router = Router() json_data = "user_list.json" @@ -17,6 +14,8 @@ async def cases(message: Message): x = '' user_id = f"{message.from_user.id}" + models.Users.add_user(message.from_user.id, message.from_user.username) + models.LogBase.add(message.from_user.id, message.from_user.username, "/cases") data = json_support.read_inf(json_data) if user_id in data.keys() and len(data[user_id]) > 0: for case in data[user_id]: @@ -30,6 +29,7 @@ async def cases(message: Message): @router.message(F.text == "/add_case") async def item_list(message: Message): + models.LogBase.add(message.from_user.id, message.from_user.username, "/add_case") await message.answer("Какой кейс вы хотите добавить?:", reply_markup=kb.cases) @@ -57,6 +57,7 @@ async def answer(callback: CallbackQuery): @router.message(F.text == '/clear') async def clear(message: Message): + models.LogBase.add(message.from_user.id, message.from_user.username, "/clear") data = json_support.read_inf(json_data) user_id = f"{message.from_user.id}" del data[user_id] @@ -66,20 +67,21 @@ async def clear(message: Message): @router.message(F.text == "/start") async def start(message: Message): - telegram_id = message.from_user.id - username = message.from_user.username - models.add_user(telegram_id, username) + models.LogBase.add(message.from_user.id, message.from_user.username, "/start") + models.Users.add_user(message.from_user.id, message.from_user.username) await message.answer("Ну привет", reply_markup=kb.main_kb) @router.message(F.text == "/help") async def help_func(message: Message): + models.LogBase.add(message.from_user.id, message.from_user.username, "/help") await message.answer("Помогите...") @router.message(F.text == "/vip") async def get_vip(message: Message): - if models.check_vip(message.from_user.id) is True: + models.LogBase.add(message.from_user.id, message.from_user.username, "/vip") + if models.Users.check_vip(message.from_user.id) is True: await message.answer("Вип меню", reply_markup=kb.users_vip_kb) else: await message.answer("У вас нет доступа к это команде") diff --git a/handlers/vip.py b/handlers/vip.py index 685474c..c0ce687 100644 --- a/handlers/vip.py +++ b/handlers/vip.py @@ -17,7 +17,8 @@ class AddInventory(StatesGroup): @router.message(F.text == "/support") async def support(message: Message): - if models.check_vip(message.from_user.id) is True: + models.LogBase.add(message.from_user.id, message.from_user.username, "/support") + if models.Users.check_vip(message.from_user.id) is True: await message.answer("Напишите - @ZhuchkaTriplesix") else: await message.answer("У вас нет доступа к этой команде") @@ -25,14 +26,17 @@ async def support(message: Message): @router.message(F.text == "/vip_help") async def vip_help(message: Message): + models.LogBase.add(message.from_user.id, message.from_user.username, "/vip_help") await message.answer( - "Команды для Vip пользователей:\n/support - Персональная помощь с ботом\n/steam_id - Поменять стим айди (по умолчанию его нет)\n/my_steamid - Ваш стим айди") + "Команды для Vip пользователей:\n/support - Персональная помощь с ботом\n/add_item - Добавить предметы\n/my_items - Ваши предметы") +# noinspection PyTypeChecker @router.message(F.text == "/my_items") async def my_cases(message: Message): - if models.check_vip(message.from_user.id) is True: - my_items = models.user_items(message.from_user.id) + models.LogBase.add(message.from_user.id, message.from_user.username, "/my_items") + if models.Users.check_vip(message.from_user.id) is True: + my_items = models.Items.user_items(message.from_user.id) answer = 'Ваши предметы:\n\n' for key in my_items: answer = answer + f"{key}: {my_items[key]}\n" @@ -41,9 +45,10 @@ async def my_cases(message: Message): await message.answer("У вас нет доступа к этой команде") +# noinspection PyTypeChecker @router.callback_query(F.data == "items_price") async def items_price(callback: CallbackQuery): - items = models.user_items(callback.from_user.id) + items = models.Items.user_items(callback.from_user.id) answer = 'Стоимость вашего инвентаря:\n\n' total = 0 for key in items: @@ -60,6 +65,7 @@ async def items_price(callback: CallbackQuery): @router.message(F.text == "/add_item") async def add_item(message: Message, state: FSMContext): + models.LogBase.add(message.from_user.id, message.from_user.username, "/add_item") await message.answer( "Напишите название предмета на англ и через . кол-во\nПримеры написания:\nClutch Case.100\nFracture Case.10") await state.set_state(AddInventory.add_item_state) @@ -72,6 +78,6 @@ async def add_item_bd(message: Message, state: FSMContext): hash_name = mes[0] item_count = int(mes[1]) telegram_id = message.from_user.id - models.add_item(telegram_id, hash_name, item_count) + models.Items.add_item(telegram_id, hash_name, item_count) await message.answer(f"Вы добавили {hash_name}") await state.clear() diff --git a/keyboards.py b/keyboards.py index f3182f3..2e6f365 100644 --- a/keyboards.py +++ b/keyboards.py @@ -71,8 +71,8 @@ ] admins_kb = ReplyKeyboardMarkup(keyboard=admin_keyboard, resize_keyboard=True) user_vip_keyboard = [ - [KeyboardButton(text="/steamid"), - KeyboardButton(text="/my_steamid")], + [KeyboardButton(text="/add_item"), + KeyboardButton(text="/my_items")], [KeyboardButton(text="/support"), KeyboardButton(text="/vip_help")] ] diff --git a/models.py b/models.py index 8023a0a..7aec10b 100644 --- a/models.py +++ b/models.py @@ -14,6 +14,7 @@ conn = engine.connect() +# noinspection PyTypeChecker,PyMethodParameters,PyShadowingNames class Users(Base): __tablename__ = "users" id = Column(Integer, primary_key=True) @@ -23,7 +24,62 @@ class Users(Base): created_date = Column(DateTime, default=datetime.datetime.utcnow) updated_at = Column(DateTime, default=datetime.datetime.utcnow) - + def add_user(telegram_id: int, username: str): + session = Session() + user = session.query(Users).where(Users.telegram_id == telegram_id).first() + if user is not None: + print("User is already in database") + else: + user = Users(telegram_id=telegram_id, username=username) + Users.add_close(user, session) + + def add_close(user, session): + session.add(user) + session.commit() + session.close() + print("Successful adding") + + def add_admin(telegram_id: int, username: str): + session = Session() + user = session.query(Users).where(Users.telegram_id == telegram_id).first() + if user is not None: + print("User is already in database") + else: + user = Users(username=username, telegram_id=telegram_id, group_id=2) + Users.add_close(user, session) + + def change_access(telegram_id: int, group_id: int): + session = Session() + user = session.query(Users).where(Users.telegram_id == telegram_id).first() + if user is not None: + user.group_id = group_id + user.updated_at = datetime.datetime.utcnow() + session.commit() + session.close() + + def check_vip(telegram_id: int) -> object: + user = session.query(Users).where(Users.telegram_id == telegram_id).first() + user_group = user.group_id + if user_group >= 1: + return True + else: + return False + + def check_admin(telegram_id: int) -> object: + user = session.query(Users).where(Users.telegram_id == telegram_id).first() + user_group = user.group_id + if user_group >= 2: + return True + else: + return False + + def get_id(telegram_id: int) -> object: + user = session.query(Users).where(Users.telegram_id == telegram_id).first() + user_id = user.id + return user_id + + +# noinspection PyTypeChecker,PyShadowingNames,PyMethodParameters class Items(Base): __tablename__ = "items" id = Column(Integer, primary_key=True) @@ -31,90 +87,47 @@ class Items(Base): hash_name = Column(String(length=30)) item_count = Column(Integer) + def add_item(telegram_id: int, hash_name: str, item_count: int) -> object: + session = Session() + user_id = Users.get_id(telegram_id) + item = session.query(Items).filter(Items.hash_name == hash_name).where(Items.user_id == user_id).first() + if item.hash_name is None: + item = Items(user_id=user_id, hash_name=hash_name, item_count=item_count) + session.add(item) + session.commit() + session.close() + else: + item.item_count = item_count + session.commit() + session.close() + + def user_items(telegram_id: int) -> object: + session = Session() + user_id = Users.get_id(telegram_id) + items = session.query(Items).where(Items.user_id == user_id).all() + user_item = {} + for i in items: + hash_name = i.hash_name + item_count = i.item_count + u = {hash_name: item_count} + user_item.update(u) + return user_item + + +# noinspection PyShadowingNames,PyMethodParameters +class LogBase(Base): + __tablename__ = "functions_log" + id = Column(Integer, primary_key=True) + telegram_id = Column(BigInteger, index=True) + username = Column(String(length=32)) + function_name = Column(String(length=12)) -Base.metadata.create_all(engine) - - -def add_close(user, session): - session.add(user) - session.commit() - session.close() - print("Successful adding") - - -def add_user(telegram_id, username): - session = Session() - user = session.query(Users).where(Users.telegram_id == telegram_id).first() - if user is not None: - print("User is already in database") - else: - user = Users(telegram_id=telegram_id, username=username) - add_close(user, session) - - -def add_admin(telegram_id, username): - session = Session() - user = session.query(Users).where(Users.telegram_id == telegram_id).first() - if user is not None: - print("User is already in database") - else: - user = Users(username=username, telegram_id=telegram_id, group_id=2) - add_close(user, session) - - -def change_access(telegram_id, group_id): - session = Session() - user = session.query(Users).where(Users.telegram_id == telegram_id).first() - if user is not None: - user.group_id = group_id - user.updated_at = datetime.datetime.utcnow() - session.commit() - session.close() - - -def check_vip(telegram_id): - user = session.query(Users).where(Users.telegram_id == telegram_id).first() - user_group = user.group_id - if user_group >= 1: - return True - else: - return False - - -def check_admin(telegram_id): - user = session.query(Users).where(Users.telegram_id == telegram_id).first() - user_group = user.group_id - if user_group >= 2: - return True - else: - return False - - -def get_id(telegram_id): - user = session.query(Users).where(Users.telegram_id == telegram_id).first() - user_id = user.id - return user_id - - -def add_item(telegram_id, hash_name, item_count): - session = Session() - user_id = get_id(telegram_id) - item = session.query(Items).filter(Items.hash_name == hash_name).where(Items.user_id == user_id).first() - if item is None: - item = Items(user_id=user_id, hash_name=hash_name, item_count=item_count) - session.add(item) + def add(telegram_id: int, username: str, function_name: str): + session = Session() + func = LogBase(telegram_id=telegram_id, username=username, function_name=function_name) + session.add(func) session.commit() session.close() -def user_items(telegram_id): - session = Session() - user_id = get_id(telegram_id) - items = session.query(Items).where(Items.user_id == user_id).all() - user_item = {} - for i in items: - hash_name = i.hash_name - item_count = i.item_count - u = {hash_name: item_count} - user_item.update(u) - return user_item +Base.metadata.create_all(engine) diff --git a/user_list.json b/user_list.json index da113c0..b8dbe94 100644 --- a/user_list.json +++ b/user_list.json @@ -4,25 +4,16 @@ "Danger Zone Case", "Clutch Case" ], - "960622004": [ - "CS:GO Weapon Case", - "eSports 2013 Case", - "Operation Bravo Case", - "CS:GO Weapon Case 2", - "eSports 2013 Winter Case", - "Winter Offensive Weapon Case", - "CS:GO Weapon Case 3", - "Operation Phoenix Weapon Case", - "Huntsman Weapon Case" - ], "6643425631": [ "Danger Zone Case", "CS20 Case", "Clutch Case" ], + "960622004": [ + "Snakebite Case", + "Revolution Case" + ], "736491563": [ - "Clutch Case", - "Danger Zone Case", - "CS20 Case" + "Operation Breakout Weapon Case" ] } \ No newline at end of file