Debian Codegraph @

src/Lambdabot/Plugin/Core/Help.hs

raw
1 -- | Provide help for plugins
2 module Lambdabot.Plugin.Core.Help (helpPlugin) where
3
4 import Lambdabot.Command
5 import Lambdabot.Message (Message)
6 import Lambdabot.Module
7 import Lambdabot.Monad
8 import Lambdabot.Plugin
9 import Lambdabot.Util
10
11 import Control.Monad.Reader
12
13 helpPlugin :: Module ()
14 helpPlugin = newModule
15 { moduleCmds = return
16 [ (command "help")
17 { help = say "help <command>. Ask for help for <command>. Try 'list' for all commands"
18 , process = \args -> withMsg $ \msg -> do
19 tgt <- getTarget
20 lb (doHelp msg tgt args) >>= mapM_ say
21 }
22 ]
23 }
24
25 moduleHelp :: (Monad m, Message a) =>
26 Command m -> a -> Nick -> String -> m [String]
27 moduleHelp theCmd msg tgt cmd =
28 execCmd (help theCmd) msg tgt cmd
29
30 --
31 -- If a target is a command, find the associated help, otherwise if it's
32 -- a module, return a list of commands that module implements.
33 --
34 doHelp :: Message t => t -> Nick -> [Char] -> LB [[Char]]
35 doHelp msg tgt [] = doHelp msg tgt "help"
36 doHelp msg tgt rest =
37 withCommand arg -- see if it is a command
38 (inModuleNamed arg -- else maybe it's a module name
39 (doHelp msg tgt "help") -- else give up
40 (do -- its a module
41 cmds <- moduleCmds =<< asks theModule
42 let ss = cmds >>= cmdNames
43 let s | null ss = arg ++ " is a module."
44 | otherwise = arg ++ " provides: " ++ showClean ss
45 return [s]))
46
47 -- so it's a valid command, try to find its help
48 (\theCmd -> moduleHelp theCmd msg tgt arg)
49
50 where (arg:_) = words rest