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