]> us-delaporte.pagekite.me Git - matrix-shop-bot.git/blob - matrix_shop_bot/bot.py
Fixes for last store
[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 return [f'Item added with id {result}']
61
62 if match.command('add-store'):
63 new = ' '.join(match.args()[1:])
64 result = Data().add_store(new)
65 return [f'Store added with id {result}']
66
67 if match.command('assign-item-to-store'):
68 item_id = int(match.args()[1])
69 store_id = int(match.args()[2])
70 Data().assign_store(item_id, store_id)
71 return ['Success']
72
73 if match.command('assign'):
74 item_id = int(match.args()[1])
75 Data().assign_last_store(item_id)
76
77 # Now print the list for the assigned store
78 items = Data().get_last_shopping_list()
79 results = ['Shopping List']
80 for item in items:
81 results.append(str(item))
82 return results
83
84 if match.command('buy'):
85 item_id = ' '.join(match.args()[1:])
86 Data().purchase_item(item_id)
87 return ['Success']
88
89 if match.command('clear'):
90 store_id = ' '.join(match.args()[1:])
91 Data().clear_list(store_id)
92 self.last_store_id = None
93 return ['Success']
94
95 if match.command('clear-all'):
96 store_id = ' '.join(match.args()[1:])
97 Data().clear_list(store_id, clear_all=True)
98 return ['Success']
99
100
101 if match.command('list-for-store'):
102 store_id = ' '.join(match.args()[1])
103 items = Data().get_shopping_list_for_store(store_id)
104 results = ['Shopping List']
105 for item in items:
106 results.append(str(item))
107 return results
108
109 if match.command('list'):
110 items = Data().get_last_shopping_list()
111 results = ['Shopping List']
112 for item in items:
113 results.append(str(item))
114 return results
115
116 if match.command('unsorted'):
117 items = Data().get_unsorted_list()
118 results = ['Items assigned to no store']
119 for item in items:
120 results.append(str(item))
121 return results
122
123 if match.command('remove-item'):
124 item_id = ' '.join(match.args()[1:])
125 Data().remove_item(item_id)
126 return ['Success']
127
128 if match.command('remove-store'):
129 store_id = ' '.join(match.args()[1:])
130 Data().remove_store(store_id)
131 return ['Success']
132
133 return [do_shop.__doc__]
134
135 async def shop_bot(room, message, bot):
136 match = botlib.MessageMatch(room, message, bot, PREFIX)
137
138 if not match.is_not_from_this_bot() and match.prefix():
139 return
140
141 try:
142 if match.prefix():
143 result_lines = await do_shop(match)
144 for line in result_lines:
145 await bot.api.send_text_message(
146 room.room_id, str(line))
147 except Exception as ex:
148 for line in ['You have:', ' no tea', str(ex)]:
149 await bot.api.send_text_message(
150 room.room_id, str(line))
151
152
153