]> apps.delaporte.us Git - matrix-fridge-bot.git/blob - matrix_fridge_bot/bot.py
Fixup
[matrix-fridge-bot.git] / matrix_fridge_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.getLogger('nio').setLevel(logging.WARNING)
27
28 class ChoreBot():
29 def __init__(self, matrix_server,
30 bot_name,
31 bot_pwd,
32 db_file=None,
33 bot_prefix='!chore'):
34
35 logging.basicConfig(filename='matrix-chore-bot.log', encoding='utf-8', level=logging.DEBUG)
36
37
38 creds = botlib.Creds(matrix_server, bot_name, bot_pwd)
39 self.bot = botlib.Bot(creds)
40 self.prefix = bot_prefix
41 self.data = ChoreData(db_file=db_file)
42
43 def run(self):
44 self.bot.run()
45
46 async def handle_message(self, room, message):
47 match = botlib.MessageMatch(room, message, self.bot, self.prefix)
48
49 if not match.is_not_from_this_bot() and match.prefix():
50 return
51
52 if match.prefix():
53 try:
54 result_lines = await self._handle_match(match)
55 for line in result_lines:
56 await self.bot.api.send_text_message(
57 room.room_id, str(line))
58 except Exception as ex:
59 line = f'Ye flask must be load bearing, or something... {str(ex)}'
60 await self.bot.api.send_text_message(
61 room.room_id, str(line))
62
63
64 async def _handle_match(self, match):
65
66 if match.command('list'):
67 results = ['## Listing all...']
68 for item in self.data.list():
69 results.append(item)
70 results.append('-- End of List --')
71 return results
72
73 if match.command('filter'):
74 term = ' '.join(match.args()[1:])
75 results = [f'## List matching {term}']
76 for item in self.data.filter(term):
77 results.append(item)
78 results.append('-- End of List --')
79 return results
80
81 if match.command('setup'):
82 results = []
83 results.append('Doing chore setup...')
84 result = self.data.setup()
85 results.append(str(result))
86 return results
87
88 if match.command('add'):
89 new_chore = ' '.join(match.args()[1:])
90 result = self.data.add(new_chore)
91 return [result]
92
93 if match.command('check'):
94 chore_id = match.args()[1]
95 result = self.data.check(chore_id)
96 return [result]
97
98 if match.command('done'):
99 ids = match.args()[1:]
100 results = []
101 for chore_id in ids:
102 result = self.data.done(chore_id)
103 results.append(result)
104 return results
105
106 if match.command('undo'):
107 chore_id = match.args()[1]
108 result = self.data.undo(chore_id)
109 return [result]
110
111 if match.command('remove'):
112 chore_id = match.args()[1]
113 result = self.data.remove(chore_id)
114 return [result]
115
116 return [__doc__]
117