]>
apps.delaporte.us Git - matrix-goal-bot.git/blob - matrix_goal_bot/bot.py
1 from datetime
import datetime
as dt
5 import simplematrixbotlib
as botlib
7 from .db
import Data
, pretty_print
9 SERVER
= os
.environ
.get('MATRIX_SERVER')
10 BOT_NAME
= os
.environ
.get('GOAL_BOT')
11 BOT_PWD
= os
.environ
.get('GOAL_BOT_PWD')
14 creds
= botlib
.Creds(SERVER
, BOT_NAME
, BOT_PWD
)
15 return botlib
.Bot(creds
)
17 DATE_FORMAT
= '%Y-%m-%d'
20 return dt
.now().strftime(DATE_FORMAT
)
24 logger
= logging
.getLogger(__name__
)
25 logging
.basicConfig(filename
='goals.log', encoding
='utf-8', level
=logging
.DEBUG
)
26 logging
.getLogger('nio').setLevel(logging
.WARNING
)
28 async def do_goal(match
):
37 extra.help - show extended help
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
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
56 if match
.command('setup'):
57 result
= Data().setup()
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}']
65 if match
.command('list'):
66 goals
= Data().get_all_goals()
67 results
= ['# Goals list...']
69 results
.append(str(goal
))
73 if match
.command('list.done'):
74 goals
= Data().get_done_goals()
75 results
= ['# Completed Goals...']
77 results
.append(str(goal
))
80 if match
.command('task'):
81 new
= ' '.join(match
.args()[1:])
82 goal_id
= Data().quick_add_task(new
)
83 goal
,tasks
= Data().get_goal(goal_id
)
84 results
= pretty_print(goal
, tasks
)
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}']
92 if match
.command('print'):
93 if len(match
.args()) > 1:
94 goal_id
= int(match
.args()[1])
96 goal_id
= Data()._get
_pref
_goal
_id
()
98 logger
.debug(f
'Searching for goal {goal_id}...')
99 goal
,tasks
= Data().get_goal(goal_id
)
100 results
= pretty_print(goal
, tasks
)
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
)
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
)
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
)
125 if match
.command('done'):
126 ids
= match
.args()[1:]
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
)
134 if match
.command('list.all'):
135 goals
= Data().get_all_goals()
136 results
= ['# Everything is...']
138 _
,tasks
= Data().get_goal(goal
.id)
139 details
= pretty_print(goal
, tasks
)
143 if match
.command('visions'):
144 items
= Data().get_all_visions()
145 results
= ['# Visions']
147 results
.append('-- No current visions --')
153 if match
.command('visionless'):
154 items
= Data().visionless()
155 results
= ['# Goals with no vision']
157 results
.append('-- No visionless goals --')
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
)
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
)
180 if match
.command('extra.help'):
184 return [do_goal
.__doc
__]
186 async def goal_bot(room
, message
, bot
):
187 match
= botlib
.MessageMatch(room
, message
, bot
, PREFIX
)
189 if not match
.is_not_from_this_bot() and 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
))