]>
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 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
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
50 if match
.command('setup'):
51 result
= Data().setup()
54 if match
.command('stores'):
55 items
= Data().get_stores()
56 results
= ['List of Stores']
58 results
.append(str(item
))
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
))
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}']
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
)
80 if match
.command('assign'):
81 item_id
= int(match
.args()[1])
82 Data().assign_last_store(item_id
)
84 # Now print the list for the assigned store
85 items
= Data().get_last_shopping_list()
86 results
= ['Shopping List']
88 results
.append(str(item
))
91 if match
.command('buy'):
92 item_id
= ' '.join(match
.args()[1:])
93 Data().purchase_item(item_id
)
96 if match
.command('clear'):
97 store_id
= ' '.join(match
.args()[1:])
98 Data().clear_list(store_id
)
99 self
.last_store_id
= None
102 if match
.command('clear.all'):
103 store_id
= ' '.join(match
.args()[1:])
104 Data().clear_list(store_id
, clear_all
=True)
108 if match
.command('at'):
109 store_id
= ' '.join(match
.args()[1])
110 items
= Data().get_shopping_list_for_store(store_id
)
111 results
= ['Shopping List']
113 results
.append(str(item
))
116 if match
.command('list'):
117 items
= Data().get_last_shopping_list()
118 results
= ['Shopping List']
120 results
.append(str(item
))
123 if match
.command('unsorted'):
124 items
= Data().get_unsorted_list()
125 results
= ['Items assigned to no store']
127 results
.append(str(item
))
130 if match
.command('remove.item'):
131 item_id
= ' '.join(match
.args()[1:])
132 Data().remove_item(item_id
)
135 if match
.command('remove.store'):
136 store_id
= ' '.join(match
.args()[1:])
137 Data().remove_store(store_id
)
141 if match
.command('extra.help'):
142 return [extended_help
]
144 return [do_shop
.__doc
__]
146 async def shop_bot(room
, message
, bot
):
147 match
= botlib
.MessageMatch(room
, message
, bot
, PREFIX
)
149 if not match
.is_not_from_this_bot() and match
.prefix():
154 result_lines
= await do_shop(match
)
155 for line
in result_lines
:
156 await bot
.api
.send_text_message(
157 room
.room_id
, str(line
))
158 except Exception as ex
:
159 for line
in ['You have:', ' no tea', str(ex
)]:
160 await bot
.api
.send_text_message(
161 room
.room_id
, str(line
))