|
1 | 1 | import asyncio |
2 | 2 | import logging |
| 3 | +from datetime import datetime, timedelta, timezone |
3 | 4 | from typing import Any |
4 | 5 |
|
5 | 6 | from aiogram import Bot |
@@ -49,20 +50,32 @@ async def _safe_request(self, func, *args, **kwargs) -> Any: |
49 | 50 | logger.error(f"BotAPI Error: {e}", exc_info=True) |
50 | 51 | raise e |
51 | 52 |
|
52 | | - async def kick_chat_member(self, chat_id: int | str, user_id: int) -> bool: |
| 53 | + async def kick_chat_member( |
| 54 | + self, chat_id: int | str, user_id: int, ban_duration_minutes: int = 1 |
| 55 | + ) -> bool: |
53 | 56 | """ |
54 | | - Kicks a user from a chat (bans and then unbans to allow rejoining). |
| 57 | + Kicks a user from a chat by temporarily banning them. |
| 58 | +
|
| 59 | + Args: |
| 60 | + chat_id: The chat ID to kick the user from |
| 61 | + user_id: The user ID to kick |
| 62 | + ban_duration_minutes: Duration of the ban in minutes (default: 1) |
55 | 63 | """ |
56 | | - logger.info(f"Kicking user {user_id} from chat {chat_id}") |
57 | | - # Ban to remove |
58 | | - banned = await self._safe_request( |
59 | | - self.bot.ban_chat_member, chat_id=chat_id, user_id=user_id |
| 64 | + logger.info( |
| 65 | + f"Kicking user {user_id} from chat {chat_id} " |
| 66 | + f"(temp ban for {ban_duration_minutes} minute(s))" |
60 | 67 | ) |
61 | | - # Unban to allow rejoining (classic 'kick') |
62 | | - unbanned = await self._safe_request( |
63 | | - self.bot.unban_chat_member, chat_id=chat_id, user_id=user_id |
| 68 | + # Calculate temporary ban until date |
| 69 | + until_date = datetime.now(timezone.utc) + timedelta( |
| 70 | + minutes=ban_duration_minutes |
| 71 | + ) |
| 72 | + # Temporarily ban the user (allows rejoining after ban_duration_minutes) |
| 73 | + return await self._safe_request( |
| 74 | + self.bot.ban_chat_member, |
| 75 | + chat_id=chat_id, |
| 76 | + user_id=user_id, |
| 77 | + until_date=until_date, |
64 | 78 | ) |
65 | | - return banned and unbanned |
66 | 79 |
|
67 | 80 | async def unban_chat_member(self, chat_id: int | str, user_id: int) -> bool: |
68 | 81 | """ |
|
0 commit comments