Skip to content

Conversation

yasirarism
Copy link
Owner

@yasirarism yasirarism commented Jan 29, 2025

Summary by Sourcery

Update the PyPi search command to fetch data directly from the PyPi API, display additional package information, and present the results in a more user-friendly format. Improve the warn command to handle cases where the bot lacks ban permissions. Update the chatbot prompts to instruct the AI to use the user's specified language. Add error handlers for common network-related exceptions. Refine the Google search function to enhance snippet extraction. Update the broadcast messages function to handle FloodWait exceptions more effectively.

New Features:

  • Display additional package information in PyPi search results, such as requirements, author details, and project URLs.
  • Present PyPi search results in a more user-friendly format.

Enhancements:

  • Improve error handling for FloodWait, RPCError, and SlowmodeWait exceptions.

Saatnya back fix bugs

Signed-off-by: Yasir Aris M <git@yasir.id>
Signed-off-by: Yasir Aris M <git@yasir.id>
Signed-off-by: Yasir Aris M <git@yasir.id>
Signed-off-by: Yasir Aris M <git@yasir.id>
Signed-off-by: Yasir Aris M <git@yasir.id>
Signed-off-by: Yasir Aris M <git@yasir.id>
Signed-off-by: Yasir Aris M <git@yasir.id>
Signed-off-by: Yasir Aris M <git@yasir.id>
Signed-off-by: Yasir Aris M <git@yasir.id>
Signed-off-by: Yasir Aris M <git@yasir.id>
Signed-off-by: Yasir Aris M <git@yasir.id>
Signed-off-by: Yasir Aris M <git@yasir.id>
Signed-off-by: Yasir Aris M <git@yasir.id>
Will add another in another time

Signed-off-by: Yasir Aris M <git@yasir.id>
Signed-off-by: Yasir Aris M <git@yasir.id>
Signed-off-by: Yasir Aris M <git@yasir.id>
@sourcery-ai
Copy link
Contributor

sourcery-ai bot commented Jan 29, 2025

Reviewer's Guide by Sourcery

This pull request includes several bug fixes and enhancements across different modules. The changes include improvements to the pypi search functionality, fixes to the warn command, updates to the chatbot AI, and error handling improvements.

Sequence diagram for updated PyPI search flow

sequenceDiagram
    actor User
    participant Bot
    participant PyPI
    participant Telegraph

    User->>Bot: /pypi command
    Bot->>PyPI: GET /pypi/{package}/json
    alt Success (200)
        PyPI-->>Bot: Package data
        alt Response too long
            Bot->>Telegraph: Post package details
            Telegraph-->>Bot: Telegraph URL
            Bot-->>User: Telegraph URL with package info
        else Normal response
            Bot-->>User: Formatted package info
        end
    else Failure
        Bot-->>User: Connection error message
    end
Loading

Sequence diagram for enhanced warn system

sequenceDiagram
    actor Admin
    participant Bot
    participant Database

    Admin->>Bot: Warn command
    Bot->>Database: Get current warns
    Database-->>Bot: Warns count
    alt Warns >= 2
        Bot->>Bot: Attempt to ban user
        alt Has ban permission
            Bot-->>Admin: Ban success message
            Bot->>Database: Remove warns
        else No ban permission
            Bot-->>Admin: No permission message
        end
    else Warns < 2
        Bot->>Database: Add warn
        Bot-->>Admin: Warning message
    end
Loading

Class diagram for error handling updates

classDiagram
    class ErrorHandler {
        +handle_error(client, update, error)
    }
    class FloodWait {
        +value: int
    }
    class SlowmodeWait {
        +value: int
    }
    class RPCError

    ErrorHandler ..> FloodWait
    ErrorHandler ..> SlowmodeWait
    ErrorHandler ..> RPCError

    note for ErrorHandler "New centralized error handling for flood and slowmode waits"
Loading

File-Level Changes

Change Details Files
Refactor pypi search to directly fetch package information from pypi API.
  • Removed pagination logic.
  • Directly fetch package information from pypi API.
  • Added detailed package information to the message.
  • Added a try-except block to handle MessageTooLong errors by posting the result to telegraph.
misskaty/plugins/pypi_search.py
Fix warn command to handle ban permission errors.
  • Added a try-except block to handle ChatAdminRequired errors when banning a user.
  • Changed reply_text to reply_msg to fix reply issues.
misskaty/plugins/admin.py
Update chatbot AI to include system prompt and fix openai key usage.
  • Added a system prompt to the gemini chatbot to define the AI's character.
  • Added a system prompt to the openai chatbot to define the AI's character.
  • Fixed openai key usage to use the correct key for the openai chatbot.
  • Added a new handler for bot business messages for the ask command.
misskaty/plugins/chatbot_ai.py
Add error handling for FloodWait, RPCError, and SlowmodeWait.
  • Added an error handler for FloodWait, RPCError, and SlowmodeWait.
  • Added a sleep for FloodWait and SlowmodeWait errors.
misskaty/plugins/dev.py
Fix google search snippet selector.
  • Fixed google search snippet selector to use the correct class name.
misskaty/plugins/misc_tools.py
Fix floodwait sleep.
  • Fixed floodwait sleep to use the correct value.
utils.py

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. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the 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 exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!
  • Generate a plan of action for an issue: Comment @sourcery-ai plan on
    an issue to generate a plan of action for it.

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
Contributor

@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 @yasirarism - I've reviewed your changes and found some issues that need to be addressed.

Blocking issues:

  • Hardcoded OpenAI API key found. (link)

Overall Comments:

  • Please split these changes into separate PRs - one for error handling improvements, one for the PyPI search rewrite, and one for the chatbot modifications. This will make the changes easier to review and maintain.
  • Add a proper description explaining what issues these changes are fixing and any potential breaking changes, especially for the PyPI search rewrite.
Here's what I looked at during the review
  • 🟡 General issues: 2 issues found
  • 🔴 Security: 1 blocking issue
  • 🟢 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.

html = await fetch.get(f"https://pypi.org/pypi/{kueri}/json")
if html.status_code != 200:
return await pesan.edit_msg("Failed connect fo pypi server")
res = html.json()
Copy link
Contributor

Choose a reason for hiding this comment

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

suggestion (bug_risk): Consider adding error handling for JSON parsing

The JSON parsing could fail if the response is malformed. Consider wrapping this in a try-catch block to handle potential JSONDecodeError exceptions.

Suggested implementation:

    html = await fetch.get(f"https://pypi.org/pypi/{kueri}/json")
    if html.status_code != 200:
        return await pesan.edit_msg("Failed connect fo pypi server")
    try:
        res = html.json()
    except json.JSONDecodeError:
        return await pesan.edit_msg("Failed to parse PyPI response - invalid JSON received")
    requirement = (

You'll need to add the following import at the top of the file if it's not already present:

import json

await message.chat.ban_member(user_id)
await message.reply_text(strings("exceed_warn_msg").format(mention=mention))
await remove_warns(chat_id, await int_to_alpha(user_id))
try:
Copy link
Contributor

Choose a reason for hiding this comment

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

suggestion: Consider handling additional ban-related exceptions

While ChatAdminRequired is handled, consider also catching other potential exceptions like UserAdminInvalid or other permission-related errors that could occur during member banning.

Suggested implementation:

        except ChatAdminRequired:
            await message.reply_msg(strings("no_ban_permission"))
        except UserAdminInvalid:
            await message.reply_msg("I can't ban admin users!")
        except (ChatError, UserBannedInChannel) as e:
            await message.reply_msg(f"Failed to ban user: {str(e)}")

Note: If the import statement for pyrogram.errors isn't visible in the provided code snippet, you'll need to add or modify it at the top of the file. Also, you may want to add these error messages to your strings system rather than having them hardcoded.

else:
gptai_conversations[uid].append({"role": "user", "content": pertanyaan})
ai_response = await get_openai_stream_response(True, OPENAI_KEY, "https://models.inference.ai.azure.com" if uid == OWNER_ID else "https://duckai.yasirapi.eu.org/v1", "gpt-4o" if uid == OWNER_ID else "gpt-4o-mini", gptai_conversations[uid], msg, strings)
ai_response = await get_openai_stream_response(True, OPENAI_KEY, "https://models.inference.ai.azure.com", "gpt-4o-mini", gptai_conversations[uid], msg, strings)
Copy link
Contributor

Choose a reason for hiding this comment

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

🚨 issue (security): Hardcoded OpenAI API key found.

The OPENAI_KEY variable is hardcoded in the openai_chatbot function. This is a security risk and should be stored securely, such as in environment variables or a secrets management service.

@yasirarism yasirarism merged commit aeb6d41 into master Jan 29, 2025
6 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant