66
77from jinja2 import Environment , PackageLoader
88
9+ from operator import attrgetter
10+
911wi = None
1012
1113
@@ -17,6 +19,7 @@ def get_template_env():
1719def get_application ():
1820 app = Application ([
1921 (r'/' , TestHandler ),
22+ (r'/commands/' , CommandsHandler ),
2023 (r"/s/(.*)" , StaticFileHandler , {"path" : "./cloudbot/web/static" }),
2124 ])
2225 return app
@@ -35,6 +38,45 @@ def get(self):
3538 self .write (template .render (** args ))
3639
3740
41+ class CommandsHandler (RequestHandler ):
42+ @gen .coroutine
43+ def get (self ):
44+ template = wi .env .get_template ('commands.html' )
45+ commands = []
46+
47+ for plugin in sorted (set (wi .bot .plugin_manager .commands .values ()), key = attrgetter ("name" )):
48+ # use set to remove duplicate commands (from multiple aliases), and sorted to sort by name
49+ command = plugin .name
50+
51+ aliases = ", " .join ([i for i in plugin .aliases if not i == command ])
52+
53+ if aliases :
54+ cmd = "{} ({})" .format (command , aliases )
55+ else :
56+ cmd = "{}" .format (command )
57+
58+ if plugin .doc :
59+ if plugin .doc .split ()[0 ].isalpha ():
60+ doc = plugin .doc
61+ else :
62+ doc = "{} {}" .format (command , plugin .doc )
63+ else :
64+ doc = "Command has no documentation." .format (command )
65+
66+ if plugin .permissions :
67+ doc += " (Permission required: {})\n \n " .format (", " .join (plugin .permissions ))
68+
69+ commands .append ((cmd , doc ))
70+
71+ args = {
72+ 'bot_name' : wi .config .get ('bot_name' , 'CloudBot' ),
73+ 'bot_version' : cloudbot .__version__ ,
74+ 'commands' : commands
75+ }
76+
77+ self .write (template .render (** args ))
78+
79+
3880class WebInterface ():
3981 def __init__ (self , bot ):
4082 self .bot = bot
0 commit comments