@@ -191,34 +191,50 @@ def is_private_message_from_another_user(message_dict, current_user_id):
191
191
return False
192
192
193
193
def setup_default_commands (bot_details , message_handler ):
194
- def def_about ():
195
- if bot_details ['description' ] == "" :
196
- return "**{}**" .format (bot_details ['name' ])
197
- return "**{}**: {}" .format (bot_details ['name' ], bot_details ['description' ])
194
+ def default_empty_response ():
195
+ return "Oops. Your message was empty."
198
196
199
- def def_help ():
200
- return ("\n " .join ("**{}** - {}" .format (k , v [1 ])
201
- for k , v in defaults .items () if k ) + "\n " +
202
- "\n " .join ("**{}** - {}" .format (k , v )
203
- for k , v in bot_details ['commands' ].items () if k ))
197
+ def default_about_response ():
198
+ if bot_details ['description' ] == "" :
199
+ return "**{name}**" .format (** bot_details )
200
+ return "**{name}**: {description}" .format (** bot_details )
204
201
205
- def def_commands ():
202
+ def default_commands_response ():
206
203
return "**Commands**: {} {}" .format (
207
- " " .join (k for k in defaults if k ),
208
- " " .join (k for k in bot_details ['commands' ] if k ))
209
- defaults = OrderedDict ([ # Variable definition required for callbacks above
210
- ('' , (lambda : "Oops. Your message was empty." , "[BLANK MESSAGE NOT SHOWN]" )),
211
- ('about' , (def_about , "The type and use of this bot" )),
204
+ " " .join (name for name in command_defaults if name ),
205
+ " " .join (name for name in bot_details ['commands' ] if name ))
206
+
207
+ def commands_list ():
208
+ return ("\n " .join ("**{}** - {}" .format (name , options [1 ])
209
+ for name , options in command_defaults .items () if name ) +
210
+ "\n " +
211
+ "\n " .join ("**{}** - {}" .format (name , description )
212
+ for name , description in bot_details ['commands' ].items () if name ))
213
+
214
+ def default_help_response ():
215
+ return "{}\n {}\n {}" .format (default_about_response (),
216
+ message_handler .usage (), commands_list ())
217
+ # FIXME Add boilerplate text above, to include eg. how to mention bot?
218
+ # Should usage be included?
219
+
220
+ command_defaults = OrderedDict ([ # Variable definition required for callbacks above
221
+ ('' , (default_empty_response , "[BLANK MESSAGE NOT SHOWN]" )),
222
+ ('about' , (default_about_response , "The type and use of this bot" )),
212
223
('usage' , ((lambda : message_handler .usage (), "Bot-provided usage text" ))),
213
- ('help' , (lambda : "{}\n {}\n {}" .format (def_about (), message_handler .usage (), def_help ()),
214
- "This help text" )),
215
- ('commands' , (def_commands , "A short list of supported commands" ))
224
+ ('help' , (default_help_response , "This help text" )),
225
+ ('commands' , (default_commands_response , "A short list of supported commands" ))
216
226
])
217
- return defaults
227
+ return command_defaults
228
+
229
+ def updated_default_commands (default_commands , bot_details ):
230
+ if not bot_details ['default_commands_enabled' ]:
231
+ return OrderedDict ()
232
+ exclude_list = bot_details ['commands' ] or ('commands' , 'help' )
233
+ return OrderedDict ((name ,option ) for name , option in default_commands .items ()
234
+ if name not in exclude_list )
218
235
219
- def sync_botdetails_defaultcommands (bot_details , default_commands ):
220
236
# Update default_commands from any changes in bot_details
221
- if not bot_details ['defaults ' ]: # Bot class will handle all commands
237
+ if not bot_details ['default_commands_enabled ' ]: # Bot class will handle all commands
222
238
default_commands = {}
223
239
else :
224
240
if len (bot_details ['commands' ]) == 0 : # No commands specified, so don't use this feature
@@ -228,9 +244,6 @@ def sync_botdetails_defaultcommands(bot_details, default_commands):
228
244
for command in bot_details ['commands' ]: # Bot commands override defaults
229
245
if command in default_commands :
230
246
del default_commands [command ]
231
- # Sync default_commands changes with bot_details
232
- if len (default_commands ) == 0 :
233
- bot_details ['defaults' ] = False
234
247
235
248
def run_message_handler_for_bot (lib_module , quiet , config_file , bot_name ):
236
249
# type: (Any, bool, str) -> Any
@@ -256,13 +269,16 @@ def run_message_handler_for_bot(lib_module, quiet, config_file, bot_name):
256
269
'name' : bot_name .capitalize (),
257
270
'description' : "" ,
258
271
'commands' : {},
259
- 'defaults ' : True ,
272
+ 'default_commands_enabled ' : True ,
260
273
}
261
274
bot_details .update (getattr (lib_module .handler_class , 'META' , {}))
262
275
263
276
# Initialise default commands, then override & sync with bot_details
264
277
default_commands = setup_default_commands (bot_details , message_handler )
265
- sync_botdetails_defaultcommands (bot_details , default_commands )
278
+ updated_defaults = updated_default_commands (default_commands , bot_details )
279
+ # Update bot_details if updated_defaults is empty
280
+ if len (updated_defaults ) == 0 :
281
+ bot_details ['default_commands_enabled' ] = False
266
282
267
283
if not quiet :
268
284
print ("Running {} Bot:" .format (bot_details ['name' ]))
@@ -288,16 +304,14 @@ def handle_message(message):
288
304
return
289
305
290
306
if is_private_message or is_mentioned :
291
- # Handle any default_commands first
292
- if len (default_commands ) > 0 :
293
- if '' in default_commands and len (message ['content' ]) == 0 :
294
- restricted_client .send_reply (message , default_commands ['' ][0 ]())
295
- return
296
- for command in default_commands :
297
- if command == '' :
298
- continue
299
- if message ['content' ].startswith (command ):
300
- restricted_client .send_reply (message , default_commands [command ][0 ]())
307
+ # Handle any default commands first
308
+ if len (updated_defaults ) > 0 :
309
+ for command in updated_defaults :
310
+ if command == '' and len (message ['content' ]) == 0 :
311
+ restricted_client .send_reply (message , updated_defaults ['' ][0 ]())
312
+ return
313
+ if command == message ['content' ]:
314
+ restricted_client .send_reply (message , updated_defaults [command ][0 ]())
301
315
return
302
316
# ...then pass anything else to bot to deal with
303
317
message_handler .handle_message (
0 commit comments