Skip to content

Commit d32f5da

Browse files
authored
fix: use temporary ban for kicking users (#230)
2 parents 8e94bf5 + 1633444 commit d32f5da

1 file changed

Lines changed: 23 additions & 10 deletions

File tree

backend/community_manager/services/bot_api.py

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import asyncio
22
import logging
3+
from datetime import datetime, timedelta, timezone
34
from typing import Any
45

56
from aiogram import Bot
@@ -49,20 +50,32 @@ async def _safe_request(self, func, *args, **kwargs) -> Any:
4950
logger.error(f"BotAPI Error: {e}", exc_info=True)
5051
raise e
5152

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:
5356
"""
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)
5563
"""
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))"
6067
)
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,
6478
)
65-
return banned and unbanned
6679

6780
async def unban_chat_member(self, chat_id: int | str, user_id: int) -> bool:
6881
"""

0 commit comments

Comments
 (0)