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