]> us-delaporte.pagekite.me Git - matrix-chore-bot.git/blob - matrix_chore_bot/bot.py
Add filter command
[matrix-chore-bot.git] / matrix_chore_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 as ChoreData
8
9 SERVER = os.environ.get('MATRIX_SERVER')
10 BOT_NAME = os.environ.get('MATRIX_BOT')
11 BOT_PWD = os.environ.get('MATRIX_BOT_PWD')
12
13 CHORES = ChoreData()
14
15 logger = logging.getLogger(__name__)
16 logging.basicConfig(filename='matrix-chore-bot.log', encoding='utf-8', level=logging.DEBUG)
17 logging.getLogger('nio').setLevel(logging.WARNING)
18
19 async def chore_bot(room, message, bot, prefix='!chore'):
20 match = botlib.MessageMatch(room, message, bot, prefix)
21
22 if not match.is_not_from_this_bot() and match.prefix():
23 return
24
25 if match.prefix():
26 try:
27 result_lines = await handle_chore(match)
28 for line in result_lines:
29 await bot.api.send_text_message(
30 room.room_id, str(line))
31 except Exception as ex:
32 line = f'Ye flask must be load bearing, or something... {str(ex)}'
33 await bot.api.send_text_message(
34 room.room_id, str(line))
35
36
37 async def handle_chore(match):
38 '''
39 Prefix: !chore
40
41 Synax:
42 !chore COMMAND ARGS
43
44 Commands:
45 help - show this help
46 add - add a chore
47 check - check status of a chore
48 done - mark a chore as done
49 list - print the chore list
50 filter TERM - list all chores that include TERM
51 undo - mark a chore as undone
52 '''
53
54 if match.command('list'):
55 results = ['## Listing all...']
56 for item in CHORES.list():
57 results.append(item)
58 results.append('-- End of List --')
59 return results
60
61 if match.command('filter'):
62 term = ' '.join(match.args()[1:])
63 results = [f'## List matching {term}']
64 for item in CHORES.filter(term):
65 results.append(item)
66 results.append('-- End of List --')
67 return results
68
69 if match.command('setup'):
70 results = []
71 results.append('Doing chore setup...')
72 result = CHORES.setup()
73 results.append(str(result))
74 return results
75
76 if match.command('add'):
77 new_chore = ' '.join(match.args()[1:])
78 result = CHORES.add(new_chore)
79 return [result]
80
81 if match.command('check'):
82 chore_id = match.args()[1]
83 result = CHORES.check(chore_id)
84 return [result]
85
86 if match.command('done'):
87 chore_id = match.args()[1]
88 result = CHORES.done(chore_id)
89 return [result]
90
91 if match.command('undo'):
92 chore_id = match.args()[1]
93 result = CHORES.undo(chore_id)
94 return [result]
95
96 if match.command('remove'):
97 chore_id = match.args()[1]
98 result = CHORES.remove(chore_id)
99 return [result]
100
101 return [handle_chore.__doc__]
102