summaryrefslogtreecommitdiffstats
path: root/restaurants.py
blob: 8e57b334d679a88ffbb5e03654ef7bfb88a5f62e (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
import os
import db

CATEGORY = "restaurant"
OLD_DB_PATH = os.path.join(os.path.dirname(__file__), "restaurants.db")


def init_db():
    db.init_db()
    db.migrate_from(OLD_DB_PATH, CATEGORY)
    db.seed(CATEGORY)


def _format(row) -> str:
    return f"{row['name']}\n- {row['note']}\nAdded by {row['added_by']}"


HELP_TEXT = (
    "🍽️ Restaurant bot commands:\n"
    "  !addrestaurant <name> | <note>  — add a new restaurant (names must be unique)\n"
    "  !removerestaurant <name>        — remove a restaurant by name\n"
    "  !findrestaurant [keyword]       — get a random restaurant (optionally filtered by keyword in note)"
)


async def handle_restaurant_command(bot_api, room_id: str, sender: str, body: str, self_name: str):
    """
    Returns True if the message was handled as a restaurant command, False otherwise.
    Call this from your on_message_event listener.
    """
    stripped = body.strip()
    lower = stripped.lower()

    if ("where to eat"  in lower or "what to eat" in lower) and not lower.startswith("!"):
        pick = db.find_item(CATEGORY)
        reply = f"🎲 Random pick:\n\n{_format(pick)}" if pick else "No restaurants in the list yet. Use !addrestaurant to add one!"
        await bot_api.send_text_message(room_id, reply)
        return True

    if not lower.startswith("!"):
        return False

    # !addrestaurant <name> | <note>
    if lower.startswith("!addrestaurant"):
        rest = stripped[len("!addrestaurant"):].strip()
        if not rest:
            await bot_api.send_text_message(
                room_id,
                "Usage: !addrestaurant <name> | <note>\nExample: !addrestaurant Sushi Place | great omakase | 📍 Downtown",
            )
            return True
        if "|" in rest:
            name, _, note = rest.partition("|")
        else:
            name = rest
            note = ""
        added_by = sender.lstrip("@").split(":")[0]
        error = db.add_item(CATEGORY, name, added_by, note=note)
        reply = error if error else f"✅ Added **{name.strip()}** to the restaurant list!"
        await bot_api.send_text_message(room_id, reply)
        return True

    # !removerestaurant <name>
    if lower.startswith("!removerestaurant"):
        name = stripped[len("!removerestaurant"):].strip()
        if not name:
            await bot_api.send_text_message(
                room_id,
                "Usage: !removerestaurant <name>\nExample: !removerestaurant MCDONALDS",
            )
            return True
        result = db.remove_item(CATEGORY, name)
        if result is None:
            reply = f"❌ No restaurant named **{name}** found. Names are case-insensitive."
        else:
            reply = f"🗑️ Removed **{result}** from the restaurant list."
        await bot_api.send_text_message(room_id, reply)
        return True

    # !findrestaurant [keyword]
    if lower.startswith("!findrestaurant"):
        keyword = stripped[len("!findrestaurant"):].strip()
        pick = db.find_item(CATEGORY, keyword)
        if pick is None:
            reply = f"🔍 No restaurants found with **{keyword}** in their notes." if keyword else "No restaurants in the list yet. Use !addrestaurant to add one!"
        else:
            label = f'🎲 Random pick (keyword: "{keyword}"):' if keyword else "🎲 Random pick:"
            reply = f"{label}\n\n{_format(pick)}"
        await bot_api.send_text_message(room_id, reply)
        return True

    # !restaurants (help alias)
    if lower.startswith("!restaurants"):
        await bot_api.send_text_message(room_id, HELP_TEXT)
        return True

    return False
send patches to the email below
yukais@pinapelz.com
include the subject [PATCH repo_name]
pinapelz.com
homepage