]> apps.delaporte.us Git - matrix-shop-bot.git/blob - matrix_shop_bot/bot.py
Print latest list after each add
[matrix-shop-bot.git] / matrix_shop_bot / bot.py
1 from datetime import datetime as dt
2 import logging
3 import os
4
5 import simplematrixbotlib as botlib
6
7 from .db import Data
8
9 MODULE='shop'
10
11 DATE_FORMAT = '%Y-%m-%d'
12
13 def today():
14 return dt.now().strftime(DATE_FORMAT)
15
16 PREFIX = f'!{MODULE}'
17 logger = logging.getLogger(__name__)
18 logging.basicConfig(filename=f'{MODULE}.log', encoding='utf-8', level=logging.DEBUG)
19 logging.getLogger('nio').setLevel(logging.WARNING)
20
21 async def do_shop(match):
22 '''
23 Prefix: !shop
24
25 Synax:
26 !shop COMMAND ARGS
27
28 Commands:
29 help - show this help
30 add - add an item to shop for
31 add-store - add a store
32 assign-item-to-store ITEM_ID STORE_ID - plan to shop for at store for item
33 assign ITEM_ID - assign item to last used store id
34 buy - mark item as purchased
35 clear STORE_ID - clear all purchased items for STORE_ID from database
36 clear-all STORE_ID - clear **all** items for STORE_ID from database
37 list - re-show list from last used store
38 list-for-store STORE_ID - list all items assigned to store
39 list-stores - list all stores
40 unsorted - list all items not assigned to any store
41 remove-item ITEM_ID - remove a shopping item from the datatbase
42 remove-store STORE_ID - remove a store from the datatbase (requires clearing the list first)
43 setup - run one time database setup
44 '''
45
46 if match.command('setup'):
47 result = Data().setup()
48 return [result]
49
50 if match.command('list-stores'):
51 items = Data().get_stores()
52 results = ['List of Stores']
53 for item in items:
54 results.append(str(item))
55 return results
56
57 if match.command('add'):
58 new = ' '.join(match.args()[1:])
59 result = Data().add_item(new)
60 results = [f'Item added with id {result}']
61 for item in Data().get_last_shopping_list():
62 results.append(str(item))
63 return results
64
65 if match.command('add-store'):
66 new = ' '.join(match.args()[1:])
67 result = Data().add_store(new)
68 return [f'Store added with id {result}']
69
70 if match.command('assign-item-to-store'):
71 item_id = int(match.args()[1])
72 store_id = int(match.args()[2])
73 Data().assign_store(item_id, store_id)
74 return ['Success']
75
76 if match.command('assign'):
77 item_id = int(match.args()[1])
78 Data().assign_last_store(item_id)
79
80 # Now print the list for the assigned store
81 items = Data().get_last_shopping_list()
82 results = ['Shopping List']
83 for item in items:
84 results.append(str(item))
85 return results
86
87 if match.command('buy'):
88 item_id = ' '.join(match.args()[1:])
89 Data().purchase_item(item_id)
90 return ['Success']
91
92 if match.command('clear'):
93 store_id = ' '.join(match.args()[1:])
94 Data().clear_list(store_id)
95 self.last_store_id = None
96 return ['Success']
97
98 if match.command('clear-all'):
99 store_id = ' '.join(match.args()[1:])
100 Data().clear_list(store_id, clear_all=True)
101 return ['Success']
102
103
104 if match.command('list-for-store'):
105 store_id = ' '.join(match.args()[1])
106 items = Data().get_shopping_list_for_store(store_id)
107 results = ['Shopping List']
108 for item in items:
109 results.append(str(item))
110 return results
111
112 if match.command('list'):
113 items = Data().get_last_shopping_list()
114 results = ['Shopping List']
115 for item in items:
116 results.append(str(item))
117 return results
118
119 if match.command('unsorted'):
120 items = Data().get_unsorted_list()
121 results = ['Items assigned to no store']
122 for item in items:
123 results.append(str(item))
124 return results
125
126 if match.command('remove-item'):
127 item_id = ' '.join(match.args()[1:])
128 Data().remove_item(item_id)
129 return ['Success']
130
131 if match.command('remove-store'):
132 store_id = ' '.join(match.args()[1:])
133 Data().remove_store(store_id)
134 return ['Success']
135
136 return [do_shop.__doc__]
137
138 async def shop_bot(room, message, bot):
139 match = botlib.MessageMatch(room, message, bot, PREFIX)
140
141 if not match.is_not_from_this_bot() and match.prefix():
142 return
143
144 try:
145 if match.prefix():
146 result_lines = await do_shop(match)
147 for line in result_lines:
148 await bot.api.send_text_message(
149 room.room_id, str(line))
150 except Exception as ex:
151 for line in ['You have:', ' no tea', str(ex)]:
152 await bot.api.send_text_message(
153 room.room_id, str(line))
154
155
156