Skip to content

Commit

Permalink
fix(filters): ChatAdminRequired error (#282)
Browse files Browse the repository at this point in the history
Fixes #281

Add handling for `ChatAdminRequired` exception in `UserIsAdmin` filter.

* Import `ChatAdminRequired` from `hydrogram.errors`.
* Modify `UserIsAdmin.__call__` method to catch `ChatAdminRequired` exception.
* Add warning message in `UserIsAdmin.__call__` method when `ChatAdminRequired` exception is caught, informing the user that the bot needs to be an administrator to perform the action.
  • Loading branch information
HitaloM authored Nov 13, 2024
1 parent ccfd62f commit 4b08162
Showing 1 changed file with 13 additions and 3 deletions.
16 changes: 13 additions & 3 deletions src/korone/filters/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

from hydrogram import Client
from hydrogram.enums import ChatMemberStatus, ChatType
from hydrogram.errors import ChatAdminRequired
from hydrogram.filters import Filter
from hydrogram.types import CallbackQuery, ChatPrivileges, Message

Expand Down Expand Up @@ -34,9 +35,18 @@ async def __call__(self) -> bool:
if message.chat.type == ChatType.PRIVATE:
return True

chat_member = await message.chat.get_member(user.id)
if chat_member.status in {ChatMemberStatus.ADMINISTRATOR, ChatMemberStatus.OWNER}:
return True
try:
chat_member = await message.chat.get_member(user.id)
if chat_member.status in {ChatMemberStatus.ADMINISTRATOR, ChatMemberStatus.OWNER}:
return True
except ChatAdminRequired:
if self.show_alert:
alert_text = _("I need to be an administrator to perform that action!")
if is_callback:
await update.answer(text=alert_text, show_alert=True, cache_time=60)
else:
await message.reply(alert_text)
return False

if self.show_alert:
alert_text = _("You must be an administrator to use this.")
Expand Down

0 comments on commit 4b08162

Please sign in to comment.