]> apps.delaporte.us Git - matrix-goal-bot.git/blob - matrix_goal_bot/bot.py
Fixup
[matrix-goal-bot.git] / matrix_goal_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, pretty_print
8
9 SERVER = os.environ.get('MATRIX_SERVER')
10 BOT_NAME = os.environ.get('GOAL_BOT')
11 BOT_PWD = os.environ.get('GOAL_BOT_PWD')
12
13 def get_bot():
14 creds = botlib.Creds(SERVER, BOT_NAME, BOT_PWD)
15 return botlib.Bot(creds)
16
17 DATE_FORMAT = '%Y-%m-%d'
18
19 def today():
20 return dt.now().strftime(DATE_FORMAT)
21
22 PREFIX = '!goal'
23
24 logger = logging.getLogger(__name__)
25 logging.basicConfig(filename='goals.log', encoding='utf-8', level=logging.DEBUG)
26 logging.getLogger('nio').setLevel(logging.WARNING)
27
28 async def do_goal(match):
29 '''
30 Prefix: !goal
31
32 Synax:
33 !goal COMMAND ARGS
34
35 Commands:
36 help - show this help
37 add - add a goal
38 add-task ID - add a task under the goal specified by ID
39 add-vision - add a new vision (collection of goals)
40 close-goal ID - mark goal done by ID
41 close-vision ID - mark vision as complete by ID
42 done ID - mark task done by ID
43 list - list incomplete goals
44 list-all - list everything
45 list-done - list completed goals
46 print GOAL_ID - print a goal and all tasks
47 print-vision ID - print a vision and all goals
48 set-vision GOAL_ID VISION_ID- associate GOAL_ID with VISION_ID
49 visions - list incomplete visions
50 '''
51
52 if match.command('setup'):
53 result = Data().setup()
54 return [result]
55
56 if match.command('add'):
57 new = ' '.join(match.args()[1:])
58 result = Data().add_goal(new)
59 return [f'Goal added with id {result}']
60
61 if match.command('list'):
62 goals = Data().get_all_goals()
63 results = ['# Goals list...']
64 for goal in goals:
65 results.append(str(goal))
66 return results
67
68
69 if match.command('list-done'):
70 goals = Data().get_done_goals()
71 results = ['# Completed Goals...']
72 for goal in goals:
73 results.append(str(goal))
74 return results
75
76
77 if match.command('add-task'):
78 goal_id = int(match.args()[1])
79 new = ' '.join(match.args()[2:])
80 result = Data().add_task(goal_id=goal_id, name=new)
81 goal,tasks = Data().get_goal(goal_id)
82 results = pretty_print(goal, tasks)
83 return results
84 # return [f'Added task with id {result}']
85
86 if match.command('add-vision'):
87 new = ' '.join(match.args()[1:])
88 result = Data().add_vision(new)
89 return [f'Vision added with id {result}']
90
91 if match.command('print'):
92 goal_id = int(match.args()[1])
93 logger.debug(f'Searching for goal {goal_id}...')
94 goal,tasks = Data().get_goal(goal_id)
95 results = pretty_print(goal, tasks)
96 return results
97
98
99 if match.command('print-vision'):
100 get_id = int(match.args()[1])
101 vision,goals = Data().get_vision(get_id)
102 results = pretty_print(vision, goals)
103 return results
104
105 if match.command('close-goal'):
106 goal_id = int(match.args()[1])
107 vision_id = Data().complete_goal(goal_id)
108 vision,goals = Data().get_vision(vision_id)
109 results = pretty_print(vision, goals)
110 return results
111
112 if match.command('close-vision'):
113 vision_id = int(match.args()[1])
114 Data().complete_vision(vision_id)
115 vision,goals = Data().get_vision(vision_id)
116 results = pretty_print(vision, goals)
117 return results
118
119
120 if match.command('done'):
121 task_id = int(match.args()[1])
122 goal_id = Data().complete_task(task_id)
123 goal,tasks = Data().get_goal(goal_id)
124 results = pretty_print(goal, tasks)
125 return results
126
127 if match.command('list-all'):
128 goals = Data().get_all_goals()
129 results = ['# Everything is...']
130 for goal in goals:
131 _,tasks = Data().get_goal(goal.id)
132 details = pretty_print(goal, tasks)
133 results += details
134 return results
135
136 if match.command('visions'):
137 items = Data().get_all_visions()
138 results = ['# Visions']
139 if not items:
140 results.append('-- No current visions --')
141
142 for item in items:
143 results.append(item)
144 return results
145
146
147 if match.command('set-vision'):
148 goal_id = int(match.args()[1])
149 vision_id = int(match.args()[2])
150 Data().assign_vision(goal_id=goal_id, vision_id=vision_id)
151 vision,goals = Data().get_vision(vision_id)
152 results = pretty_print(vision, goals)
153 return results
154
155 return [do_goal.__doc__]
156
157 async def goal_bot(room, message, bot):
158 match = botlib.MessageMatch(room, message, bot, PREFIX)
159
160 if not match.is_not_from_this_bot() and match.prefix():
161 return
162
163 try:
164 if match.prefix():
165 result_lines = await do_goal(match)
166 for line in result_lines:
167 await bot.api.send_text_message(
168 room.room_id, str(line))
169 except Exception as ex:
170 return ['Ye cannot get ye flask.']
171
172