Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

changed help page parser so that the entire command name is matched b… #143

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/Tablebot/Internal/Handler/Command.hs
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ makeReadable (TrivialError i _ good) =
let (lab, others) = getLabel (toList good)
in case lab of
Just l -> (FancyError i . singleton . ErrorCustom $ KnownError l others, Just l)
Nothing -> (FancyError i . singleton $ ErrorCustom UnknownError, Nothing)
Nothing -> (FancyError i . singleton $ ErrorCustom $ KnownError "Unknown error without label" others, Nothing)
where
getLabel :: [ErrorItem (Token Text)] -> (Maybe String, [String])
getLabel [] = (Nothing, [])
Expand Down
17 changes: 10 additions & 7 deletions src/Tablebot/Utility/Help.hs
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,16 @@
module Tablebot.Utility.Help where

import Data.Default (Default (def))
import Data.Functor (($>))
import Data.Text (Text)
import qualified Data.Text as T
import Tablebot.Internal.Permission (getSenderPermission, userHasPermission)
import Tablebot.Internal.Plugins (changeAction)
import Tablebot.Internal.Types
import Tablebot.Utility.Discord (Message, sendMessage)
import Tablebot.Utility.Parser (skipSpace)
import Tablebot.Utility.Parser (skipSpace1)
import Tablebot.Utility.Permission (requirePermission)
import Tablebot.Utility.Types hiding (helpPages)
import Text.Megaparsec (choice, chunk, eof, try, (<?>), (<|>))
import Text.Megaparsec (choice, chunk, eof, (<|>))

helpHelpPage :: HelpPage
helpHelpPage = HelpPage "help" [] "show information about commands" "**Help**\nShows information about bot commands\n\n*Usage:* `help <page>`" [] None
Expand All @@ -32,15 +31,19 @@ generateHelp rootText p =
}

handleHelp :: Text -> [HelpPage] -> Parser (Message -> CompiledDatabaseDiscord ())
handleHelp rootText hp = parseHelpPage root
handleHelp rootText hp = base <|> parsePages
where
root = HelpPage "" [] "" rootText hp None
base = eof >> pure (displayHelp root) -- base help command
parsePages = choice $ map parseHelpPage hp -- one of the commands we're asking for help on

parseHelpPage :: HelpPage -> Parser (Message -> CompiledDatabaseDiscord ())
parseHelpPage hp = do
_ <- choice (map chunk (helpName hp : helpAliases hp))
skipSpace
(try eof $> displayHelp hp) <|> choice (map parseHelpPage $ helpSubpages hp) <?> "Unknown Subcommand"
_ <- choice (map chunk (helpName hp : helpAliases hp)) -- we parse out the command name
base <|> subcommands
where
base = eof >> pure (displayHelp hp)
subcommands = skipSpace1 >> choice (map parseHelpPage $ helpSubpages hp)

displayHelp :: HelpPage -> Message -> CompiledDatabaseDiscord ()
displayHelp hp m = changeAction () . requirePermission (helpPermission hp) m $ do
Expand Down