summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorPinapelz <yukais@pinapelz.com>2026-03-18 19:08:15 -0700
committerPinapelz <yukais@pinapelz.com>2026-03-18 19:08:15 -0700
commit44face929d3e1e4a964dd4f948acf34cfd5e3c55 (patch)
tree3acf0f1d4c25d41a7e246d109a3f641a3536e517
parent9a4561069ea3a794f1d66d0b2ff04d8d1ff20411 (diff)
feat: view game by idx rng
-rw-r--r--db.py19
-rw-r--r--games.py14
2 files changed, 31 insertions, 2 deletions
diff --git a/db.py b/db.py
index 5116708..8e01b82 100644
--- a/db.py
+++ b/db.py
@@ -119,6 +119,25 @@ def remove_item(category: str, name: str) -> str | None:
return actual_name
+def count_items(category: str) -> int:
+ with _get_conn() as conn:
+ row = conn.execute(
+ "SELECT COUNT(*) AS cnt FROM items WHERE category = ?", (category,)
+ ).fetchone()
+ return row["cnt"]
+
+
+def get_item_by_index(category: str, index: int) -> sqlite3.Row | None:
+ with _get_conn() as conn:
+ rows = conn.execute(
+ "SELECT name, note, added_by FROM items WHERE category = ? ORDER BY id ASC",
+ (category,),
+ ).fetchall()
+ if not rows or index < 1 or index > len(rows):
+ return None
+ return rows[index - 1]
+
+
def find_item(category: str, keyword: str = "") -> sqlite3.Row | None:
"""Return a random item from the category, optionally filtered by keyword in note."""
keyword = keyword.strip().lower()
diff --git a/games.py b/games.py
index 532da5d..d1b666b 100644
--- a/games.py
+++ b/games.py
@@ -27,8 +27,18 @@ async def handle_game_command(bot_api, room_id: str, sender: str, body: str, sel
# Passive trigger: any message containing "what to play"
if "what to play" in lower and not lower.startswith("!"):
- pick = db.find_item(CATEGORY)
- reply = f"Fresh from the list of considerations:\n\n{_format(pick)}" if pick else "No games in the list yet. Use !addgame to add one!"
+ after = lower.split("what to play", 1)[1].strip()
+ if after.isdigit():
+ index = int(after)
+ total = db.count_items(CATEGORY)
+ pick = db.get_item_by_index(CATEGORY, index)
+ if pick is None:
+ reply = f"❌ Index {index} is out of range. Valid range: 1–{total}."
+ else:
+ reply = f"#{index} from the list of considerations:\n\n{_format(pick)}"
+ else:
+ pick = db.find_item(CATEGORY)
+ reply = f"Fresh from the list of considerations:\n\n{_format(pick)}" if pick else "No games in the list yet. Use !addgame to add one!"
await bot_api.send_text_message(room_id, reply)
return True
send patches to the email below
yukais@pinapelz.com
include the subject [PATCH repo_name]
pinapelz.com
homepage