]> apps.delaporte.us Git - matrix-chore-bot.git/blob - matrix_chore_bot/bot.py
Add ability to run with separate prefix and database file
[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 undo - mark a chore as undone
51 '''
52
53 if match.command('list'):
54 results = ['## Listing chores...']
55 for item in CHORES.list():
56 results.append(item)
57 results.append('-- End of List --')
58 return results
59
60 if match.command('setup'):
61 results = []
62 results.append('Doing chore setup...')
63 result = CHORES.setup()
64 results.append(str(result))
65 return results
66
67 if match.command('add'):
68 new_chore = ' '.join(match.args()[1:])
69 result = CHORES.add(new_chore)
70 return [result]
71
72 if match.command('check'):
73 chore_id = match.args()[1]
74 result = CHORES.check(chore_id)
75 return [result]
76
77 if match.command('done'):
78 chore_id = match.args()[1]
79 result = CHORES.done(chore_id)
80 return [result]
81
82 if match.command('undo'):
83 chore_id = match.args()[1]
84 result = CHORES.undo(chore_id)
85 return [result]
86
87 if match.command('remove'):
88 chore_id = match.args()[1]
89 result = CHORES.remove(chore_id)
90 return [result]
91
92 return [handle_chore.__doc__]
93