]> us-delaporte.pagekite.me Git - matrix-shop-bot.git/blob - matrix_shop_bot/bot.py
Add missing functions
[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, pretty_print
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 list STORE_ID - list all items assigned to store
34 list-unsorted - list all items not assigned to any store
35 setup - run one time database setup
36 '''
37
38 if match.command('setup'):
39 result = Data().setup()
40 return [result]
41
42 if match.command('add'):
43 new = ' '.join(match.args()[1:])
44 result = Data().add_item(new)
45 return [f'Item added with id {result}']
46
47 if match.command('add-store'):
48 new = ' '.join(match.args()[1:])
49 result = Data().add_store(new)
50 return [f'Store added with id {result}']
51
52
53 if match.command('list'):
54 items = Data().get_shopping_list(store_id)
55 for item in items:
56 results.append(str(item))
57 return results
58
59 if match.command('unsorted'):
60 items = Data().list_unsorted()
61 for item in items:
62 results.append(str(item))
63 return results
64
65 if match.command('assign'):
66 store_id = int(match.args()[1])
67 item_id = int(match.args()[2])
68 result = Data().assign_store(store_id, item_id)
69 return result
70
71 return [do_shop.__doc__]
72
73 async def shop_bot(room, message, bot):
74 match = botlib.MessageMatch(room, message, bot, PREFIX)
75
76 if not match.is_not_from_this_bot() and match.prefix():
77 return
78
79 try:
80 if match.prefix():
81 result_lines = await do_shop(match)
82 for line in result_lines:
83 await bot.api.send_text_message(
84 room.room_id, str(line))
85 except Exception as ex:
86 return ['You have:', ' no tea']
87
88