]> us-delaporte.pagekite.me Git - matrix-goal-bot.git/blob - matrix_goal_bot/bot.py
Allow renaming goals
[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 if match.command('done'):
120 task_id = int(match.args()[1])
121 goal_id = Data().complete_task(task_id)
122 goal,tasks = Data().get_goal(goal_id)
123 results = pretty_print(goal, tasks)
124 return results
125
126 if match.command('list.all'):
127 goals = Data().get_all_goals()
128 results = ['# Everything is...']
129 for goal in goals:
130 _,tasks = Data().get_goal(goal.id)
131 details = pretty_print(goal, tasks)
132 results += details
133 return results
134
135 if match.command('visions'):
136 items = Data().get_all_visions()
137 results = ['# Visions']
138 if not items:
139 results.append('-- No current visions --')
140
141 for item in items:
142 results.append(item)
143 return results
144
145 if match.command('visions'):
146 items = Data().visionless()
147 results = ['# Goals with no vision']
148 if not items:
149 results.append('-- No visionless goals --')
150
151 for item in items:
152 results.append(item)
153 return results
154
155 if match.command('set.vision'):
156 goal_id = int(match.args()[1])
157 vision_id = int(match.args()[2])
158 Data().assign_vision(goal_id=goal_id, vision_id=vision_id)
159 vision,goals = Data().get_vision(vision_id)
160 results = pretty_print(vision, goals)
161 return results
162
163
164 if match.command('rename'):
165 goal_id = int(match.args()[1])
166 name = ' '.join(match.args()[2:])
167 Data().rename_goal(goal_id=goal_id, name=name)
168 goal,tasks = Data().get_goal(goal_id)
169 results = pretty_print(goal, tasks)
170 return results
171
172 # Help text
173 return [do_goal.__doc__]
174
175 async def goal_bot(room, message, bot):
176 match = botlib.MessageMatch(room, message, bot, PREFIX)
177
178 if not match.is_not_from_this_bot() and match.prefix():
179 return
180
181 try:
182 if match.prefix():
183 result_lines = await do_goal(match)
184 for line in result_lines:
185 await bot.api.send_text_message(
186 room.room_id, str(line))
187 except Exception as ex:
188 line ='Ye cannot get ye flask.'
189 await bot.api.send_text_message(
190 room.room_id, str(line))
191
192