]> us-delaporte.pagekite.me Git - matrix-shop-bot.git/blob - matrix_shop_bot/bot.py
f426663a8b8a6f42cb2a6f00a516e62ab13618c3
[matrix-shop-bot.git] / matrix_shop_bot / bot.py
1 from datetime import datetime as dt
2 import logging
3 import os
4
5 import simplematrixbotlib as botlib
6
7 from .db import Data
8
9 MODULE='shop'
10
11 DATE_FORMAT = '%Y-%m-%d'
12
13 def today():
14 return dt.now().strftime(DATE_FORMAT)
15
16 PREFIX = f'!{MODULE}'
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)
20
21 async def do_shop(match):
22 '''
23 Prefix: !shop
24
25 Synax:
26 !shop COMMAND ARGS
27
28 Commands:
29 help - show this help
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-stores - list all stores
38 list-unsorted - list all items not assigned to any store
39 remove 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
42 '''
43
44 if match.command('setup'):
45 result = Data().setup()
46 return [result]
47
48 if match.command('list-stores'):
49 items = Data().get_stores()
50 results = ['List of Stores']
51 for item in items:
52 results.append(str(item))
53 return results
54
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}']
59
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}']
64
65 if match.command('assign'):
66 store_id = int(match.args()[1])
67 item_id = int(match.args()[2])
68 Data().assign_store(store_id, item_id)
69
70 # Now print the list for the assigned store
71 items = Data().get_shopping_list(store_id)
72 results = ['Shopping List']
73 for item in items:
74 results.append(str(item))
75 return results
76
77 if match.command('buy'):
78 item_id = ' '.join(match.args()[1:])
79 Data().purchase_item(item_id)
80 return ['Success']
81
82 if match.command('clear'):
83 store_id = ' '.join(match.args()[1:])
84 Data().clear_list(store_id)
85 self.last_store_id = None
86 return ['Success']
87
88 if match.command('clear-all'):
89 store_id = ' '.join(match.args()[1:])
90 Data().clear_list(store_id, clear_all=True)
91 return ['Success']
92
93 if match.command('list'):
94 store_id = ' '.join(match.args()[1:])
95 if len(store_id) == 0:
96 store_id = None
97
98 items = Data().get_shopping_list(store_id)
99 results = ['Shopping List']
100 for item in items:
101 results.append(str(item))
102 return results
103
104
105 if match.command('unsorted'):
106 items = Data().list_unsorted()
107 for item in items:
108 results.append(str(item))
109 return results
110
111 if match.command('remove-item'):
112 item_id = ' '.join(match.args()[1:])
113 Data().remove_item(item_id)
114 return ['Success']
115
116 if match.command('remove-store'):
117 store_id = ' '.join(match.args()[1:])
118 Data().remove_store(store_id)
119 return ['Success']
120
121 return [do_shop.__doc__]
122
123 async def shop_bot(room, message, bot):
124 match = botlib.MessageMatch(room, message, bot, PREFIX)
125
126 if not match.is_not_from_this_bot() and match.prefix():
127 return
128
129 try:
130 if match.prefix():
131 result_lines = await do_shop(match)
132 for line in result_lines:
133 await bot.api.send_text_message(
134 room.room_id, str(line))
135 except Exception as ex:
136 for line in ['You have:', ' no tea']:
137 await bot.api.send_text_message(
138 room.room_id, str(line))
139
140
141