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