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