]>
apps.delaporte.us Git - matrix-shop-bot.git/blob - matrix_shop_bot/bot.py
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_ID STORE_ID - plan to shop for at store for item
33 buy - mark item as purchased
34 clear STORE_ID - clear all purchased items for STORE_ID from database
35 clear-all STORE_ID - clear **all** items for STORE_ID from database
36 list STORE_ID - list all items assigned to store
37 list-stores - list all stores
38 unsorted - list all items not assigned to any store
39 remove-item ITEM_ID - remove a shopping item from the datatbase
40 remove-store STORE_ID - remove a store from the datatbase (requires clearing the list first)
41 setup - run one time database setup
44 if match
.command('setup'):
45 result
= Data().setup()
48 if match
.command('list-stores'):
49 items
= Data().get_stores()
50 results
= ['List of Stores']
52 results
.append(str(item
))
55 if match
.command('add'):
56 new
= ' '.join(match
.args()[1:])
57 result
= Data().add_item(new
)
58 return [f
'Item added with id {result}']
60 if match
.command('add-store'):
61 new
= ' '.join(match
.args()[1:])
62 result
= Data().add_store(new
)
63 return [f
'Store added with id {result}']
65 if match
.command('assign'):
66 item_id
= int(match
.args()[1])
67 store_id
= int(match
.args()[2])
69 if not isinstance(store_id
, int):
72 Data().assign_store(item_id
, store_id
)
74 # Now print the list for the assigned store
75 items
= Data().get_shopping_list(store_id
)
76 results
= ['Shopping List']
78 results
.append(str(item
))
81 if match
.command('buy'):
82 item_id
= ' '.join(match
.args()[1:])
83 Data().purchase_item(item_id
)
86 if match
.command('clear'):
87 store_id
= ' '.join(match
.args()[1:])
88 Data().clear_list(store_id
)
89 self
.last_store_id
= None
92 if match
.command('clear-all'):
93 store_id
= ' '.join(match
.args()[1:])
94 Data().clear_list(store_id
, clear_all
=True)
97 if match
.command('list'):
98 store_id_str
= ' '.join(match
.args()[1])
100 store_id
= int(store_id_str
) if store_id_str
else None
101 items
= Data().get_shopping_list(store_id
)
103 results
= ['Shopping List']
105 results
.append(str(item
))
109 if match
.command('unsorted'):
110 items
= Data().get_unsorted_list()
111 results
= ['Items assigned to no store']
113 results
.append(str(item
))
116 if match
.command('remove-item'):
117 item_id
= ' '.join(match
.args()[1:])
118 Data().remove_item(item_id
)
121 if match
.command('remove-store'):
122 store_id
= ' '.join(match
.args()[1:])
123 Data().remove_store(store_id
)
126 return [do_shop
.__doc
__]
128 async def shop_bot(room
, message
, bot
):
129 match
= botlib
.MessageMatch(room
, message
, bot
, PREFIX
)
131 if not match
.is_not_from_this_bot() and match
.prefix():
136 result_lines
= await do_shop(match
)
137 for line
in result_lines
:
138 await bot
.api
.send_text_message(
139 room
.room_id
, str(line
))
140 except Exception as ex
:
141 for line
in ['You have:', ' no tea', str(ex
)]:
142 await bot
.api
.send_text_message(
143 room
.room_id
, str(line
))