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

saving games to db, lesser msgs, timeout for db #8

Merged
merged 1 commit into from
Feb 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 7 additions & 5 deletions src/db.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,16 @@

async def get_db() -> aiosqlite.Connection:
if not getattr(get_db, "db", None):
db = await aiosqlite.connect(config.SQLITE_DB_FILE_PATH)
db = await aiosqlite.connect(config.SQLITE_DB_FILE_PATH,
timeout=60 * 60 * 24 * 1 # 1 day
)
get_db.db = db

return get_db.db


async def fetch_all(
sql: LiteralString, params: Iterable[Any] | None = None
sql: LiteralString, params: Iterable[Any] | None = None
) -> list[dict]:
cursor = await _get_cursor(sql, params)
rows = await cursor.fetchall()
Expand All @@ -24,7 +26,7 @@ async def fetch_all(


async def fetch_one(
sql: LiteralString, params: Iterable[Any] | None = None
sql: LiteralString, params: Iterable[Any] | None = None
) -> dict | None:
cursor = await _get_cursor(sql, params)
row = await cursor.fetchone()
Expand All @@ -34,7 +36,7 @@ async def fetch_one(


async def execute(
sql: LiteralString, params: Iterable[Any] | None = None, *, autocommit: bool = True
sql: LiteralString, params: Iterable[Any] | None = None, *, autocommit: bool = True
) -> None:
db = await get_db()
await db.execute(sql, params)
Expand All @@ -51,7 +53,7 @@ async def _async_close_db() -> None:


async def _get_cursor(
sql: LiteralString, params: Iterable[Any] | None
sql: LiteralString, params: Iterable[Any] | None
) -> aiosqlite.Cursor:
db = await get_db()
db.row_factory = aiosqlite.Row
Expand Down
3 changes: 3 additions & 0 deletions src/handlers/game.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,6 @@ async def game(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
)
)
context.bot_data.update(game_metadata)

# Delete command message
await update.effective_message.delete()
Comment on lines +42 to +43
Copy link
Contributor

Choose a reason for hiding this comment

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

Consider adding error handling around the message deletion to gracefully handle potential permission issues. This ensures the bot's stability and provides a better user experience in cases where it might not have the necessary permissions to delete messages.

- await update.effective_message.delete()
+ try:
+     await update.effective_message.delete()
+ except Exception as e:
+     # Log the error or notify the bot owner
+     pass

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
# Delete command message
await update.effective_message.delete()
try:
await update.effective_message.delete()
except Exception as e:
# Log the error or notify the bot owner
pass

7 changes: 7 additions & 0 deletions src/handlers/save.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,13 @@ async def save(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
await update.effective_message.reply_text(
"The Game has been saved!. Results: {}".format(game.results)
)
# Delete the poll
await context.bot.delete_message(
chat_id=game.chat_id,
message_id=game.poll_id
)
# Delete this callback /save message
await update.effective_message.delete()
Comment on lines +98 to +104
Copy link
Contributor

Choose a reason for hiding this comment

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

Consider adding error handling around the message deletion operations to gracefully handle potential permission issues. This ensures the bot's stability and provides a better user experience in cases where it might not have the necessary permissions to delete messages.

- await context.bot.delete_message(chat_id=game.chat_id, message_id=game.poll_id)
+ try:
+     await context.bot.delete_message(chat_id=game.chat_id, message_id=game.poll_id)
+ except Exception as e:
+     # Log the error or notify the bot owner
+     pass

- await update.effective_message.delete()
+ try:
+     await update.effective_message.delete()
+ except Exception as e:
+     # Log the error or notify the bot owner
+     pass

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
# Delete the poll
await context.bot.delete_message(
chat_id=game.chat_id,
message_id=game.poll_id
)
# Delete this callback /save message
await update.effective_message.delete()
# Delete the poll
try:
await context.bot.delete_message(chat_id=game.chat_id, message_id=game.poll_id)
except Exception as e:
# Log the error or notify the bot owner
pass
# Delete this callback /save message
try:
await update.effective_message.delete()
except Exception as e:
# Log the error or notify the bot owner
pass

else:
await update.effective_message.reply_text(
"Something went wrong. Can't process your request."
Expand Down