-
Notifications
You must be signed in to change notification settings - Fork 1
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
Bug fixes #18
Conversation
(permissions), small fixes, ref
WalkthroughThe 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
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? TipsChatThere are 3 ways to chat with CodeRabbit:
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)
Additionally, you can add CodeRabbit Configration File (
|
There was a problem hiding this 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
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 ofNone
is correctly implemented using Pydantic'sField
. 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'sField
.- 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 thestart
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 toAppConfig
withBotCommand
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 theplayers
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 fromDH
toHD
in therecords
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 thepolls
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.
src/handlers/game.py
Outdated
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 |
There was a problem hiding this comment.
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.
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 |
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}" | ||
) |
There was a problem hiding this comment.
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.
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}" | |
) |
There was a problem hiding this 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
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 theresults
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 theresults
variable enhances error reporting.src/data_models/Record.py (1)
21-21
: Correcting the typo in theshorten_role
function improves consistency and clarity in role processing.src/config.py (3)
6-6
: The addition of theBotCommand
import is necessary for defining bot commands and enhances functionality.
30-30
: Correcting the typo ingame_poll_outcomes
improves consistency and clarity in game outcome processing.
47-56
: The addition of thecommands
list within theAppConfig
class enhances the bot's configurability and usability.src/handlers/game.py (4)
8-8
: The addition of theGroupChatRequiredException
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 theusername
field nullable in theplayers
table improves the database schema's flexibility and inclusivity.
34-34
: Updating the possible values in theresult
field of thegames
table ensures consistency and clarity in the database schema.
46-46
: Correcting the typo and updating the comment in therole
field of therecords
table enhances the database schema's clarity.
60-60
: Making thecreator_username
field nullable in thepolls
table improves the database schema's flexibility and inclusivity.
allows_multiple_answers=False, | ||
disable_notification=True, | ||
) | ||
except Exception as e: |
There was a problem hiding this comment.
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.
except Exception as e: | |
import logging | |
... | |
except Exception as e: | |
logging.error(f"Failed to send poll: {e}") | |
await update.effective_message.reply_text( |
There was a problem hiding this 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
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 theusername
field optional with a default value ofNone
is a sensible change for flexibility.
22-25
: The implementation ofvalidate_username
using a regular expression for sanitization is a good practice.src/__main__.py (1)
19-20
: The implementation ofpost_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
: Usingtry_to_delete_message
for message deletion and includingsend_result
in thesave
function are good practices for error handling and effective communication.
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 |
There was a problem hiding this comment.
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.
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 |
Summary by CodeRabbit
commands
list within theAppConfig
class for enhanced bot functionality.creator_username
field in the poll data model.