]> apps.delaporte.us Git - matrix-chore-bot.git/blob - matrix_chore_bot/bot.py
Add more snark
[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.getLogger('nio').setLevel(logging.WARNING)
27
28 snark = [
29 'Ye cannot get ye flask.',
30 'Ye flask must be load bearing, or something...',
31 '''You have:
32 no tea
33 ''',
34 'How are you gentlemen?',
35 'All your base are belong to us.',
36 'You have been eaten by a groo.'
37 ]
38
39
40
41 class ChoreBot():
42 def __init__(self, matrix_server,
43 bot_name,
44 bot_pwd,
45 db_file=None,
46 bot_prefix='!chore'):
47
48 logging.basicConfig(filename='matrix-chore-bot.log', encoding='utf-8', level=logging.DEBUG)
49
50
51 creds = botlib.Creds(matrix_server, bot_name, bot_pwd)
52 self.bot = botlib.Bot(creds)
53 self.prefix = bot_prefix
54 self.data = ChoreData(db_file=db_file)
55
56 def run(self):
57 self.bot.run()
58
59 async def handle_message(self, room, message):
60 match = botlib.MessageMatch(room, message, self.bot, self.prefix)
61
62 if not match.is_not_from_this_bot() and match.prefix():
63 return
64
65 if match.prefix():
66 try:
67 result_lines = await self._handle_match(match)
68 for line in result_lines:
69 await self.bot.api.send_text_message(
70 room.room_id, str(line))
71
72 except Exception as ex:
73 logger.warn(f'Caught exception: {str(ex)}')
74 dumb_msg_idx = len(str(ex)) % len(snark)
75 dumb_msg = snark[dumb_msg_idx]
76 await self.bot.api.send_text_message(
77 room.room_id, dumb_msg)
78
79 await self.bot.api.send_text_message(
80 room.room_id, str(ex))
81
82 except Exception as ex:
83 line = f'Ye flask must be load bearing, or something... {str(ex)}'
84 await self.bot.api.send_text_message(
85 room.room_id, str(line))
86
87
88 async def _handle_match(self, match):
89
90 if match.command('list'):
91 results = ['## Listing all...']
92 for item in self.data.list():
93 results.append(item)
94 results.append('-- End of List --')
95 return results
96
97 if match.command('filter'):
98 term = ' '.join(match.args()[1:])
99 results = [f'## List matching {term}']
100 for item in self.data.filter(term):
101 results.append(item)
102 results.append('-- End of List --')
103 return results
104
105 if match.command('setup'):
106 results = []
107 results.append('Doing chore setup...')
108 result = self.data.setup()
109 results.append(str(result))
110 return results
111
112 if match.command('add'):
113 new_chore = ' '.join(match.args()[1:])
114 result = self.data.add(new_chore)
115 return [result]
116
117 if match.command('check'):
118 chore_id = match.args()[1]
119 result = self.data.check(chore_id)
120 return [result]
121
122 if match.command('done'):
123 ids = match.args()[1:]
124 results = []
125 for chore_id in ids:
126 result = self.data.done(chore_id)
127 results.append(result)
128 return results
129
130 if match.command('undo'):
131 chore_id = match.args()[1]
132 result = self.data.undo(chore_id)
133 return [result]
134
135 if match.command('remove'):
136 chore_id = match.args()[1]
137 result = self.data.remove(chore_id)
138 return [result]
139
140 return [__doc__]
141