Почему мой бот в одном чате работает а в другом нет (все права администратора выданы)
import asyncio
from typing import Any
import logging
from contextlib import suppress
from aiogram import Router, Bot, Dispatcher, F, types
from aiogram.types import Message
from aiogram.filters import Command, CommandObject
from aiogram.enums import ParseMode
from aiogram.exceptions import TelegramBadRequest
from datetime import datetime, timedelta
from aiogram.enums.chat_member_status import ChatMemberStatus
logging.basicConfig(level=logging.INFO)
def parse_time(time_string: str | None) -> datetime | None:
if not time_string:
return None
match_ = re.match(r"(\d+)([a-z])", time_string.lower().strip())
current_datetime = datetime.now()
if match_:
value = int(match_.group(1))
unit = match_.group(2)
match unit:
case "h": time_delta = timedelta(hours=value)
case "d": time_delta = timedelta(days=value)
case "w": time_delta = timedelta(weeks=value)
case _: time_delta = None
else:
return None
new_datetime = current_datetime + time_delta
return new_datetime
router = Router()
async def is_admin(message, bot):
member = await bot.get_chat_member(message.chat.id, message.from_user.id)
bot = await bot.get_chat_member(message.chat.id, bot.id)
if member.status not in [ChatMemberStatus.ADMINISTRATOR, ChatMemberStatus.CREATOR] or bot.status != ChatMemberStatus.ADMINISTRATOR:
return False
return True
@router.message(Command("ban"))
async def ban(message: Message, bot: Bot, command: CommandObject | None = None) -> Any:
reply = message.reply_to_message
if not reply:
return None
until_date = parse_time(command.args)
mention = reply.from_user.mention_html(reply.from_user.first_name)
with suppress(TelegramBadRequest):
try:
await bot.ban_chat_member(
chat_id=message.chat.id, user_id=reply.from_user.id, until_date=until_date
)
await message.answer("Пользователь, заблокированный администратором")
except TelegramBadRequest as e:
logging.error(f"An error occurred: {e}")
@router.message(F.chat.type == "supergroup", F.new_chat_members)
async def welcome_new_members(message: Message, bot: Bot) -> Any:
for user in message.new_chat_members:
await message.answer(f"Приветсвую {user.first_name}, в нашей группе")
async def main() -> None:
try:
bot = Bot("Мой токен")
dp = Dispatcher()
dp.include_router(router)
await bot.delete_webhook(True)
await dp.start_polling(bot)
except Exception as e:
logging.error(f"An error occurred in the main function: {e}")
if __name__ == "__main__":
asyncio.run(main())```
Ответы (1 шт):
Автор решения: AnnaBazueva
→ Ссылка
Проверила команду /ban
в двух группа - успешно:
INFO:aiogram.dispatcher:Start polling
INFO:aiogram.dispatcher:Run polling for bot @BotWonderbot id=6837065902 - 'Wonderbot'
INFO:aiogram.event:Update id=676189679 is not handled. Duration 0 ms by bot id=6837065902
INFO:aiogram.event:Update id=676189680 is handled. Duration 0 ms by bot id=6837065902
INFO:aiogram.event:Update id=676189681 is not handled. Duration 0 ms by bot id=6837065902
INFO:aiogram.event:Update id=676189682 is handled. Duration 0 ms by bot id=6837065902
ERROR:aiogram.dispatcher:Failed to fetch updates - TelegramNetworkError: HTTP Client says - Request timeout error
WARNING:aiogram.dispatcher:Sleep for 1.000000 seconds and try again... (tryings = 0, bot id = 6837065902)
INFO:aiogram.dispatcher:Connection established (tryings = 1, bot id = 6837065902)
INFO:aiogram.event:Update id=676189683 is not handled. Duration 0 ms by bot id=6837065902
INFO:aiogram.event:Update id=676189685 is not handled. Duration 0 ms by bot id=6837065902
INFO:aiogram.event:Update id=676189684 is handled. Duration 375 ms by bot id=6837065902
INFO:aiogram.event:Update id=676189687 is not handled. Duration 16 ms by bot id=6837065902
INFO:aiogram.event:Update id=676189686 is handled. Duration 358 ms by bot id=6837065902
ERROR:aiogram.dispatcher:Failed to fetch updates - TelegramNetworkError: HTTP Client says - Request timeout error
WARNING:aiogram.dispatcher:Sleep for 1.000000 seconds and try again... (tryings = 0, bot id = 6837065902)
INFO:aiogram.dispatcher:Connection established (tryings = 1, bot id = 6837065902)
в импортах почему то нет модуля re
? (но это так...)
Проверьте разрешения для бота в проблемной группе.
Уточните что не работает, и добавьте в вопрос логи...