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

Bug fixes #18

Merged
merged 7 commits into from
Apr 4, 2024
Merged

Bug fixes #18

merged 7 commits into from
Apr 4, 2024

Conversation

Alex-Kopylov
Copy link
Owner

@Alex-Kopylov Alex-Kopylov commented Mar 24, 2024

Summary by CodeRabbit

  • New Features
    • Introduced a new commands list within the AppConfig class for enhanced bot functionality.
    • Added a validation method for cleaning usernames in player data models.
    • Included an optional creator_username field in the poll data model.
  • Bug Fixes
    • Corrected typos in strings related to Hitler's role in game data models for accurate representation.
    • Fixed a string comparison case for role names to ensure consistency.
  • Documentation
    • Updated README with detailed instructions and links for user interaction with the bot.
  • Refactor
    • Improved error handling and message deletion logic in bot handlers.
  • Chores
    • Enhanced configuration settings with additional parameters for startup functions.

Copy link
Contributor

coderabbitai bot commented Mar 24, 2024

Walkthrough

The recent updates across various files focus on refining string values, improving error handling, enhancing data models, and ensuring proper group chat interactions. These changes aim to enhance user experience, data integrity, and overall functionality of the Telegram bot.

Changes

Files Change Summary
src/config.py, src/data_models/Game.py, src/data_models/Record.py Corrected string values from "Canceler" to "Chancellor" for consistency.
src/data_models/Player.py Updated username field to be optional and added username cleaning validation method.
src/data_models/Poll.py, src/sql/init.sql Added optional creator_username field with nullable settings.
src/exceptions.py Introduced GroupChatRequiredException for non-group chat game initiation attempts.
src/handlers/game.py, src/handlers/save.py Improved error handling, message management, and added message deletion functionality.
src/handlers/start.py Enhanced user guidance and configuration settings handling in the start function.
src/utils.py Enhanced group chat check and added message deletion functionality with error handling.

🐇✨

In the world of code and bot,
Changes come, and flaws are fought.
With every update, we refine,
Making sure the stars align.
Through group chats and games we hop,
Onward, upward, without stop!
🌟🐰

Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

Share

Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai generate interesting stats about this repository and render them as a table.
    • @coderabbitai show all the console.log statements in this repository.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (invoked as PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger a review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai help to get help.

Additionally, you can add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.

CodeRabbit Configration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/coderabbit-overrides.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Review Status

Actionable comments generated: 3

Configuration used: CodeRabbit UI

Commits Files that changed from the base of the PR and between 449f339 and 124aadb.
Files selected for processing (12)
  • README.md (1 hunks)
  • src/main.py (2 hunks)
  • src/config.py (2 hunks)
  • src/data_models/Game.py (1 hunks)
  • src/data_models/Player.py (2 hunks)
  • src/data_models/Poll.py (2 hunks)
  • src/exceptions.py (1 hunks)
  • src/handlers/game.py (2 hunks)
  • src/handlers/save.py (3 hunks)
  • src/handlers/start.py (1 hunks)
  • src/sql/init.sql (3 hunks)
  • src/utils.py (1 hunks)
Files skipped from review due to trivial changes (1)
  • src/data_models/Game.py
Additional comments: 16
src/data_models/Poll.py (1)
  • 12-12: The addition of the creator_username field as an optional string with a default value of None is correctly implemented using Pydantic's Field. This change aligns with the PR's objective to adjust data models for better data handling and clarity.
src/exceptions.py (1)
  • 1-10: The implementation of GroupChatRequiredException is well-done, with a clear custom message and user ID attribute to enhance error handling in group chat contexts. This aligns with the PR's objective to improve error handling and group chat specific logic.
src/utils.py (1)
  • 11-11: The modification to is_message_from_group_chat to check for both "group" and "supergroup" chat types is correct and aligns with the PR's objective to enhance group chat functionality.
src/data_models/Player.py (2)
  • 8-8: Making the username field optional and nullable aligns with the PR's objective to adjust data models for better data handling. This change is correctly implemented using Pydantic's Field.
  • 20-23: The addition of validate_username to clean non-alphanumeric characters from the username is a good practice for data sanitization. However, ensure that this cleaning process aligns with the expected username format in your application context.
src/handlers/start.py (1)
  • 7-20: The addition of an AppConfig parameter to the start function and the expanded response message enhance user interaction and guidance, aligning with the PR's objectives. Ensure that all calls to this function are updated to include the new parameter.
src/__main__.py (1)
  • 19-21: The addition of the post_init function to set bot commands aligns with the PR's objective to streamline the bot's startup process. This is a good practice for initializing bot commands based on configuration.
README.md (1)
  • 15-15: Correcting the link for adding the bot to a group chat in the README.md file aligns with the PR's objective to enhance functionality and user experience. Ensure the link is tested and functional.
src/config.py (1)
  • 47-56: Adding a commands list to AppConfig with BotCommand instances for various bot commands is a good practice for organized and accessible command management, aligning with the PR's objectives.
src/handlers/game.py (2)
  • 19-23: The implementation for checking if a message is from a group chat before starting a game is well done. It ensures that the game functionality is restricted to the intended context, enhancing user experience and data integrity.
  • 63-67: The addition of message deletion at the end of the game function is a thoughtful touch, helping to keep the chat tidy. Good practice!
src/sql/init.sql (3)
  • 4-4: Allowing the username field in the players table to be NULL is a thoughtful adjustment, accommodating the reality that not all Telegram users have a username. This enhances data integrity and inclusivity.
  • 46-46: Updating the role field value from DH to HD in the records table likely improves consistency and clarity. Ensure that all related code and documentation reflect this change for uniformity across the application.
  • 60-60: Making the creator_username field in the polls table nullable is consistent with accommodating users without a Telegram username. This change aligns well with the principle of inclusivity in data modeling.
src/handlers/save.py (2)
  • 113-121: The adjustments to message deletion logic after saving game results are well thought out, contributing to a cleaner chat environment. Good integration with post-game tasks.
  • 65-67: The adjustment in the _pass_checks function to correctly reference the user attempting to stop a poll is a significant improvement. It enhances clarity and provides targeted feedback, improving the user experience.

Comment on lines 28 to 41
message = await context.bot.send_poll(
update.effective_chat.id,
f"@{update.effective_user.username} wants you to record the last game. Please choose your outcome:",
questions,
is_anonymous=False,
allows_multiple_answers=False,
disable_notification=True,
)
except Exception as e:
await update.effective_message.reply_text(
f"The bot need permission to create Telegram Polls to start a game\n"
f"Please grant those permissions in chat settings."
)
return
Copy link
Contributor

Choose a reason for hiding this comment

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

Consider logging the exception caught when sending a poll fails. This can aid in debugging and understanding the specific issues encountered during operation.

+ import logging
  ...
  except Exception as e:
+     logging.error(f"Failed to send poll: {e}")
      await update.effective_message.reply_text(

Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation.

Suggested change
message = await context.bot.send_poll(
update.effective_chat.id,
f"@{update.effective_user.username} wants you to record the last game. Please choose your outcome:",
questions,
is_anonymous=False,
allows_multiple_answers=False,
disable_notification=True,
)
except Exception as e:
await update.effective_message.reply_text(
f"The bot need permission to create Telegram Polls to start a game\n"
f"Please grant those permissions in chat settings."
)
return
import logging
message = await context.bot.send_poll(
update.effective_chat.id,
f"@{update.effective_user.username} wants you to record the last game. Please choose your outcome:",
questions,
is_anonymous=False,
allows_multiple_answers=False,
disable_notification=True,
)
except Exception as e:
logging.error(f"Failed to send poll: {e}")
await update.effective_message.reply_text(
f"The bot need permission to create Telegram Polls to start a game\n"
f"Please grant those permissions in chat settings."
)
return

Comment on lines +19 to +32
async def send_result(context, update, game: Game, records: list[Record]):
try:
await context.bot.send_photo(
chat_id=game.chat_id,
photo=await draw_result_image(
records=records, result=game.results, update=update, context=context
),
caption=f"The Game has been saved! Result: {game.results}",
disable_notification=True,
)
except Exception as e:
await update.effective_message.reply_text(
f"Result: {game.results}\nP.S. this bot can send you a result image, allow it to send photos. {e}"
)
Copy link
Contributor

Choose a reason for hiding this comment

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

The send_result function introduces a robust way to communicate game results. Consider logging the exception caught when sending a photo fails to aid in debugging and understanding the specific issues encountered.

+ import logging
  ...
  except Exception as e:
+     logging.error(f"Failed to send result photo: {e}")
      await update.effective_message.reply_text(

Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation.

Suggested change
async def send_result(context, update, game: Game, records: list[Record]):
try:
await context.bot.send_photo(
chat_id=game.chat_id,
photo=await draw_result_image(
records=records, result=game.results, update=update, context=context
),
caption=f"The Game has been saved! Result: {game.results}",
disable_notification=True,
)
except Exception as e:
await update.effective_message.reply_text(
f"Result: {game.results}\nP.S. this bot can send you a result image, allow it to send photos. {e}"
)
import logging
async def send_result(context, update, game: Game, records: list[Record]):
try:
await context.bot.send_photo(
chat_id=game.chat_id,
photo=await draw_result_image(
records=records, result=game.results, update=update, context=context
),
caption=f"The Game has been saved! Result: {game.results}",
disable_notification=True,
)
except Exception as e:
logging.error(f"Failed to send result photo: {e}")
await update.effective_message.reply_text(
f"Result: {game.results}\nP.S. this bot can send you a result image, allow it to send photos. {e}"
)

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 1

Review Status

Configuration used: CodeRabbit UI

Commits Files that changed from the base of the PR and between 124aadb and 6999fe3.
Files selected for processing (5)
  • src/config.py (3 hunks)
  • src/data_models/Game.py (3 hunks)
  • src/data_models/Record.py (1 hunks)
  • src/handlers/game.py (2 hunks)
  • src/sql/init.sql (4 hunks)
Additional comments not posted (15)
src/data_models/Game.py (3)

14-14: The correction in the comment for the results attribute improves clarity and accuracy.


23-23: Correcting the typo in the validation logic ensures consistency and clarity in the game's outcome processing.


40-40: Correcting the error message to accurately reference the results variable enhances error reporting.

src/data_models/Record.py (1)

21-21: Correcting the typo in the shorten_role function improves consistency and clarity in role processing.

src/config.py (3)

6-6: The addition of the BotCommand import is necessary for defining bot commands and enhances functionality.


30-30: Correcting the typo in game_poll_outcomes improves consistency and clarity in game outcome processing.


47-56: The addition of the commands list within the AppConfig class enhances the bot's configurability and usability.

src/handlers/game.py (4)

8-8: The addition of the GroupChatRequiredException import is necessary for ensuring the game starts only in appropriate contexts.


19-23: The logic to check if a message is from a group chat before starting the game enhances the bot's robustness and user experience.


28-40: The improved error handling when sending polls, including a specific error message for permission issues, makes the bot more user-friendly.


63-67: The addition of a message deletion function at the end of the game function improves the bot's cleanliness and user experience.

src/sql/init.sql (4)

4-4: Making the username field nullable in the players table improves the database schema's flexibility and inclusivity.


34-34: Updating the possible values in the result field of the games table ensures consistency and clarity in the database schema.


46-46: Correcting the typo and updating the comment in the role field of the records table enhances the database schema's clarity.


60-60: Making the creator_username field nullable in the polls table improves the database schema's flexibility and inclusivity.

allows_multiple_answers=False,
disable_notification=True,
)
except Exception as e:
Copy link
Contributor

Choose a reason for hiding this comment

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

Consider logging the exception caught when sending a poll fails. This can aid in debugging and understanding the specific issues encountered during operation.

+ import logging
  ...
  except Exception as e:
+     logging.error(f"Failed to send poll: {e}")
      await update.effective_message.reply_text(

Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation.

Suggested change
except Exception as e:
import logging
...
except Exception as e:
logging.error(f"Failed to send poll: {e}")
await update.effective_message.reply_text(

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 1

Review Status

Configuration used: CodeRabbit UI

Commits Files that changed from the base of the PR and between 6999fe3 and 07ad964.
Files selected for processing (4)
  • src/main.py (2 hunks)
  • src/data_models/Player.py (2 hunks)
  • src/handlers/save.py (3 hunks)
  • src/utils.py (1 hunks)
Additional comments not posted (6)
src/utils.py (1)

11-11: LGTM! The logic for checking if a message is from a group chat is correctly implemented.

src/data_models/Player.py (2)

8-8: Making the username field optional with a default value of None is a sensible change for flexibility.


22-25: The implementation of validate_username using a regular expression for sanitization is a good practice.

src/__main__.py (1)

19-20: The implementation of post_init for setting bot commands dynamically is a good practice.

src/handlers/save.py (2)

65-69: Correctly referencing the user attempting to stop a poll in _pass_checks enhances game integrity.


117-125: Using try_to_delete_message for message deletion and including send_result in the save function are good practices for error handling and effective communication.

Comment on lines +14 to +18
async def try_to_delete_message(context, chat_id, message_id):
try:
await context.bot.delete_message(chat_id=chat_id, message_id=message_id)
except Exception as e:
return
Copy link
Contributor

Choose a reason for hiding this comment

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

Consider logging exceptions in try_to_delete_message to aid in debugging.

  except Exception as e:
+     logging.error(f"Failed to delete message: {e}")
      return

Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation.

Suggested change
async def try_to_delete_message(context, chat_id, message_id):
try:
await context.bot.delete_message(chat_id=chat_id, message_id=message_id)
except Exception as e:
return
async def try_to_delete_message(context, chat_id, message_id):
try:
await context.bot.delete_message(chat_id=chat_id, message_id=message_id)
except Exception as e:
logging.error(f"Failed to delete message: {e}")
return

@Alex-Kopylov Alex-Kopylov merged commit 1975e5e into main Apr 4, 2024
4 checks passed
@Alex-Kopylov Alex-Kopylov deleted the develop branch April 6, 2024 14:14
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