]>
us-delaporte.pagekite.me Git - matrix-shop-bot.git/blob - matrix_shop_bot/bot.py
b1437efd885c7ee06cbb377f804e82da2a33b5a4
1 from datetime
import datetime
as dt
5 import simplematrixbotlib
as botlib
11 DATE_FORMAT
= '%Y-%m-%d'
14 return dt
.now().strftime(DATE_FORMAT
)
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
)
21 async def do_shop(match
):
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
46 if match
.command('setup'):
47 result
= Data().setup()
50 if match
.command('list-stores'):
51 items
= Data().get_stores()
52 results
= ['List of Stores']
54 results
.append(str(item
))
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}']
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}']
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
)
73 if match
.command('assign'):
74 item_id
= int(match
.args()[1])
75 Data().assign_last_store(item_id
)
77 # Now print the list for the assigned store
78 items
= Data().get_last_shopping_list()
79 results
= ['Shopping List']
81 results
.append(str(item
))
84 if match
.command('buy'):
85 item_id
= ' '.join(match
.args()[1:])
86 Data().purchase_item(item_id
)
89 if match
.command('clear'):
90 store_id
= ' '.join(match
.args()[1:])
91 Data().clear_list(store_id
)
92 self
.last_store_id
= None
95 if match
.command('clear-all'):
96 store_id
= ' '.join(match
.args()[1:])
97 Data().clear_list(store_id
, clear_all
=True)
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']
106 results
.append(str(item
))
109 if match
.command('list'):
110 items
= Data().get_last_shopping_list()
111 results
= ['Shopping List']
113 results
.append(str(item
))
116 if match
.command('unsorted'):
117 items
= Data().get_unsorted_list()
118 results
= ['Items assigned to no store']
120 results
.append(str(item
))
123 if match
.command('remove-item'):
124 item_id
= ' '.join(match
.args()[1:])
125 Data().remove_item(item_id
)
128 if match
.command('remove-store'):
129 store_id
= ' '.join(match
.args()[1:])
130 Data().remove_store(store_id
)
133 return [do_shop
.__doc
__]
135 async def shop_bot(room
, message
, bot
):
136 match
= botlib
.MessageMatch(room
, message
, bot
, PREFIX
)
138 if not match
.is_not_from_this_bot() and 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
))