]>
us-delaporte.pagekite.me Git - matrix-shop-bot.git/blob - matrix_shop_bot/bot.py
581e19b7f180304fa145ff7e97f459ab2afb70ed
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 at STORE_ID - use this store for future commands
34 buy - mark item as purchased
35 clear STORE - clear all purchased items for STORE
36 list - re-show list from last used store
37 past STORE - re-show past purchased items from STORE
38 stores - list all stores
41 assign ITEM_ID - assign item to last used store id
42 assign.item.to.store ITEM_ID STORE_ID - plan to shop for at store for item
43 clear.all STORE - clear **all** items for STORE
44 remove ITEM_ID - remove a shopping item from the datatbase
45 remove.store STORE_ID - remove a store from the datatbase (requires clearing the list first)
46 setup - run one time database setup
47 unsorted - list all items not assigned to any store
51 if match
.command('setup'):
52 result
= Data().setup()
55 if match
.command('stores'):
56 items
= Data().get_stores()
57 results
= ['List of Stores']
59 results
.append(str(item
))
62 if match
.command('add'):
63 new
= ' '.join(match
.args()[1:])
64 result
= Data().add_item(new
)
65 results
= [f
'Item added with id {result}']
66 for item
in Data().get_last_shopping_list():
67 results
.append(str(item
))
70 if match
.command('add.store'):
71 new
= ' '.join(match
.args()[1:])
72 result
= Data().add_store(new
)
73 return [f
'Store added with id {result}']
75 if match
.command('assign.item.to.store'):
76 item_id
= int(match
.args()[1])
77 store_id
= int(match
.args()[2])
78 Data().assign_store(item_id
, store_id
)
81 if match
.command('assign'):
82 item_id
= int(match
.args()[1])
83 Data().assign_last_store(item_id
)
85 # Now print the list for the assigned store
86 items
= Data().get_last_shopping_list()
87 results
= ['Shopping List']
89 results
.append(str(item
))
92 if match
.command('buy'):
93 items
= ' '.join(match
.args()[1:])
94 results
= Data().purchase_items(items
)
96 # for item in Data().get_last_shopping_list():
97 # results.append(str(item))
100 if match
.command('clear'):
101 store_name
= ' '.join(match
.args()[1:])
102 results
= Data().clear_list(store_name
)
105 if match
.command('clear.all'):
106 store_name
= ' '.join(match
.args()[1:])
107 results
= Data().clear_list(store_name
, clear_all
=True)
110 if match
.command('at'):
111 store_name
= ' '.join(match
.args()[1:])
112 items
= Data().get_shopping_list(store_name
)
113 results
= ['Shopping List']
115 results
.append(str(item
))
118 if match
.command('list'):
119 items
= Data().get_last_shopping_list()
120 results
= ['Shopping List']
122 results
.append(str(item
))
126 if match
.command('past'):
127 store_name
= ' '.join(match
.args()[1:])
128 items
= Data().list_purchases(store_name
)
129 results
= ['Past purchases...']
131 results
.append(str(item
))
135 if match
.command('unsorted'):
136 items
= Data().get_unsorted_list()
137 results
= ['Items assigned to no store']
139 results
.append(str(item
))
142 if match
.command('remove'):
143 item_id
= ' '.join(match
.args()[1:])
144 Data().remove_item(item_id
)
147 if match
.command('remove.store'):
148 store_id
= ' '.join(match
.args()[1:])
149 Data().remove_store(store_id
)
153 if match
.command('extra.help'):
154 return [extended_help
]
156 return [do_shop
.__doc
__]
158 async def shop_bot(room
, message
, bot
):
159 match
= botlib
.MessageMatch(room
, message
, bot
, PREFIX
)
161 if not match
.is_not_from_this_bot() and match
.prefix():
166 result_lines
= await do_shop(match
)
167 for line
in result_lines
:
168 await bot
.api
.send_text_message(
169 room
.room_id
, str(line
))
170 except Exception as ex
:
171 for line
in ['You have:', ' no tea', str(ex
)]:
172 await bot
.api
.send_text_message(
173 room
.room_id
, str(line
))