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