]> us-delaporte.pagekite.me Git - matrix-chore-bot.git/blob - matrix_chore_bot/bot.py
Add invoke debug task to check db
[matrix-chore-bot.git] / matrix_chore_bot / bot.py
1 '''
2 Prefix: !chore
3
4 Synax:
5 !chore COMMAND ARGS
6
7 Commands:
8 help - show this help
9 add - add a chore
10 check - check status of a chore
11 done - mark a chore as done
12 list - print the chore list
13 filter TERM - list all chores that include TERM
14 undo - mark a chore as undone
15 '''
16
17 from datetime import datetime as dt
18 import logging
19 import os
20
21 import simplematrixbotlib as botlib
22
23 from .db import Data as ChoreData
24
25 logger = logging.getLogger(__name__)
26 logging.basicConfig(filename='matrix-chore-bot.log', encoding='utf-8', level=logging.DEBUG)
27 logging.getLogger('nio').setLevel(logging.WARNING)
28
29 class ChoreBot():
30 def __init__(self, matrix_server,
31 bot_name,
32 bot_pwd,
33 db_file=None,
34 bot_prefix='!chore'):
35
36 creds = botlib.Creds(matrix_server, bot_name, bot_pwd)
37 self.bot = botlib.Bot(creds)
38 self.prefix = bot_prefix
39 self.data = ChoreData(db_file=db_file)
40
41 def run(self):
42 self.bot.run()
43
44 async def handle_message(self, room, message):
45 match = botlib.MessageMatch(room, message, self.bot, self.prefix)
46
47 if not match.is_not_from_this_bot() and match.prefix():
48 return
49
50 if match.prefix():
51 try:
52 result_lines = await self._handle_match(match)
53 for line in result_lines:
54 await self.bot.api.send_text_message(
55 room.room_id, str(line))
56 except Exception as ex:
57 line = f'Ye flask must be load bearing, or something... {str(ex)}'
58 await self.bot.api.send_text_message(
59 room.room_id, str(line))
60
61
62 async def _handle_match(self, match):
63
64 if match.command('list'):
65 results = ['## Listing all...']
66 for item in self.data.list():
67 results.append(item)
68 results.append('-- End of List --')
69 return results
70
71 if match.command('filter'):
72 term = ' '.join(match.args()[1:])
73 results = [f'## List matching {term}']
74 for item in self.data.filter(term):
75 results.append(item)
76 results.append('-- End of List --')
77 return results
78
79 if match.command('setup'):
80 results = []
81 results.append('Doing chore setup...')
82 result = self.data.setup()
83 results.append(str(result))
84 return results
85
86 if match.command('add'):
87 new_chore = ' '.join(match.args()[1:])
88 result = self.data.add(new_chore)
89 return [result]
90
91 if match.command('check'):
92 chore_id = match.args()[1]
93 result = self.data.check(chore_id)
94 return [result]
95
96 if match.command('done'):
97 ids = match.args()[1:]
98 results = []
99 for chore_id in ids:
100 result = self.data.done(chore_id)
101 results.append(result)
102 return results
103
104 if match.command('undo'):
105 chore_id = match.args()[1]
106 result = self.data.undo(chore_id)
107 return [result]
108
109 if match.command('remove'):
110 chore_id = match.args()[1]
111 result = self.data.remove(chore_id)
112 return [result]
113
114 return [__doc__]
115