Skip to content

Commit

Permalink
saving games to db, lesser msgs, timeout for db (#8)
Browse files Browse the repository at this point in the history
  • Loading branch information
Alex-Kopylov authored Feb 22, 2024
1 parent b57d6ec commit bfb6307
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 5 deletions.
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()
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()
else:
await update.effective_message.reply_text(
"Something went wrong. Can't process your request."
Expand Down

0 comments on commit bfb6307

Please sign in to comment.