]> us-delaporte.pagekite.me Git - matrix-shop-bot.git/blob - matrix_shop_bot/bot.py
ae65bdc03494b7f6166c79dc40ff607c653dcd47
[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 extra.help - show extended help
31 add - add an item to shop for
32 add.store - add a store
33 stores - list all stores
34 at STORE_ID - (!shop at STORE_ID) list all items assigned to store and use this store for future commands
35 assign ITEM_ID - assign item to last used store id
36 buy - mark item as purchased
37 clear STORE_ID - clear all purchased items for STORE_ID from database
38 list - re-show list from last used store
39 '''
40 extended_help = '''
41 assign.item.to.store ITEM_ID STORE_ID - plan to shop for at store for item
42 clear.all STORE_ID - clear **all** items for STORE_ID from database
43 remove.item ITEM_ID - remove a shopping item from the datatbase
44 remove.store STORE_ID - remove a store from the datatbase (requires clearing the list first)
45 setup - run one time database setup
46 unsorted - list all items not assigned to any store
47
48 '''
49
50 if match.command('setup'):
51 result = Data().setup()
52 return [result]
53
54 if match.command('stores'):
55 items = Data().get_stores()
56 results = ['List of Stores']
57 for item in items:
58 results.append(str(item))
59 return results
60
61 if match.command('add'):
62 new = ' '.join(match.args()[1:])
63 result = Data().add_item(new)
64 results = [f'Item added with id {result}']
65 for item in Data().get_last_shopping_list():
66 results.append(str(item))
67 return results
68
69 if match.command('add.store'):
70 new = ' '.join(match.args()[1:])
71 result = Data().add_store(new)
72 return [f'Store added with id {result}']
73
74 if match.command('assign.item.to.store'):
75 item_id = int(match.args()[1])
76 store_id = int(match.args()[2])
77 Data().assign_store(item_id, store_id)
78 return ['Success']
79
80 if match.command('assign'):
81 item_id = int(match.args()[1])
82 Data().assign_last_store(item_id)
83
84 # Now print the list for the assigned store
85 items = Data().get_last_shopping_list()
86 results = ['Shopping List']
87 for item in items:
88 results.append(str(item))
89 return results
90
91 if match.command('buy'):
92 items = ' '.join(match.args()[1:])
93 results = Data().purchase_items(items)
94 # results = []
95 # for item in Data().get_last_shopping_list():
96 # results.append(str(item))
97 return results
98
99 if match.command('clear'):
100 store_name = ' '.join(match.args()[1:])
101 results = Data().clear_list(store_name)
102 return results
103
104 if match.command('clear.all'):
105 store_name = ' '.join(match.args()[1:])
106 results = Data().clear_list(store_name, clear_all=True)
107 return results
108
109 if match.command('at'):
110 store_name = ' '.join(match.args()[1:])
111 items = Data().get_shopping_list(store_name)
112 results = ['Shopping List']
113 for item in items:
114 results.append(str(item))
115 return results
116
117 if match.command('list'):
118 items = Data().get_last_shopping_list()
119 results = ['Shopping List']
120 for item in items:
121 results.append(str(item))
122 return results
123
124
125 if match.command('past'):
126 store_name = ' '.join(match.args()[1:])
127 items = Data().list_purchases(store_name)
128 results = ['Past purchases at {store_name}']
129 for item in items:
130 results.append(str(item))
131 return results
132
133
134 if match.command('unsorted'):
135 items = Data().get_unsorted_list()
136 results = ['Items assigned to no store']
137 for item in items:
138 results.append(str(item))
139 return results
140
141 if match.command('remove.item'):
142 item_id = ' '.join(match.args()[1:])
143 Data().remove_item(item_id)
144 return ['Success']
145
146 if match.command('remove.store'):
147 store_id = ' '.join(match.args()[1:])
148 Data().remove_store(store_id)
149 return ['Success']
150
151
152 if match.command('extra.help'):
153 return [extended_help]
154
155 return [do_shop.__doc__]
156
157 async def shop_bot(room, message, bot):
158 match = botlib.MessageMatch(room, message, bot, PREFIX)
159
160 if not match.is_not_from_this_bot() and match.prefix():
161 return
162
163 try:
164 if match.prefix():
165 result_lines = await do_shop(match)
166 for line in result_lines:
167 await bot.api.send_text_message(
168 room.room_id, str(line))
169 except Exception as ex:
170 for line in ['You have:', ' no tea', str(ex)]:
171 await bot.api.send_text_message(
172 room.room_id, str(line))
173
174
175