From a2f1647ccb0298f6c2b238fe3d282fbae171d9bc Mon Sep 17 00:00:00 2001
From: Thorsten <mail@aero2k.de>
Date: Thu, 4 Oct 2018 19:53:10 +0200
Subject: [PATCH] enable reverse lookup of commands to plugin names for help

---
 distbot/plugins/plugin_help.py | 16 +++++++++++++---
 1 file changed, 13 insertions(+), 3 deletions(-)
 mode change 100755 => 100644 distbot/plugins/plugin_help.py

diff --git a/distbot/plugins/plugin_help.py b/distbot/plugins/plugin_help.py
old mode 100755
new mode 100644
index ae650e1..ce52450
--- a/distbot/plugins/plugin_help.py
+++ b/distbot/plugins/plugin_help.py
@@ -31,6 +31,7 @@ class Plugins(Worker):
     usage = "bot: help [plugin name], bot: plugin activate/deactivate <plugin name>"
 
     plugin_storage = {}
+    reverse_lookup = {}
 
     def __init__(self, actionqueue, queue="work"):
         super(Plugins, self).__init__(actionqueue, queue)
@@ -46,15 +47,24 @@ class Plugins(Worker):
     def callback_plugins(self, ch, method, properties, body):
         body = json.loads(body.decode("utf-8"))
         logger.debug("received plugin in registry")
-        self.plugin_storage[body["name"]] = body
+        self.plugin_storage[body["name"].lower()] = body
         ch.basic_ack(delivery_tag=method.delivery_tag)
 
-    def send_help(self, plugin_name=None):
+    def build_reverse_lookup(self):
+        reverse_lookup = {}
+        for plugin, definition in self.plugin_storage.items():
+            for key in definition['binding_keys']:
+                if key.startswith("nick"):
+                    reverse_lookup[key.split('.')[1]] = plugin
 
+    def send_help(self, plugin_name=None):
         if not plugin_name:
             msg = "Known commands/reactions:\n" + ", ".join(self.plugin_storage.keys())
         else:
-            definition = self.plugin_storage[plugin_name]
+            definition = self.plugin_storage.get(plugin_name.lower()) \
+                         or self.plugin_storage.get(self.reverse_lookup.get(plugin_name.lower()))
+            if not definition:
+                return Action(msg="unknown command, try help")
             msg = ("{}: \n"
                    "Description:\t{}\n"
                    "Usage:\t{}\n\n").format(
-- 
2.47.3