Checking user is a member of the channel

This commit is contained in:
2024-02-09 12:52:56 +03:00
parent 780855339c
commit e89cb04c02
2 changed files with 29 additions and 8 deletions

View File

@@ -1,14 +1,17 @@
from typing import List from typing import List
from aiogram import Bot, F, types from aiogram import Bot, F, types
from aiogram.filters import CommandStart from aiogram.enums import ChatMemberStatus
from aiogram.filters import Command, CommandStart
from handlers.handler import Handler from handlers.handler import Handler
from neuroapi import neuroapi from neuroapi import neuroapi
from neuroapi.types import Admin as AdminType from neuroapi.types import Admin as AdminType
from neuroapi.types import BotSettings as BotSettingsType
class UserCommands(Handler): class UserCommands(Handler):
settings: BotSettingsType
def __init__(self, bot: Bot) -> None: def __init__(self, bot: Bot) -> None:
super().__init__(bot) super().__init__(bot)
@@ -19,13 +22,22 @@ class UserCommands(Handler):
@self.router.message(F.chat.type == 'private') @self.router.message(F.chat.type == 'private')
async def forward_post(message: types.Message): async def forward_post(message: types.Message):
self.settings = BotSettingsType.get_active()
user = await bot.get_chat_member(self.settings.channel, message.from_user.id)
if user is None:
await message.reply('Ошибка')
return
user_in_channel = user.status == ChatMemberStatus.LEFT
admins: List[AdminType] = await neuroapi.admin.get() admins: List[AdminType] = await neuroapi.admin.get()
canReply = True canReply = True
for admin in admins: for admin in admins:
await bot.send_message(admin.user_id, f'Вам новое сообщение от пользователя {message.from_user.full_name}. ' + await bot.send_message(admin.user_id, f'Вам новое сообщение от пользователя {message.from_user.full_name}. ' +
(f'\nНик: @{message.from_user.username}' if message.from_user.username else f'ID: {message.from_user.id}')) (f'\nНик: @{message.from_user.username}' if message.from_user.username else f'ID: {message.from_user.id}') +
forwarded_message = await bot.forward_message(admin.user_id, message.chat.id, message.message_id) f'\nПользователь{" не " if user_in_channel else " "}состоит в канале')
if forwarded_message.forward_from is None: try:
canReply = False forwarded_message = await bot.forward_message(admin.user_id, message.chat.id, message.message_id)
if forwarded_message.forward_from is None:
canReply = False
except:
pass
await message.reply('Ваше сообщение было отправлено администраторам'+('' if canReply else '\nНо они не смогут вам ответить из-за ваших настроек конфиденциальности.')) await message.reply('Ваше сообщение было отправлено администраторам'+('' if canReply else '\nНо они не смогут вам ответить из-за ваших настроек конфиденциальности.'))

View File

@@ -1,11 +1,13 @@
from dataclasses import dataclass from dataclasses import dataclass
from typing import Optional
from uuid import UUID from uuid import UUID
from ._helpers import * from ._helpers import *
from ._singleton import Singleton
@dataclass @dataclass
class BotSettings: class BotSettings(Singleton):
uuid: UUID uuid: UUID
message_times: List[str] message_times: List[str]
channel: str channel: str
@@ -27,3 +29,10 @@ class BotSettings:
result["channel"] = from_str(self.channel) result["channel"] = from_str(self.channel)
result["isActive"] = from_bool(self.is_active) result["isActive"] = from_bool(self.is_active)
return result return result
@staticmethod
def get_active() -> Optional['BotSettings']:
try:
return BotSettings._instances[BotSettings]
except:
return None