]>
apps.delaporte.us Git - matrix-shop-bot.git/blob - matrix_shop_bot/bot.py
0e898e6036a008b4aaa4b07450cf680952d15279
1 from datetime
import datetime
as dt
5 import simplematrixbotlib
as botlib
7 from .db
import Data
, pretty_print
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 STORE_ID ITEM_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-unsorted - list all items not assigned to any store
38 remove ITEM_ID - remove a shopping item from the datatbase
39 remove-store STORE_ID - remove a store from the datatbase (requires clearing the list first)
40 setup - run one time database setup
43 if match
.command('setup'):
44 result
= Data().setup()
47 if match
.command('add'):
48 new
= ' '.join(match
.args()[1:])
49 result
= Data().add_item(new
)
50 return [f
'Item added with id {result}']
52 if match
.command('add-store'):
53 new
= ' '.join(match
.args()[1:])
54 result
= Data().add_store(new
)
55 return [f
'Store added with id {result}']
57 if match
.command('assign'):
58 store_id
= int(match
.args()[1])
59 item_id
= int(match
.args()[2])
60 result
= Data().assign_store(store_id
, item_id
)
63 if match
.command('buy'):
64 item_id
= ' '.join(match
.args()[1:])
65 Data().purchase_item(item_id
)
67 if match
.command('clear'):
68 store_id
= ' '.join(match
.args()[1:])
69 Data().clear_list(store_id
)
71 if match
.command('clear-all'):
72 store_id
= ' '.join(match
.args()[1:])
73 Data().clear_list(store_id
, clear_all
=True)
75 if match
.command('list'):
76 items
= Data().get_shopping_list(store_id
)
78 results
.append(str(item
))
81 if match
.command('list-stores'):
82 items
= Data().get_stores()
84 results
.append(str(item
))
87 if match
.command('unsorted'):
88 items
= Data().list_unsorted()
90 results
.append(str(item
))
93 if match
.command('remove-item'):
94 item_id
= ' '.join(match
.args()[1:])
95 Data().remove_item(item_id
)
97 if match
.command('remove-store'):
98 store_id
= ' '.join(match
.args()[1:])
99 Data().remove_store(store_id
)
101 return [do_shop
.__doc
__]
103 async def shop_bot(room
, message
, bot
):
104 match
= botlib
.MessageMatch(room
, message
, bot
, PREFIX
)
106 if not match
.is_not_from_this_bot() and match
.prefix():
111 result_lines
= await do_shop(match
)
112 for line
in result_lines
:
113 await bot
.api
.send_text_message(
114 room
.room_id
, str(line
))
115 except Exception as ex
:
116 return ['You have:', ' no tea']