Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix ChatAdminRequired error by sending warning messages #282

Merged
merged 1 commit into from
Nov 13, 2024

Conversation

HitaloM
Copy link
Owner

@HitaloM HitaloM commented Nov 13, 2024

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.

Summary by Sourcery

Bug Fixes:

  • Handle the ChatAdminRequired exception in the UserIsAdmin filter to prevent errors when the bot lacks administrative privileges.

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.
Copy link

sourcery-ai bot commented Nov 13, 2024

Reviewer's Guide by Sourcery

This PR enhances error handling in the UserIsAdmin filter by adding specific handling for ChatAdminRequired exceptions. The implementation wraps the admin check in a try-catch block and provides user-friendly warning messages when the bot lacks necessary admin privileges.

Sequence diagram for handling ChatAdminRequired exception

sequenceDiagram
    actor User
    participant Bot
    participant Chat
    participant Message
    participant Update

    User->>Bot: Send command
    Bot->>Chat: Get member status
    alt User is admin
        Chat-->>Bot: Return admin status
        Bot-->>User: Allow action
    else ChatAdminRequired exception
        Chat-->>Bot: Throw ChatAdminRequired
        alt show_alert is true
            Bot->>User: Show alert "I need to be an administrator to perform that action!"
        else
            Bot->>User: Reply "I need to be an administrator to perform that action!"
        end
    end
Loading

File-Level Changes

Change Details Files
Added exception handling for ChatAdminRequired error in admin privilege check
  • Wrapped admin status check in a try-catch block
  • Added handling for ChatAdminRequired exception
  • Implemented warning message display through both callback answers and direct replies
  • Added cache time of 60 seconds for callback warning messages
  • Return false when ChatAdminRequired exception is caught
src/korone/filters/admin.py

Assessment against linked issues

Issue Objective Addressed Explanation
#281 Handle ChatAdminRequired error when checking if a user is an admin
#281 Provide user feedback when the bot lacks required admin privileges

Possibly linked issues


Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time. You can also use
    this command to specify where the summary should be inserted.

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

Copy link

@sourcery-ai sourcery-ai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey @HitaloM - I've reviewed your changes - here's some feedback:

Overall Comments:

  • The ChatAdminRequired import is referenced in the description but not shown in the diff. Please ensure it's properly imported from hydrogram.errors.
Here's what I looked at during the review
  • 🟢 General issues: all looks good
  • 🟢 Security: all looks good
  • 🟢 Testing: all looks good
  • 🟢 Complexity: all looks good
  • 🟢 Documentation: all looks good

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

@HitaloM HitaloM added bug Something isn't working enhancement New feature or request and removed bug Something isn't working labels Nov 13, 2024
@HitaloM HitaloM merged commit 4b08162 into main Nov 13, 2024
2 checks passed
@HitaloM HitaloM deleted the fix-chat-admin-required branch November 13, 2024 15:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
1 participant