Info command

This commit is contained in:
2023-12-27 16:01:42 +03:00
parent 915e36890b
commit 43b1c5ecf4
2 changed files with 41 additions and 21 deletions

View File

@@ -29,6 +29,7 @@ def get_post_info(post: neuroTypes.Post, post_id: int) -> str:
class AdminCommands(Handler): class AdminCommands(Handler):
settings: BotSettingsType settings: BotSettingsType
def __init__(self, bot: Bot) -> None: def __init__(self, bot: Bot) -> None:
super().__init__(bot) super().__init__(bot)
self.router.message.middleware(AdminMiddleware()) self.router.message.middleware(AdminMiddleware())
@@ -41,14 +42,33 @@ class AdminCommands(Handler):
@self.router.message(Command('info')) @self.router.message(Command('info'))
async def info_command(message: types.Message): async def info_command(message: types.Message):
posts: List[neuroTypes.Post] = await neuroapi.post.get_will_post() posts: List[neuroTypes.Post] = await neuroapi.post.get_will_post()
admins: List[neuroTypes.Admin] = await neuroapi.admin.get()
post_c = {} post_c = {}
for post in posts: for post in posts:
if post.from_user_id not in post_c: if post.from_user_id not in post_c:
post_c[post.from_user_id] = 1 post_c[post.from_user_id] = 1
else: else:
post_c[post.from_user_id] += 1 post_c[post.from_user_id] += 1
await message.answer(str(post_c)) res = "Количество постов от админов:\n"
res2 = "\nПосты:\n"
for admin in admins:
if admin.user_id in post_c:
res += f'[{admin.user_name}](tg://user?id={admin.user_id}): {post_c[admin.user_id]}\n'
else:
res += f'[{admin.user_name}](tg://user?id={admin.user_id}): 0\n'
admin_posts = list(
filter(lambda x: x.from_user_id == admin.user_id, posts))
res2 += f'Посты от {admin.user_name}:\n'
if len(admin_posts):
for i, post in enumerate(admin_posts):
#TODO: Если возможно, сделать чтоб было ссылкой на сообщений с /newpost
res2 += f'{i+1}. {post.text}\n'
else:
res2 += 'Их нет\)\n'
await message.answer((res+res2).replace('#', '\#').replace("_", "\_").replace('.', '\.').replace(',', '\,').replace('!', '\!'), parse_mode='markdownv2')
"""
TODO: Изменение постов сделать нормально, не через редактирование сообщений
@self.router.message(ChangePosts()) @self.router.message(ChangePosts())
async def change_post(message: types.Message, state: FSMContext): async def change_post(message: types.Message, state: FSMContext):
posts = await neuroapi.post.get_will_post() posts = await neuroapi.post.get_will_post()
@@ -179,13 +199,15 @@ class AdminCommands(Handler):
data = await state.get_data() data = await state.get_data()
if 'edit_msg' in data: if 'edit_msg' in data:
await bot.delete_message(message_id=data['edit_msg'], chat_id=callback.message.chat.id) await bot.delete_message(message_id=data['edit_msg'], chat_id=callback.message.chat.id)
"""
@self.router.message(Command('post')) @self.router.message(Command('post'))
async def post(message: types.Message | None = None): async def post(message: types.Message | None = None):
try: try:
post = await neuroapi.post.get_post_to_post() post = await neuroapi.post.get_post_to_post()
if (post): if (post):
images = MediaGroupBuilder(caption=post.text + '\n\nПредложка: @neur0w0men_reply_bot') images = MediaGroupBuilder(
caption=post.text + '\n\nПредложка: @neur0w0men_reply_bot')
image: neuroTypes.Image image: neuroTypes.Image
for image in sorted(post.images, key=lambda x: x.message_id): for image in sorted(post.images, key=lambda x: x.message_id):
images.add_photo(image.file_id, images.add_photo(image.file_id,
@@ -196,7 +218,8 @@ class AdminCommands(Handler):
elif message: elif message:
await message.answer('Нет постов') await message.answer('Нет постов')
except Exception as e: except Exception as e:
if message: await message.answer(f'Ошибка {e}') if message:
await message.answer(f'Ошибка {e}')
@self.router.message(NewSoloPostFilter()) @self.router.message(NewSoloPostFilter())
async def post_solo(message: types.Message): async def post_solo(message: types.Message):
@@ -230,8 +253,8 @@ class AdminCommands(Handler):
schedule.every().friday.at(i).do(post, None) schedule.every().friday.at(i).do(post, None)
if i not in ['10:00', '20:00']: if i not in ['10:00', '20:00']:
schedule.every().sunday.at(i).do(post, None) schedule.every().sunday.at(i).do(post, None)
if mes: await mes.answer('Настройки обновлены!') if mes:
await mes.answer('Настройки обновлены!')
async def settings_and_schedule_checker(): async def settings_and_schedule_checker():
await update_settings() await update_settings()
@@ -240,7 +263,3 @@ class AdminCommands(Handler):
await asyncio.sleep(1) await asyncio.sleep(1)
asyncio.create_task(settings_and_schedule_checker()) asyncio.create_task(settings_and_schedule_checker())

View File

@@ -1,22 +1,23 @@
from dataclasses import dataclass from dataclasses import dataclass
from typing import Any from typing import Any
from ._helpers import * from ._helpers import *
@dataclass @dataclass
class Admin: class Admin:
id: int
user_id: int user_id: int
user_name: str
@staticmethod @staticmethod
def from_dict(obj: Any) -> 'Admin': def from_dict(obj: Any) -> 'Admin':
assert isinstance(obj, dict) assert isinstance(obj, dict)
id = from_int(obj.get("id"))
user_id = int(from_str(obj.get("user_id"))) user_id = int(from_str(obj.get("user_id")))
return Admin(id, user_id) user_name = from_str(obj.get("user_name"))
return Admin(user_id, user_name)
def to_dict(self) -> dict: def to_dict(self) -> dict:
result: dict = {} result: dict = {}
result["id"] = from_int(self.id)
result["user_id"] = from_str(str(self.user_id)) result["user_id"] = from_str(str(self.user_id))
result["user_name"] = from_str(self.user_name)
return result return result