- added .help and .commands

Sat, 29 Nov 2014 17:45:20 +0200

author
Teemu Piippo <crimsondusk64@gmail.com>
date
Sat, 29 Nov 2014 17:45:20 +0200
changeset 102
2bad379cd416
parent 101
5e32ab7ae823
child 104
27294de7b0d3

- added .help and .commands

mod_util.py file | annotate | diff | comparison | revisions
modulecore.py file | annotate | diff | comparison | revisions
--- a/mod_util.py	Sat Nov 29 17:03:10 2014 +0200
+++ b/mod_util.py	Sat Nov 29 17:45:20 2014 +0200
@@ -2,6 +2,7 @@
 import urllib2
 import json
 from modulecore import command_error
+import modulecore as ModuleCore
 
 ModuleData = {
 	'commands':
@@ -19,6 +20,20 @@
 			'args': '<term...>',
 			'level': 'normal',
 		},
+
+		{
+			'name': 'commands',
+			'description': 'Lists commands available to the calling user',
+			'args': None,
+			'level': 'normal',
+		},
+
+		{
+			'name': 'help',
+			'description': 'Prints help about a given command',
+			'args': '<command>',
+			'level': 'normal',
+		},
 	]
 }
 
@@ -70,4 +85,26 @@
 		down = data['list'][0]['thumbs_down']
 		bot.privmsg (replyto, "\002%s\002: %s\0033 %d\003 up,\0035 %d\003 down" % (word, definition, up, down))
 	except Exception as e:
-		command_error ('Urban dictionary lookup failed: %s' % e)
\ No newline at end of file
+		command_error ('Urban dictionary lookup failed: %s' % e)
+
+def cmd_commands (bot, replyto, ident, host, **rest):
+	commandlist = ModuleCore.get_available_commands (ident, host)
+	partitioned=[]
+
+	while len (commandlist) > 0:
+		partitioned.append (commandlist[0:15])
+		commandlist = commandlist[15:]
+
+	for part in partitioned:
+		bot.privmsg (replyto, 'Available commands: %s' % (", ".join (part)))
+
+def cmd_help (bot, replyto, ident, host, args, **rest):
+	cmd = ModuleCore.get_command_by_name (args['command'])
+
+	if not cmd:
+		command_error ('unknown command \'%s\'' % args['command'])
+
+	if not ModuleCore.is_available (cmd, ident, host):
+		command_error ('you may not use %s' % cmd['name'])
+
+	bot.privmsg (replyto, '%s %s: %s' % (cmd['name'], cmd['args'], cmd['description']))
--- a/modulecore.py	Sat Nov 29 17:03:10 2014 +0200
+++ b/modulecore.py	Sat Nov 29 17:45:20 2014 +0200
@@ -67,6 +67,18 @@
 	raise CommandError (message)
 
 #
+# is_available (cmd, ident, host)
+#
+# Is the given command available to the given user?
+#
+def is_available (cmd, ident, host):
+	if cmd['level'] == 'admin' \
+	and not "%s@%s" % (ident, host) in Config.get_value ('admins', default=[]):
+		return False
+
+	return True
+
+#
 # call_command (bot, message, cmdname, **kvargs)
 #
 # Calls a cobalt command
@@ -77,8 +89,7 @@
 	except KeyError:
 		return False
 	
-	if cmd['level'] == 'admin' \
-	and not "%s@%s" % (kvargs['ident'], kvargs['host']) in Config.get_value ('admins', default=[]):
+	if not is_available (cmd, kvargs['ident'], kvargs['host']):
 		command_error ("you may not use %s" % cmdname)
 	
 	match = re.compile (cmd['regex']).match (message)
@@ -99,6 +110,33 @@
 	return True
 
 #
+# get_available_commands
+#
+# Gets a list of commands available to the given user
+#
+def get_available_commands (ident, host):
+	result=[]
+
+	for cmdname,cmd in Commands.iteritems():
+		if not is_available (cmd, ident, host):
+			continue
+
+		result.append (cmdname)
+
+	return result
+
+#
+# get_command_by_name
+#
+# Gets a command by name
+#
+def get_command_by_name (name):
+	try:
+		return Commands[name]
+	except:
+		return None
+
+#
 # make_regex
 #
 # Takes the argument list and returns a corresponding regular expression
@@ -156,4 +194,4 @@
 	#done
 
 	return '^[^ ]+%s$' % regex
-#enddef
\ No newline at end of file
+#enddef

mercurial